Documentation
¶
Overview ¶
Package pureapi provides a unified API for building HTTP applications.
Package pureapi provides a unified API for building HTTP applications.
Example:
package main
import (
"context"
"fmt"
"net/http"
"log"
"github.com/aatuh/pureapi-core"
)
func main() {
// Create a new server
server := pureapi.NewServer()
// Register a simple GET route
server.Get("/hello", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("Hello, World!"))
})
// Register a route with parameters
server.Get("/users/:id", func(w http.ResponseWriter, r *http.Request) {
params := pureapi.RouteParams(r)
userID := params["id"]
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "User ID: %s", userID)
})
// Start the server
log.Println("Server starting on :8080")
if err := http.ListenAndServe(":8080", server.Handler()); err != nil {
log.Fatal(err)
}
}
Index ¶
- func APIErrorFrom(err APIError) *apierror.DefaultAPIError
- func NewAPIError(id string) *apierror.DefaultAPIError
- func NewBuiltinRouter() router.Router
- func NewHandler[T any](ih InputHandler[T], lf HandlerLogicFn[T], eh ErrorHandler, oh OutputHandler) endpoint.Handler[T]
- func QueryMap(r *http.Request) map[string]any
- func RouteParams(r *http.Request) map[string]string
- type APIError
- type ErrorHandler
- type HandlerLogicFn
- type InputHandler
- type Middleware
- type Middlewares
- type OutputHandler
- type Server
- func (s *Server) Delete(path string, fn http.HandlerFunc) endpoint.Endpoint
- func (s *Server) Get(path string, fn http.HandlerFunc) endpoint.Endpoint
- func (s *Server) Handler() http.Handler
- func (s *Server) Patch(path string, fn http.HandlerFunc) endpoint.Endpoint
- func (s *Server) Post(path string, fn http.HandlerFunc) endpoint.Endpoint
- func (s *Server) Put(path string, fn http.HandlerFunc) endpoint.Endpoint
- type ServerOption
- type Stack
- type Wrapper
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func APIErrorFrom ¶
func APIErrorFrom(err APIError) *apierror.DefaultAPIError
APIErrorFrom converts an APIError to its default concrete type.
Parameters:
- err: The API error to convert.
Returns:
- *apierror.DefaultAPIError: The converted API error.
func NewAPIError ¶
func NewAPIError(id string) *apierror.DefaultAPIError
NewAPIError constructs a new API error with an ID.
Parameters:
- id: The error identifier.
Returns:
- *apierror.DefaultAPIError: A new API error instance.
func NewBuiltinRouter ¶
NewBuiltinRouter exposes the tiny built-in router.
Returns:
- router.Router: A new built-in router instance.
func NewHandler ¶
func NewHandler[T any]( ih InputHandler[T], lf HandlerLogicFn[T], eh ErrorHandler, oh OutputHandler, ) endpoint.Handler[T]
NewHandler constructs the default endpoint handler pipeline.
Parameters:
- ih: The input handler for processing request input.
- lf: The handler logic function for business logic.
- eh: The error handler for mapping errors to API responses.
- oh: The output handler for writing responses.
Returns:
- endpoint.Handler[T]: A new handler instance.
Types ¶
type ErrorHandler ¶
type ErrorHandler = endpoint.ErrorHandler
ErrorHandler maps errors to API errors and status codes.
type HandlerLogicFn ¶
HandlerLogicFn performs business logic for an endpoint.
type InputHandler ¶
type InputHandler[T any] interface { endpoint.InputHandler[T] }
InputHandler processes request input into a typed value.
type Middlewares ¶
type Middlewares = endpoint.Middlewares
Middlewares is a collection of middleware.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server is a small facade over server.Handler with route helpers.
func NewServer ¶
func NewServer(opts ...ServerOption) *Server
NewServer constructs a server with optional configuration.
Parameters:
- opts: Optional server configuration options.
Returns:
- *Server: A new Server instance with the specified configuration.
func (*Server) Delete ¶
Delete registers a DELETE route and returns the created endpoint for chaining.
Parameters:
- path: The URL path for the route.
- fn: The handler function for the route.
Returns:
- endpoint.Endpoint: The created endpoint for method chaining.
func (*Server) Get ¶
Get registers a GET route and returns the created endpoint for chaining.
Parameters:
- path: The URL path for the route.
- fn: The handler function for the route.
Returns:
- endpoint.Endpoint: The created endpoint for method chaining.
func (*Server) Handler ¶
Handler returns the underlying http.Handler.
Returns:
- http.Handler: The underlying HTTP handler.
func (*Server) Patch ¶
Patch registers a PATCH route and returns the created endpoint for chaining.
Parameters:
- path: The URL path for the route.
- fn: The handler function for the route.
Returns:
- endpoint.Endpoint: The created endpoint for method chaining.
func (*Server) Post ¶
Post registers a POST route and returns the created endpoint for chaining.
Parameters:
- path: The URL path for the route.
- fn: The handler function for the route.
Returns:
- endpoint.Endpoint: The created endpoint for method chaining.
type ServerOption ¶
type ServerOption = server.HandlerOption
ServerOption configures the underlying HTTP handler.
func WithBodyLimit ¶
func WithBodyLimit(limit int64) ServerOption
WithBodyLimit sets maximum request body size in bytes.
Parameters:
- limit: The maximum request body size in bytes.
Returns:
- ServerOption: A server option function.
func WithCustomNotFound ¶
func WithCustomNotFound(h http.Handler) ServerOption
WithCustomNotFound sets a custom 404 handler.
Parameters:
- h: The custom 404 handler.
Returns:
- ServerOption: A server option function.
func WithEventEmitter ¶
func WithEventEmitter(em event.EventEmitter) ServerOption
WithEventEmitter sets a custom event emitter for the server.
func WithQueryDecoder ¶
func WithQueryDecoder(d querydec.Decoder) ServerOption
WithQueryDecoder sets the query decoder to use.
Parameters:
- d: The query decoder to use.
Returns:
- ServerOption: A server option function.
func WithRouter ¶
func WithRouter(r router.Router) ServerOption
WithRouter sets the router to use.
Parameters:
- r: The router implementation to use.
Returns:
- ServerOption: A server option function.
type Wrapper ¶
Wrapper wraps a middleware with an ID and optional metadata.
func NewWrapperFromHandler ¶
NewWrapperFromHandler constructs a wrapper from an http middleware.
Parameters:
- id: The identifier for the wrapper.
- mw: The HTTP middleware function.
Returns:
- Wrapper: A new middleware wrapper.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package apierror provides structured error handling for HTTP APIs.
|
Package apierror provides structured error handling for HTTP APIs. |
|
Package endpoint provides type-safe HTTP endpoint definitions and handlers.
|
Package endpoint provides type-safe HTTP endpoint definitions and handlers. |
|
Package event provides an event-driven architecture for HTTP applications.
|
Package event provides an event-driven architecture for HTTP applications. |
|
Package querydec provides flexible query parameter decoding for HTTP requests.
|
Package querydec provides flexible query parameter decoding for HTTP requests. |
|
Package router provides pluggable HTTP routing with path parameter support.
|
Package router provides pluggable HTTP routing with path parameter support. |
|
Package server provides HTTP server implementation with production features.
|
Package server provides HTTP server implementation with production features. |