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
|
2021-07-19 17:59:08 +02:00
|
|
|
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) {
|
2021-07-19 17:59:08 +02:00
|
|
|
for _, f := range ws.modules {
|
2021-06-01 10:51:35 +02:00
|
|
|
f(r, ws)
|
|
|
|
}
|
|
|
|
|
2021-07-19 17:59:08 +02:00
|
|
|
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)))
|
|
|
|
}
|