Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
hrmhatef committed Feb 26, 2024
1 parent 825a0fe commit 957baa4
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 18 deletions.
1 change: 1 addition & 0 deletions cmd/faultdetector/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ func (app *App) Start() {
app.wg.Add(1)
go app.faultDetector.Start()

app.apiServer.AddHandler(app.faultDetector, app.config.Api.RegisterVersions)
app.wg.Add(1)
go app.apiServer.Start()

Expand Down
12 changes: 7 additions & 5 deletions pkg/api/handlers/v1/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ type statusResponse struct {
}

// GetStatus is the handler for the 'GET /api/v1/status' endpoint.
func GetStatus(c *gin.Context) {
status := statusResponse{
Status: "ok",
func GetStatus(c *gin.Context, b bool) {
var s statusResponse
if b {
s.Status = "ok"
} else {
s.Status = "not"
}

c.IndentedJSON(http.StatusOK, status)
c.IndentedJSON(http.StatusOK, s)
}
16 changes: 8 additions & 8 deletions pkg/api/routes/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package routes

import (
"github.com/LiskHQ/op-fault-detector/pkg/api/handlers"
v1 "github.com/LiskHQ/op-fault-detector/pkg/api/handlers/v1"
"github.com/LiskHQ/op-fault-detector/pkg/log"
"github.com/gin-gonic/gin"
)
Expand All @@ -22,13 +21,14 @@ func RegisterHandlersByGroup(logger log.Logger, routerGroup *gin.RouterGroup, ve

// RegisterHandlersForVersion is responsible to register API version specific route handlers.
func RegisterHandlersForVersion(logger log.Logger, routerGroup *gin.RouterGroup, version string) {
group := routerGroup.Group(version)
// TODO should remove?
// group := routerGroup.Group(version)

switch version {
case "v1":
group.GET("/status", v1.GetStatus)
// switch version {
// case "v1":
// group.GET("/status", v1.GetStatus)

default:
logger.Warningf("No routes and handlers defined for version %s. Please verify the API config.", version)
}
// default:
// logger.Warningf("No routes and handlers defined for version %s. Please verify the API config.", version)
// }
}
31 changes: 27 additions & 4 deletions pkg/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import (
"sync"
"time"

v1 "github.com/LiskHQ/op-fault-detector/pkg/api/handlers/v1"
"github.com/LiskHQ/op-fault-detector/pkg/api/middlewares"
"github.com/LiskHQ/op-fault-detector/pkg/api/routes"
"github.com/LiskHQ/op-fault-detector/pkg/config"
"github.com/LiskHQ/op-fault-detector/pkg/faultdetector"
"github.com/LiskHQ/op-fault-detector/pkg/log"
"github.com/gin-gonic/gin"
)
Expand All @@ -25,6 +27,26 @@ type HTTPServer struct {
errorChan chan error
}

// TODO add comment
func (w *HTTPServer) AddHandler(fd *faultdetector.FaultDetector, versions []string) {
basePath := w.router.BasePath()
baseGroup := w.router.Group(basePath)
for _, version := range versions {
group := baseGroup.Group(version)
switch version {
case "v1":
group.GET("/status", func(c *gin.Context) {
v1.GetStatus(c, fd.GetStatus())
})

default:
w.logger.Warningf("No routes and handlers defined for version %s. Please verify the API config.", version)
}
}

}

Check failure on line 47 in pkg/api/server.go

View workflow job for this annotation

GitHub Actions / check

unnecessary trailing newline (whitespace)

// TODO check register handler already called before start
// Start starts the HTTP API server.
func (w *HTTPServer) Start() {
defer w.wg.Done()
Expand Down Expand Up @@ -74,11 +96,12 @@ func NewHTTPServer(ctx context.Context, logger log.Logger, wg *sync.WaitGroup, c

routes.RegisterHandlers(logger, router)

// TODO should remove
// Register handlers for routes following the base path
basePath := config.Api.BasePath
baseGroup := router.Group(basePath)
logger.Debugf("Registering handlers for endpoints under path '%s'.", basePath)
routes.RegisterHandlersByGroup(logger, baseGroup, config.Api.RegisterVersions)
// basePath := config.Api.BasePath
// baseGroup := router.Group(basePath)
// logger.Debugf("Registering handlers for endpoints under path '%s'.", basePath)
// routes.RegisterHandlersByGroup(logger, baseGroup, config.Api.RegisterVersions)

host := config.Api.Server.Host
port := config.Api.Server.Port
Expand Down
8 changes: 7 additions & 1 deletion pkg/faultdetector/faultdetector.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type FaultDetector struct {
oracleContractAccessor OracleAccessor
faultProofWindow uint64
currentOutputIndex uint64
diverged bool
diverged bool // TODO add mutex
ticker *time.Ticker
quitTickerChan chan struct{}
notification *notification.Notification
Expand Down Expand Up @@ -271,6 +271,12 @@ func (fd *FaultDetector) checkFault() error {
return nil
}

// TODO add comments
func (fd *FaultDetector) GetStatus() bool {
return fd.diverged
}

// TODO add comments
func GetFaultDetector(ctx context.Context, logger log.Logger, l1RpcApi *chain.ChainAPIClient, l2RpcApi *chain.ChainAPIClient, oracleContractAccessor OracleAccessor, faultProofWindow uint64, currentOutputIndex uint64, metrics *faultDetectorMetrics, notification *notification.Notification, diverged bool, wg *sync.WaitGroup, errorChan chan error) *FaultDetector {
return &FaultDetector{
ctx: ctx,
Expand Down

0 comments on commit 957baa4

Please sign in to comment.