Documentation ¶
Index ¶
- Constants
- func DIMiddleware() router.MiddlewareFunc
- func ErrorHandler(err error) http.Handler
- func HandleErrors(handlers ...func(ctx context.Context, err error) http.Handler) router.MiddlewareFunc
- func Register(ctx context.Context) error
- func Run(requestHttp *http.Request, requestStruct any) error
- func Validate(request *http.Request, v any) error
- type Converter
- type File
- type FileInfo
- type HTMLError
- type HTMLResponse
- type HTTPError
- type JSONResponse
- type Message
- type MessageOptions
- type RequestHandler
- func (h *RequestHandler[TRequest, TResponse]) Docs(op *spec.OperationProps) *RequestHandler[TRequest, TResponse]
- func (h *RequestHandler[TRequest, TResponse]) Operation(ctx context.Context) (*spec.Operation, error)
- func (h *RequestHandler[TRequest, TResponse]) Run(r *TRequest) (TResponse, error)
- func (h *RequestHandler[TRequest, TResponse]) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (r *RequestHandler[TRequest, TResponse]) Validate(ctx context.Context) error
- type Responder
- type Response
- type StackFrame
- type StackTrace
- type ValidationError
- type Validator
Examples ¶
Constants ¶
Variables ¶
This section is empty.
Functions ¶
func DIMiddleware ¶ added in v0.13.1
func DIMiddleware() router.MiddlewareFunc
func ErrorHandler ¶ added in v0.13.1
func HandleErrors ¶
Types ¶
type File ¶ added in v0.11.2
type File struct {
// contains filtered or unexported fields
}
type FileInfo ¶ added in v0.11.2
type FileInfo struct {
// contains filtered or unexported fields
}
type HTMLResponse ¶
type HTMLResponse struct {
Response
}
func NewHTMLResponse ¶
func NewHTMLResponse(data []byte) *HTMLResponse
type HTTPError ¶
type HTTPError struct {
// contains filtered or unexported fields
}
func NewDefaultHTTPError ¶ added in v0.12.0
func NewHTTPError ¶
type JSONResponse ¶
type JSONResponse struct {
// contains filtered or unexported fields
}
func NewJSONResponse ¶
func NewJSONResponse(data any) *JSONResponse
func (*JSONResponse) AddHeader ¶
func (r *JSONResponse) AddHeader(key, value string) *JSONResponse
func (*JSONResponse) Respond ¶
func (r *JSONResponse) Respond(w http.ResponseWriter, _ *http.Request) error
func (*JSONResponse) SetStatus ¶
func (r *JSONResponse) SetStatus(status int) *JSONResponse
type Message ¶
type Message struct { Array string `json:"array"` String string `json:"string"` Numeric string `json:"numeric"` }
func (*Message) UnmarshalJSON ¶
type MessageOptions ¶
type MessageOptions struct { Attribute string Value any Arguments []string Field reflect.StructField }
type RequestHandler ¶
type RequestHandler[TRequest, TResponse any] struct { // contains filtered or unexported fields }
func Handler ¶
func Handler[TRequest, TResponse any](callback func(r *TRequest) (TResponse, error)) *RequestHandler[TRequest, TResponse]
Example (Error) ¶
package main import ( "fmt" "net/http" "net/http/httptest" "github.com/abibby/salusa/request" ) func main() { type ExampleRequest struct { A int `query:"a" validate:"min:1"` } type ExampleResponse struct { } h := request.Handler(func(r *ExampleRequest) (*ExampleResponse, error) { return &ExampleResponse{}, nil }) rw := httptest.NewRecorder() h.ServeHTTP( rw, httptest.NewRequest("GET", "/?a=-1", http.NoBody), ) fmt.Println(rw.Result().StatusCode) }
Output: 422
Example (Input) ¶
package main import ( "fmt" "net/http" "net/http/httptest" "github.com/abibby/salusa/request" ) func main() { type ExampleRequest struct { A int `query:"a"` B int `query:"b"` } type ExampleResponse struct { Sum int `json:"sum"` } h := request.Handler(func(r *ExampleRequest) (*ExampleResponse, error) { return &ExampleResponse{ Sum: r.A + r.B, }, nil }) rw := httptest.NewRecorder() h.ServeHTTP( rw, httptest.NewRequest("GET", "/?a=10&b=5", http.NoBody), ) fmt.Println(rw.Body) }
Output: { "sum": 15 }
Example (PathParams) ¶
package main import ( "fmt" "net/http" "net/http/httptest" "github.com/abibby/salusa/request" "github.com/gorilla/mux" ) func main() { type ExampleRequest struct { A string `path:"a"` B string `path:"b"` } r := mux.NewRouter() r.Handle("/{a}/{b}", request.Handler(func(r *ExampleRequest) (*ExampleRequest, error) { return r, nil })) rw := httptest.NewRecorder() r.ServeHTTP( rw, httptest.NewRequest("GET", "/path_param_a/path_param_b", http.NoBody), ) fmt.Println(rw.Body) }
Output: { "A": "path_param_a", "B": "path_param_b" }
func (*RequestHandler[TRequest, TResponse]) Docs ¶ added in v0.13.1
func (h *RequestHandler[TRequest, TResponse]) Docs(op *spec.OperationProps) *RequestHandler[TRequest, TResponse]
func (*RequestHandler[TRequest, TResponse]) Run ¶ added in v0.8.0
func (h *RequestHandler[TRequest, TResponse]) Run(r *TRequest) (TResponse, error)
func (*RequestHandler[TRequest, TResponse]) ServeHTTP ¶
func (h *RequestHandler[TRequest, TResponse]) ServeHTTP(w http.ResponseWriter, r *http.Request)
type Response ¶
type Response struct {
// contains filtered or unexported fields
}
func NewResponse ¶
type StackFrame ¶ added in v0.11.2
type StackTrace ¶ added in v0.11.2
type StackTrace struct { GoRoutine string `json:"go_routine"` Stack []*StackFrame `json:"stack"` }
type ValidationError ¶
func (ValidationError) AddError ¶
func (e ValidationError) AddError(key string, message string)
func (ValidationError) Error ¶
func (e ValidationError) Error() string
func (ValidationError) HTMLError ¶ added in v0.8.0
func (e ValidationError) HTMLError() string
func (ValidationError) HasErrors ¶
func (e ValidationError) HasErrors() bool
func (ValidationError) Merge ¶
func (e ValidationError) Merge(vErr ValidationError)
Source Files ¶
Click to show internal directories.
Click to hide internal directories.