golang-lib/web/module.go

26 lines
604 B
Go
Raw Normal View History

2021-06-01 10:51:35 +02:00
package web
import (
"github.com/gin-contrib/static"
"github.com/gin-gonic/gin"
2021-09-29 13:08:43 +02:00
"go.uber.org/zap"
2021-06-01 10:51:35 +02:00
)
2021-07-19 17:59:47 +02:00
// A ModuleRegisterFunc is a module.
2021-06-01 10:51:35 +02:00
type ModuleRegisterFunc func(*gin.Engine, *Service)
2021-07-19 17:59:47 +02:00
// ModuleRegister adds f to ws's list of modules.
func (ws *Service) ModuleRegister(f ModuleRegisterFunc) {
ws.modules = append(ws.modules, f)
2021-06-01 10:51:35 +02:00
}
2021-07-19 17:59:47 +02:00
// Bind executes all of ws's modules with r.
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)
}
2021-09-29 13:08:43 +02:00
ws.log.Info("bind modules", zap.Int("count", len(ws.modules)))
2021-06-01 10:51:35 +02:00
r.Use(static.Serve("/", static.LocalFile(ws.Webroot, false)))
}