golang-lib/web/module.go

26 lines
610 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"
)
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
func (ws *Service) ModuleRegister(f ModuleRegisterFunc) {
ws.modules = append(ws.modules, f)
2021-06-01 10:51:35 +02:00
}
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 ws.modules {
2021-06-01 10:51:35 +02:00
f(r, ws)
}
log.Infof("loaded %d modules", len(ws.modules))
2021-06-01 10:51:35 +02:00
r.Use(static.Serve("/", static.LocalFile(ws.Webroot, false)))
}