gocontroller

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNotFound = errors.New("route not found")
View Source
var ErrValidation = errors.New("validation failed")

Functions

func ParseDTO

func ParseDTO[T any](ctx *Context) (T, error)

ParseDTO decodes JSON body into a typed DTO and runs validation.

func RegisterGeneratedControllerMetadata

func RegisterGeneratedControllerMetadata(controllerPtr any, meta ControllerMetadata)

RegisterGeneratedControllerMetadata is used by generated code.

func SetDefaultValidator

func SetDefaultValidator(v Validator)

SetDefaultValidator sets the process-wide default validator used by Validate and new routers.

func Validate

func Validate(dto any) error

Types

type App

type App struct {
	Router    *Router
	Container *Container
}

func NewApp

func NewApp(root *Module) (*App, error)

func (*App) Handler

func (a *App) Handler() http.Handler

Handler exposes the application router as an http.Handler. This makes it easy to mount in Gin/Echo/Fiber adapter wrappers.

func (*App) Listen

func (a *App) Listen(addr string) error

func (*App) SetValidator

func (a *App) SetValidator(v Validator)

SetValidator overrides the app/router validator for request DTO validation.

func (*App) Validator

func (a *App) Validator() Validator

Validator returns the validator used by the app/router.

type Container

type Container struct {
	// contains filtered or unexported fields
}

func NewContainer

func NewContainer() *Container

func (*Container) MustResolve

func (c *Container) MustResolve(target any)

func (*Container) Provide

func (c *Container) Provide(value any) error

func (*Container) Resolve

func (c *Container) Resolve(target any) error

type Context

type Context struct {
	ResponseWriter http.ResponseWriter
	Request        *http.Request
	Params         map[string]string
	Values         map[string]any
	// contains filtered or unexported fields
}

Context carries request/response helpers for handlers and middleware.

func (*Context) BindJSON

func (c *Context) BindJSON(dto any) error

func (*Context) Get

func (c *Context) Get(key string) (any, bool)

func (*Context) JSON

func (c *Context) JSON(status int, body any) error

func (*Context) Param

func (c *Context) Param(name string) string

func (*Context) Set

func (c *Context) Set(key string, value any)

func (*Context) Text

func (c *Context) Text(status int, value string) error

type Controller

type Controller interface {
	RegisterRoutes(*RouteGroup)
}

type ControllerMetadata

type ControllerMetadata struct {
	Prefix     string
	Middleware []Middleware
	Routes     []RouteMetadata
}

ControllerMetadata is decorator-like route metadata for a controller.

type DecoratedController

type DecoratedController interface {
	ControllerMetadata() ControllerMetadata
}

DecoratedController declares controller metadata for auto route binding.

type GoPlaygroundValidator

type GoPlaygroundValidator struct {
	// contains filtered or unexported fields
}

GoPlaygroundValidator uses github.com/go-playground/validator/v10.

func NewGoPlaygroundValidator

func NewGoPlaygroundValidator() *GoPlaygroundValidator

func (*GoPlaygroundValidator) Validate

func (g *GoPlaygroundValidator) Validate(dto any) error

type HTTPError

type HTTPError struct {
	StatusCode int
	Message    string
}

func NewHTTPError

func NewHTTPError(statusCode int, message string) *HTTPError

func (*HTTPError) Error

func (e *HTTPError) Error() string

type HandlerFunc

type HandlerFunc func(*Context) error

type Middleware

type Middleware func(HandlerFunc) HandlerFunc

type Module

type Module struct {
	Name        string
	Prefix      string
	Providers   []any
	Controllers []any
	Imports     []*Module
	Middleware  []Middleware
}

Module models a NestJS-like module graph.

type RouteGroup

type RouteGroup struct {
	// contains filtered or unexported fields
}

func (*RouteGroup) DELETE

func (g *RouteGroup) DELETE(path string, handler HandlerFunc, middleware ...Middleware)

func (*RouteGroup) GET

func (g *RouteGroup) GET(path string, handler HandlerFunc, middleware ...Middleware)

func (*RouteGroup) Group

func (g *RouteGroup) Group(prefix string, middleware ...Middleware) *RouteGroup

func (*RouteGroup) OPTIONS

func (g *RouteGroup) OPTIONS(path string, handler HandlerFunc, middleware ...Middleware)

func (*RouteGroup) PATCH

func (g *RouteGroup) PATCH(path string, handler HandlerFunc, middleware ...Middleware)

func (*RouteGroup) POST

func (g *RouteGroup) POST(path string, handler HandlerFunc, middleware ...Middleware)

func (*RouteGroup) PUT

func (g *RouteGroup) PUT(path string, handler HandlerFunc, middleware ...Middleware)

type RouteMetadata

type RouteMetadata struct {
	Method     string
	Path       string
	Handler    string
	Middleware []Middleware
}

RouteMetadata describes a single route in a controller.

func DELETE

func DELETE(path, handler string, middleware ...Middleware) RouteMetadata

func GET

func GET(path, handler string, middleware ...Middleware) RouteMetadata

func OPTIONS

func OPTIONS(path, handler string, middleware ...Middleware) RouteMetadata

func PATCH

func PATCH(path, handler string, middleware ...Middleware) RouteMetadata

func POST

func POST(path, handler string, middleware ...Middleware) RouteMetadata

func PUT

func PUT(path, handler string, middleware ...Middleware) RouteMetadata

type Router

type Router struct {
	// contains filtered or unexported fields
}

Router provides method-based routing and middleware composition.

func NewRouter

func NewRouter() *Router

func (*Router) DELETE

func (r *Router) DELETE(path string, handler HandlerFunc, middleware ...Middleware)

func (*Router) GET

func (r *Router) GET(path string, handler HandlerFunc, middleware ...Middleware)

func (*Router) Group

func (r *Router) Group(prefix string, middleware ...Middleware) *RouteGroup

func (*Router) OPTIONS

func (r *Router) OPTIONS(path string, handler HandlerFunc, middleware ...Middleware)

func (*Router) PATCH

func (r *Router) PATCH(path string, handler HandlerFunc, middleware ...Middleware)

func (*Router) POST

func (r *Router) POST(path string, handler HandlerFunc, middleware ...Middleware)

func (*Router) PUT

func (r *Router) PUT(path string, handler HandlerFunc, middleware ...Middleware)

func (*Router) ServeHTTP

func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)

func (*Router) SetValidator

func (r *Router) SetValidator(v Validator)

SetValidator overrides validation engine used by Context.BindJSON for this router.

func (*Router) Use

func (r *Router) Use(middleware ...Middleware)

func (*Router) Validator

func (r *Router) Validator() Validator

Validator returns the validation engine currently attached to this router.

type ValidationError

type ValidationError struct {
	Field   string
	Rule    string
	Message string
}

type ValidationErrors

type ValidationErrors []ValidationError

func (ValidationErrors) Error

func (ve ValidationErrors) Error() string

func (ValidationErrors) Unwrap

func (ve ValidationErrors) Unwrap() error

type Validator

type Validator interface {
	Validate(any) error
}

Validator is a pluggable validation engine.

func DefaultValidator

func DefaultValidator() Validator

DefaultValidator returns the process-wide default validator.

type ValidatorFunc

type ValidatorFunc func(any) error

ValidatorFunc adapts a function into a Validator.

func (ValidatorFunc) Validate

func (f ValidatorFunc) Validate(v any) error

Jump to

Keyboard shortcuts

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