Documentation
¶
Overview ¶
Package componentwithlambda provides HTTP server lifecycle integration for bootstrap, with AWS Lambda support.
Use this package instead of httpserver/component when the service may be deployed on AWS Lambda. It reads app.runmode from env during Init() and selects the appropriate runner:
- "aws_lambda" → httpserver/lambda.NewHttpServer()
- anything else → httpserver.NewHttpServer() (plain HTTP/HTTPS)
Importing this package pulls in the AWS Lambda SDK. Services that never run on Lambda should use httpserver/component to keep their binary free of unnecessary dependencies.
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 with Lambda support.
handler is a lazy provider called during Init() to obtain the http.Handler (typically *gin.Engine). The runner is selected at Init() time based on the app.runmode env key.
cfg is optional and configures the VM/plain-HTTP runner branch only - pass at most one; extra values beyond the first are ignored. Its Port field is always overwritten from the http.server.port env key regardless of what it's set to, so it only needs to carry the fields httpserver.Config otherwise defaults (WriteTimeout, ReadTimeout, TLS files, ...). The Lambda runner takes no config at all, so cfg has no effect when app.runmode is "aws_lambda". Omitting cfg entirely 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 VM-mode write timeout without losing Lambda support - Lambda mode is unaffected by this option:
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.
The runner is selected at Init() time based on the app.runmode env key:
- "aws_lambda" → AWS Lambda runner (httpserver/lambda)
- anything else → plain HTTP server (httpserver)
Import this package instead of httpserver/component when the service may be deployed on AWS Lambda. It pulls in the AWS Lambda SDK, so services that never run on Lambda should use httpserver/component to avoid the extra dependency.
var router *gin.Engine
bootstrap.New().
Add(envComp.Component("config/app.toml", &configFS)). // 1st — env
Add(logComp.Component()). // 2nd — log
Add(ginComp.Component(func(r *gin.Engine) {
router = r
adapter.Mount(r)
})).
Add(httpComp.Component(func() http.Handler { return router })).
Run()
package main
import (
"fmt"
"net/http"
httpComp "github.com/phcp-tech/common-library-golang/httpserver/componentwithlambda"
)
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 VM-mode runner (e.g. raising or disabling the write timeout for a long-lived SSE/streaming endpoint) without losing Lambda support.
cfg only applies to the VM-mode branch: its Port field is always overwritten from the http.server.port env key regardless of what's set here, and Lambda mode ignores cfg entirely (AWS controls that runtime's lifecycle, not this package). Pass at most one Config; see Component's doc comment for the full contract.
Add(httpComp.Component(func() http.Handler { return router },
httpserver.Config{WriteTimeout: httpserver.NoWriteTimeout})).
package main
import (
"fmt"
"net/http"
"github.com/phcp-tech/common-library-golang/httpserver"
httpComp "github.com/phcp-tech/common-library-golang/httpserver/componentwithlambda"
)
func main() {
c := httpComp.Component(
func() http.Handler { return http.NewServeMux() },
httpserver.Config{WriteTimeout: httpserver.NoWriteTimeout},
)
fmt.Println(c != nil)
}
Output: true
Types ¶
This section is empty.