go-scopes/server/server.go

76 lines
1.7 KiB
Go

package server
import (
"net/http"
"strings"
lib "git.sr.ht/~cco/go-scopes"
"git.sr.ht/~cco/go-scopes/core"
"git.sr.ht/~cco/go-scopes/core/message"
"github.com/gin-gonic/gin"
)
type Data = map[string]interface{}
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.New()
r.Use(gin.Recovery())
r.Use(Logger(ctx))
cfg := ctx.Config().(*Cfg)
for _, rcfg := range cfg.routes {
setRoute(ctx, rcfg, r)
}
srv := &http.Server{
Addr: ":8123",
Handler: r,
}
ctx.WithState(&ServerState{
server: srv,
})
lib.RunCtx(ctx, func(ctx lib.Context) {
srv.ListenAndServe()
})
}
func setRoute(ctx lib.Context, rcfg routeCfg, r *gin.Engine) {
switch spec := rcfg.spec.(type) {
case *fsSpec:
r.Static(rcfg.path, spec.docRoot)
case *mhSpec:
r.Match(rcfg.methods, rcfg.path+"/*msg", func(c *gin.Context) {
handleMsg(ctx, spec, c)
})
}
}
// scopes standard request (= message) handler implementation
func handleMsg(ctx lib.Context, cfg *mhSpec, gc *gin.Context) {
head := strings.Split(gc.Param("msg"), "/")[1:]
cctx := ctx.ChildContext(cfg)
msg := message.New(head...).WithSender(cctx)
// if gc.Request.Method == "POST" && data != "": msg.WithPayload(data)
core.HandleMessage(cctx, msg)
code, data := cfg.proc(ctx, msg)
gc.JSON(code, data)
}
func Async(ctx lib.Context, msg lib.Message) (int, Data) {
return http.StatusOK, Data{"status": "OK"}
}