47 lines
826 B
Go
47 lines
826 B
Go
package server
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"git.sr.ht/~cco/go-scopes/config"
|
|
"git.sr.ht/~cco/go-scopes/lib"
|
|
"git.sr.ht/~cco/go-scopes/lib/core"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type Cfg struct {
|
|
*config.BaseCfg
|
|
Port string
|
|
}
|
|
|
|
type ServerState struct {
|
|
server *http.Server
|
|
}
|
|
|
|
func Start(ctx lib.Context) {
|
|
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.GET("/", 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()
|
|
})
|
|
}
|