48 lines
857 B
Go
48 lines
857 B
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) {
|
|
rcfg := routeCfg{
|
|
methods: methods,
|
|
path: path,
|
|
spec: spec,
|
|
}
|
|
if methods == nil {
|
|
rcfg.methods = []string{"GET", "POST"}
|
|
}
|
|
c.routes = append(c.routes, rcfg)
|
|
}
|
|
|
|
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}
|
|
}
|
|
|
|
func MsgHandler(pattern string, rcvs ...string) lib.Config {
|
|
cfg := config.Base("", nil)
|
|
cfg.AddAction(pattern, action.Base(action.Forward, rcvs...))
|
|
return cfg
|
|
}
|