Documentation
¶
Overview ¶
Package component provides Gin lifecycle integration for bootstrap.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Component ¶
func Component(mount func(*gin.Engine)) bootstrap.IComponent
Component initialises the Gin router and mounts all routes.
CORS origins are read from env automatically during Init():
- prod environment: cors.allow.origins.prod
- other environments: cors.allow.origins.dev
mount is called once during Init() to register all routes. The *gin.Engine created here should be shared with the HTTP server component via a closure variable captured in the caller's main function:
var router *gin.Engine
Add(ginComp.Component(func(r *gin.Engine) {
router = r
adapter.Mount(r)
})).
Add(httpComp.Component(func() http.Handler { return router }))
Example ¶
ExampleComponent shows how gin.Component() is registered in a bootstrap chain. CORS origins are read from env automatically (cors.allow.origins.prod or .dev). The *gin.Engine created inside Init() is shared with the HTTP server component via a closure variable in main.
var router *gin.Engine
Add(ginComp.Component(func(r *gin.Engine) {
router = r
adapter.Mount(r)
})).
Add(httpComp.Component(func() http.Handler { return router }))
package main
import (
"fmt"
"net/http"
"github.com/gin-gonic/gin"
ginComp "github.com/phcp-tech/common-library-golang/gin/component"
)
func main() {
var router *gin.Engine
c := ginComp.Component(func(r *gin.Engine) {
router = r
})
fmt.Println(c != nil)
fmt.Println(c.Name())
_ = router
_ = http.NewServeMux() // suppress unused import
}
Output: true gin
Types ¶
This section is empty.