component

package
v0.2.7 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2026 License: Apache-2.0 Imports: 6 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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
Example (Filters)

ExampleComponent_filters shows how to silence request logging for specific endpoints when wiring gin.Component() into a bootstrap chain - e.g. a noisy health-check hit repeatedly by a load balancer, or a metrics-scrape path. filters is a trailing variadic (Component(mount func(*gin.Engine), filters ...slogGin.Filter)), passed straight through to libGin.InitGin - see gin's own ExampleInitGin_filters for the equivalent direct-InitGin usage, including why multiple endpoints need no new plumbing (either slogGin.IgnorePath itself is variadic, or pass several distinct filters). A request is logged unless ANY filter returns false for it - filters combine with OR, not AND.

package main

import (
	"bytes"
	"fmt"
	"log/slog"
	"net/http"
	"net/http/httptest"
	"strings"

	"github.com/gin-gonic/gin"
	ginComp "github.com/phcp-tech/common-library-golang/gin/component"

	slogGin "github.com/samber/slog-gin"
)

func main() {
	// Route logging to a buffer instead of stderr so this example can
	// assert on it. Real callers never do this.
	var buf bytes.Buffer
	slog.SetDefault(slog.New(slog.NewTextHandler(&buf, nil)))

	var router *gin.Engine
	c := ginComp.Component(func(r *gin.Engine) {
		router = r
		for _, path := range []string{"/healthz", "/readyz", "/metrics/cpu", "/api/data"} {
			r.GET(path, func(c *gin.Context) { c.Status(http.StatusOK) })
		}
	},
		slogGin.IgnorePath("/healthz", "/readyz"), // one call, multiple paths
		slogGin.IgnorePathPrefix("/metrics"),      // a second, distinct filter
	)
	if err := c.Init(); err != nil {
		fmt.Println("Init error:", err)
		return
	}

	for _, path := range []string{"/healthz", "/readyz", "/metrics/cpu", "/api/data"} {
		router.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest(http.MethodGet, path, nil))
	}

	fmt.Println(strings.Contains(buf.String(), "/healthz"))
	fmt.Println(strings.Contains(buf.String(), "/readyz"))
	fmt.Println(strings.Contains(buf.String(), "/metrics/cpu"))
	fmt.Println(strings.Contains(buf.String(), "/api/data"))
}
Output:
false
false
false
true

Types

This section is empty.

Jump to

Keyboard shortcuts

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