component

package
v0.1.37 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 16, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

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), filters ...slogGin.Filter) 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 }))

filters is optional (trailing variadic, existing callers passing only mount are unaffected) and is passed straight through to libGin.InitGin - see that function's doc comment. E.g. Component(mount, slogGin.IgnorePath("/")) silences request logging for a noisy health-check endpoint.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL