golang-lib/web/module.go

30 lines
623 B
Go
Raw Normal View History

2021-06-01 10:51:35 +02:00
package web
import (
"github.com/bdlm/log"
"github.com/gin-contrib/static"
"github.com/gin-gonic/gin"
)
var (
modules []ModuleRegisterFunc
)
2021-06-01 17:49:54 +02:00
// ModuleRegisterFunc format of module which registered to WebService
2021-06-01 10:51:35 +02:00
type ModuleRegisterFunc func(*gin.Engine, *Service)
2021-06-01 17:41:05 +02:00
// ModuleRegister used on start of WebService
2021-06-01 10:51:35 +02:00
func ModuleRegister(f ModuleRegisterFunc) {
modules = append(modules, f)
}
2021-06-01 17:41:05 +02:00
// Bind WebService to gin.Engine
2021-06-01 10:51:35 +02:00
func (ws *Service) Bind(r *gin.Engine) {
for _, f := range modules {
f(r, ws)
}
log.Infof("loaded %d modules", len(modules))
r.Use(static.Serve("/", static.LocalFile(ws.Webroot, false)))
}