Module is used to combine code with similar business functions, including Controller, Router, Template and other related components.
Assuming an API service, module name is github.com/hooto/httpsrv-demo/websrv/v1, it includes User, Role two Controllers. Generally, we create a setup.go file in the root directory of this module, and import the two controllers involved, such as:
// vim setup.go
package v1
import (
"github.com/hooto/httpsrv"
)
func NewModule() *httpsrv.Module {
mod := httpsrv.NewModule()
mod.RegisterController(new(User), new(Role))
return mod
}
// User, Role specific code implementation omitted...In main.go entry file, register above module to the service (Service) and access modules' various methods via URL /api/v1 prefix:
package main
import (
"github.com/hooto/httpsrv"
"github.com/hooto/httpsrv-demo/websrv/v1"
)
func main() {
httpsrv.DefaultService.HandleModule("/api/v1", v1.NewModule())
httpsrv.DefaultService.Config.HttpPort = 8080
httpsrv.DefaultService.Start()
}If current Module is a frontend UI type business module that needs view templates, set template path via following interface:
func NewModule() *httpsrv.Module {
mod := httpsrv.NewModule("ui")
mod.SetTemplatePath("/path/of/module/ui/views") // Local file path of templates, can set 1 ~ N paths for a module
return mod
}If current Module depends on static files, such as js, css, img, etc., set path via following interface
func NewModule() *httpsrv.Module {
mod := httpsrv.NewModule()
mod.RegisterFileServer("/assets", "/path/of/static/files", nil)
return module
}Note:
Route{Path: "assets"} parameter indicates that frontend access URL path is baseuri + "/assets/静态文件相对路径" set by Service.HandleModule(baseuri,...), such as:
httpsrv.DefaultService.HandleModule("/cms", ui.NewModule())Static file relative path is js/main.js, then final URL static file access path is:
# baseuri + route.path + static.file
/cms/assets/js/main.js