serverctl/services/entry/pkg/api/api.go
2022-02-17 12:57:45 +01:00

61 lines
1.2 KiB
Go

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"
// ServerctlApi contains everything required for the api to run
type ServerctlApi struct {
logger *zap.Logger
router *gin.Engine
routingTable *routers.RoutingTable
dependencies *dependencies.Dependencies
}
// NewServerctlApi Creates a new api struct
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))
}()
}