go-scopes/server/config.go

66 lines
1.1 KiB
Go

package server
import (
lib "git.sr.ht/~cco/go-scopes"
"git.sr.ht/~cco/go-scopes/config"
"git.sr.ht/~cco/go-scopes/core/action"
)
type Cfg struct {
//*config.BaseCfg
Port string
Addr string
routes []routeCfg
}
func (c *Cfg) AddRoute(path string, spec routeSpec, methods ...string) *Cfg {
rcfg := routeCfg{
methods: methods,
path: path,
spec: spec,
}
if methods == nil {
rcfg.methods = []string{"GET", "POST"}
}
c.routes = append(c.routes, rcfg)
return c
}
type routeCfg struct {
methods []string
path string
spec routeSpec
}
type routeSpec interface{}
type fsSpec struct {
docRoot string
}
func FileServer(docRoot string) *fsSpec {
return &fsSpec{docRoot}
}
type actionSpec struct {
*action.BaseSpec
proc msgProc
}
type mhSpec struct {
lib.Config
}
func (spec *mhSpec) AddActionProc(
pattern string, proc msgProc, receivers ...string) *mhSpec {
if proc == nil {
proc = Async
}
spec.AddAction(pattern, &actionSpec{
action.Base(action.Forward, receivers...), proc})
return spec
}
func MsgHandler() *mhSpec {
return &mhSpec{config.Default("")}
}