Move into api routers instead of main
This commit is contained in:
58
services/entry/pkg/api/api.go
Normal file
58
services/entry/pkg/api/api.go
Normal 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))
|
||||
}()
|
||||
}
|
Reference in New Issue
Block a user