serverctl/services/entry/pkg/api/api.go

61 lines
1.2 KiB
Go
Raw Normal View History

2022-02-16 16:27:48 +01:00
package api
import (
"github.com/gin-gonic/gin"
"go.uber.org/zap"
"log"
"net/http"
"serverctl/pkg/api/routers"
"serverctl/pkg/infrastructure/dependencies"
)
// Used for profiling
import _ "net/http/pprof"
2022-02-17 12:57:45 +01:00
// ServerctlApi contains everything required for the api to run
2022-02-16 16:27:48 +01:00
type ServerctlApi struct {
logger *zap.Logger
router *gin.Engine
routingTable *routers.RoutingTable
dependencies *dependencies.Dependencies
}
2022-02-17 12:57:45 +01:00
// NewServerctlApi Creates a new api struct
2022-02-16 16:27:48 +01:00
func NewServerctlApi(dependencies *dependencies.Dependencies) *ServerctlApi {
return &ServerctlApi{dependencies: dependencies}
}
func (a *ServerctlApi) SetupApi() *ServerctlApi {
a.dependencies.Logger.Info("Setting up serverctl setupApi (using gin)")
a.router = gin.Default()
a.setupCommonMiddleware().setupRoutingTable()
return a
}
func (a *ServerctlApi) RunApi() {
runProfilerHttpServer()
err := a.router.Run(":8080")
if err != nil {
panic(err)
}
}
func (a *ServerctlApi) setupCommonMiddleware() *ServerctlApi {
return a
}
func (a *ServerctlApi) setupRoutingTable() {
a.routingTable = routers.
NewRoutingTable(a.router, a.dependencies).
Setup()
}
func runProfilerHttpServer() {
go func() {
log.Println(http.ListenAndServe(":6060", nil))
}()
}