2022-12-05 12:41:37 -08:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
|
2023-06-26 08:47:22 -07:00
|
|
|
"github.com/ansrivas/fiberprometheus/v2"
|
2022-12-05 12:41:37 -08:00
|
|
|
"github.com/gofiber/fiber/v2"
|
2023-01-26 09:55:27 -08:00
|
|
|
"github.com/gofiber/fiber/v2/middleware/pprof"
|
2023-08-08 07:11:35 -07:00
|
|
|
"github.com/wormhole-foundation/wormhole-explorer/common/client/alert"
|
2023-02-13 12:28:34 -08:00
|
|
|
"github.com/wormhole-foundation/wormhole-explorer/fly/internal/health"
|
2022-12-05 12:41:37 -08:00
|
|
|
"github.com/wormhole-foundation/wormhole-explorer/fly/internal/sqs"
|
|
|
|
"github.com/wormhole-foundation/wormhole-explorer/fly/storage"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Server struct {
|
|
|
|
app *fiber.App
|
|
|
|
port string
|
|
|
|
logger *zap.Logger
|
|
|
|
}
|
|
|
|
|
2023-08-08 07:11:35 -07:00
|
|
|
func NewServer(guardianCheck *health.GuardianCheck, logger *zap.Logger, repository *storage.Repository, consumer *sqs.Consumer, isLocal, pprofEnabled bool, alertClient alert.AlertClient) *Server {
|
2022-12-05 12:41:37 -08:00
|
|
|
port := os.Getenv("API_PORT")
|
|
|
|
if port == "" {
|
|
|
|
logger.Fatal("You must set your 'API_PORT' environmental variable")
|
|
|
|
}
|
2023-08-08 07:11:35 -07:00
|
|
|
ctrl := NewController(guardianCheck, repository, consumer, isLocal, alertClient, logger)
|
2023-02-03 10:58:58 -08:00
|
|
|
app := fiber.New(fiber.Config{DisableStartupMessage: true})
|
2023-06-26 08:47:22 -07:00
|
|
|
|
|
|
|
// Configure middleware
|
|
|
|
prometheus := fiberprometheus.New("wormscan-fly")
|
|
|
|
prometheus.RegisterAt(app, "/metrics")
|
|
|
|
app.Use(prometheus.Middleware)
|
|
|
|
|
2023-01-26 09:55:27 -08:00
|
|
|
// config use of middlware.
|
|
|
|
if pprofEnabled {
|
|
|
|
app.Use(pprof.New())
|
|
|
|
}
|
2022-12-05 12:41:37 -08:00
|
|
|
api := app.Group("/api")
|
|
|
|
api.Get("/health", ctrl.HealthCheck)
|
|
|
|
api.Get("/ready", ctrl.ReadyCheck)
|
|
|
|
return &Server{
|
|
|
|
app: app,
|
|
|
|
port: port,
|
|
|
|
logger: logger,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start listen serves HTTP requests from addr.
|
|
|
|
func (s *Server) Start() {
|
|
|
|
go func() {
|
|
|
|
s.app.Listen(":" + s.port)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop gracefull server.
|
|
|
|
func (s *Server) Stop() {
|
|
|
|
_ = s.app.Shutdown()
|
|
|
|
}
|