nuts-foundation/nuts-node

View on GitHub
codegen/oapi/echo/echo-register.tmpl

Summary

Maintainability
Test Coverage
// PATCH: This template file was taken from pkg/codegen/templates/echo/echo-register.tmpl

// This is a simple interface which specifies echo.Route addition functions which
// are present on both echo.Echo and echo.Group, since we want to allow using
// either of them for path registration
type EchoRouter interface {
    CONNECT(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
    DELETE(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
    GET(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
    HEAD(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
    OPTIONS(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
    PATCH(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
    POST(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
    PUT(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
    TRACE(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
}

type Preprocessor interface {
    Preprocess(operationID string, context echo.Context)
}

type ErrorStatusCodeResolver interface {
    ResolveStatusCode(err error) int
}

// RegisterHandlers adds each server route to the EchoRouter.
func RegisterHandlers(router EchoRouter, si ServerInterface) {
    RegisterHandlersWithBaseURL(router, si, "")
}

// Registers handlers, and prepends BaseURL to the paths, so that the paths
// can be served under a prefix.
func RegisterHandlersWithBaseURL(router EchoRouter, si ServerInterface, baseURL string) {
{{if .}}
    wrapper := ServerInterfaceWrapper{
        Handler: si,
    }
{{end}}
// PATCH: This alteration wraps the call to the implementation in a function that sets the "OperationId" context parameter,
// so it can be used in error reporting middleware.
{{range .}}router.{{.Method}}(baseURL + "{{.Path | swaggerUriToEchoUri}}", func(context echo.Context) error {
        si.(Preprocessor).Preprocess("{{.OperationId}}", context)
        return wrapper.{{.OperationId}}(context)
    })
{{end}}
}