Move into api routers instead of main

This commit is contained in:
2022-02-16 16:27:48 +01:00
parent f35f277b16
commit c3946df1ff
16 changed files with 561 additions and 358 deletions

View File

@@ -0,0 +1,58 @@
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"
type ServerctlApi struct {
logger *zap.Logger
router *gin.Engine
routingTable *routers.RoutingTable
dependencies *dependencies.Dependencies
}
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))
}()
}