go-scopes/server/server.go

51 lines
922 B
Go

package server
import (
"net/http"
lib "git.sr.ht/~cco/go-scopes"
"git.sr.ht/~cco/go-scopes/config"
"git.sr.ht/~cco/go-scopes/core"
"github.com/gin-gonic/gin"
)
type Cfg struct {
*config.BaseCfg
Port string
}
type ServerState struct {
server *http.Server
}
func Start(ctx lib.Context) {
//gin.SetMode(gin.ReleaseMode)
lib.GetCfg[*Cfg](ctx).BaseCfg.WithDoneHandler(HandleDone)
Serve(ctx)
lib.RunCtx(ctx, core.Listen)
}
func HandleDone(ctx lib.Context) bool {
lib.GetState[*ServerState](ctx).server.Shutdown(ctx)
return false
}
func Serve(ctx lib.Context) {
//r := gin.Default()
r := gin.New()
r.Use(gin.Recovery())
r.Use(Logger(ctx))
r.GET("/*action", func(c *gin.Context) {
c.String(http.StatusOK, "Hello World")
})
srv := &http.Server{
Addr: ":8123",
Handler: r,
}
ctx.WithState(&ServerState{
server: srv,
})
lib.RunCtx(ctx, func(ctx lib.Context) {
srv.ListenAndServe()
})
}