component

package
v0.1.38 Latest Latest
Warning

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

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

Documentation

Overview

Package component provides HTTP server lifecycle integration for bootstrap.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Component

func Component(handler func() http.Handler, cfg ...httpserver.Config) bootstrap.IComponent

Component wraps the HTTP server as a bootstrap.IComponent.

handler is a lazy provider called during Init() to obtain the http.Handler (typically *gin.Engine). Passing a function rather than the handler directly defers evaluation until Init() time, at which point the Gin component has already been initialised.

cfg is optional; pass at most one - extra values beyond the first are ignored. It supplies every httpserver.Config field except Port, which is always overwritten from the http.server.port env key regardless of what cfg.Port is set to. Omitting cfg reproduces this function's previous behavior (a Config with only Port set).

Typical usage with a bridge 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 }))

A service with a long-lived SSE/streaming endpoint can raise (or disable) the write timeout without giving up bootstrap's lifecycle management (see ComponentWithRunner for the previous, more verbose way to do this):

Add(httpComp.Component(func() http.Handler { return router },
    httpserver.Config{WriteTimeout: httpserver.NoWriteTimeout}))
Example

ExampleComponent shows how Component() is used in a bootstrap registration chain. It reads http.server.port from env during Init(); this package always returns a plain HTTP/HTTPS runner (see httpserver/componentwithlambda for the sibling that also reads app.runmode to switch to a Lambda runner).

Init() starts the HTTP server in a background goroutine and returns immediately. Startup errors (e.g. port already in use) are handled asynchronously via shutdown.Trigger(). Close() gracefully drains in-flight requests.

Typical usage with a bridge 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"

	httpComp "github.com/phcp-tech/common-library-golang/httpserver/component"
)

func main() {
	c := httpComp.Component(func() http.Handler { return http.NewServeMux() })
	fmt.Println(c != nil)
}
Output:
true
Example (WithConfig)

ExampleComponent_withConfig shows the optional Config parameter, for services that need to customize the runner (e.g. raising or disabling the write timeout for a long-lived SSE/streaming endpoint) without giving up bootstrap's lifecycle management.

cfg's Port field is always overwritten from the http.server.port env key regardless of what's set here. Pass at most one Config; see Component's doc comment for the full contract.

package main

import (
	"fmt"
	"net/http"

	"github.com/phcp-tech/common-library-golang/httpserver"
	httpComp "github.com/phcp-tech/common-library-golang/httpserver/component"
)

func main() {
	c := httpComp.Component(
		func() http.Handler { return http.NewServeMux() },
		httpserver.Config{WriteTimeout: httpserver.NoWriteTimeout},
	)
	fmt.Println(c != nil)
}
Output:
true

func ComponentWithRunner added in v0.1.18

func ComponentWithRunner(handler func() http.Handler, factory func() httpserver.IRunner) bootstrap.IComponent

ComponentWithRunner wraps the HTTP server as a bootstrap.IComponent using a custom runner factory. Use this when the caller needs to control how the IRunner is created — for example, to inject a Lambda runner.

Prefer Component for plain HTTP/HTTPS deployments.

Example

ExampleComponentWithRunner shows how to supply a custom runner factory. Use this when the deployment target determines the runner type at runtime — for example, when Lambda support is required (see httpserver/componentwithlambda for the ready-made Lambda-aware component).

httpComp.ComponentWithRunner(
    func() http.Handler { return router },
    func() httpserver.IRunner {
        if isLambda {
            return lambda.NewHttpServer()
        }
        return httpserver.NewHttpServer(httpserver.Config{Port: port})
    },
)
package main

import (
	"fmt"
	"net/http"

	"github.com/phcp-tech/common-library-golang/httpserver"
	httpComp "github.com/phcp-tech/common-library-golang/httpserver/component"
)

func main() {
	c := httpComp.ComponentWithRunner(
		func() http.Handler { return http.NewServeMux() },
		func() httpserver.IRunner {
			return httpserver.NewHttpServer(httpserver.Config{Port: "8080"})
		},
	)
	fmt.Println(c != nil)
}
Output:
true

Types

This section is empty.

Jump to

Keyboard shortcuts

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