71 lines
2.5 KiB
Go
71 lines
2.5 KiB
Go
// Code generated by go-omni api. DO NOT EDIT.
|
|
package handler
|
|
|
|
import (
|
|
"fmt"
|
|
omni "git.yuntuo.hk/k2safe/go-omni"
|
|
"git.yuntuo.hk/k2safe/go-omni/contract"
|
|
|
|
"example.com/managed/internal/svc"
|
|
"git.yuntuo.hk/k2safe/go-omni/config"
|
|
"git.yuntuo.hk/k2safe/go-omni/health"
|
|
)
|
|
|
|
// NewApp is generated HTTP composition. The caller owns deps and its logger.
|
|
func NewApp(deps *svc.ServiceContext) (*omni.App, error) {
|
|
if deps == nil || deps.Lifecycle == nil {
|
|
return nil, fmt.Errorf("service context and lifecycle controller required; see PROJECT.md upgrade instructions")
|
|
}
|
|
cfg := deps.Config.Server
|
|
cfg.LogInstance = deps.Logger
|
|
for _, option := range deps.AppOptions() {
|
|
if option != nil {
|
|
option(&cfg)
|
|
}
|
|
}
|
|
cfg.Request = contract.MergePolicy(cfg.Request, contract.RequestPolicy{UnknownQuery: "", UTF8: "", Charset: "", Body: ""})
|
|
cfg.Response = newResponseTemplate("{\"body\":null,\"errorBody\":null,\"messageField\":\"msg\",\"requestIdField\":\"-\",\"codeType\":\"int32\",\"successCode\":0,\"errorCode\":9999,\"dataMode\":\"always\"}").Config()
|
|
cfg.ResponseEncoder = nil
|
|
if err := cfg.Validate(); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := cfg.Endpoints.ValidateRoutes([]config.EndpointRoute{
|
|
{Method: "get", Path: "/check"},
|
|
{Method: "get", Path: "/other"},
|
|
{Method: "get", Path: "/raw"},
|
|
{Method: "post", Path: "/upload"},
|
|
{Method: "post", Path: "/callback"},
|
|
{Method: "get", Path: "/download"},
|
|
{Method: "get", Path: "/slow"},
|
|
}...); err != nil {
|
|
return nil, err
|
|
}
|
|
if (cfg.Endpoints.Liveness.Enabled || cfg.Endpoints.Readiness.Enabled) && deps.Health == nil {
|
|
return nil, fmt.Errorf("enabled health endpoints require a checker")
|
|
}
|
|
app := omni.New(omni.WithConfig(cfg))
|
|
app.Use(deps.GlobalMiddleware()...)
|
|
if err := RegisterRoutes(app, deps); err != nil {
|
|
_ = app.Close()
|
|
return nil, err
|
|
}
|
|
if cfg.Endpoints.OpenAPI.Enabled {
|
|
app.GET(cfg.Endpoints.OpenAPI.Path, OpenAPI)
|
|
}
|
|
if cfg.Endpoints.Liveness.Enabled {
|
|
var options []health.HandlerOption
|
|
if cfg.Endpoints.Liveness.ResponseMode == "raw" {
|
|
options = append(options, health.WithRawResponse())
|
|
}
|
|
app.GET(cfg.Endpoints.Liveness.Path, deps.Health.LivenessHandler(options...))
|
|
}
|
|
if cfg.Endpoints.Readiness.Enabled {
|
|
options := []health.HandlerOption{health.WithShutdownContext(deps.Lifecycle.Context())}
|
|
if cfg.Endpoints.Readiness.ResponseMode == "raw" {
|
|
options = append(options, health.WithRawResponse())
|
|
}
|
|
app.GET(cfg.Endpoints.Readiness.Path, deps.Health.ReadinessHandler(options...))
|
|
}
|
|
return app, nil
|
|
}
|