Documentation
¶
Overview ¶
Package logger provides a simple logging implementation to be used in conjunction with Puff.
Package puff provides primitives for implementing a Puff Server
Index ¶
- Variables
- func BadFieldType(k string, got string, expected string) error
- func DefaultJSONLogger() *slog.Logger
- func DefaultLogger() *slog.Logger
- func ExpectedButNotFound(k string) error
- func FieldTypeError(value string, expectedType string) error
- func InvalidJSONError(v string) error
- func NewLogger(c *LoggerConfig) *slog.Logger
- func RandomNanoID() string
- func RandomToken(length int) string
- func ResponseType[T any]() reflect.Type
- func UnexpectedJSONKey(k string) error
- func Unprocessable(w http.ResponseWriter, r *http.Request)
- type AppConfig
- type Callback
- type Components
- type Contact
- type Context
- func (ctx *Context) BadRequest(message string, a ...any)
- func (ctx *Context) ClientIP() (IPAddress string)
- func (ctx *Context) Forbidden(message string, a ...any)
- func (ctx *Context) Get(key string) any
- func (ctx *Context) GetBearerToken() string
- func (ctx *Context) GetBody() ([]byte, error)
- func (ctx *Context) GetCookie(k string) string
- func (ctx *Context) GetFormFile(key string) (multipart.File, *multipart.FileHeader, error)
- func (ctx *Context) GetFormValue(k string) string
- func (ctx *Context) GetQueryParam(k string) string
- func (ctx *Context) GetRequestHeader(k string) string
- func (ctx *Context) GetRequestID() string
- func (ctx *Context) GetResponseHeader(k string) string
- func (ctx *Context) GetStatusCode() int
- func (ctx *Context) InternalServerError(message string, a ...any)
- func (ctx *Context) NotFound(message string, a ...any)
- func (c *Context) SendResponse(res Response)
- func (ctx *Context) Set(key string, value any)
- func (ctx *Context) SetContentType(v string)
- func (ctx *Context) SetCookie(cookie *http.Cookie)
- func (ctx *Context) SetResponseHeader(k, v string)
- func (ctx *Context) SetStatusCode(sc int)
- type Encoding
- type Example
- type ExternalDocumentation
- type File
- type FileResponse
- type GenericResponse
- type HTMLResponse
- type HandlerFunc
- type Header
- type Info
- type JSONResponse
- type License
- type Link
- type LoggerConfig
- type MediaType
- type Middleware
- type OpenAPI
- type OpenAPIResponse
- type Operation
- type Parameter
- type PathItem
- type Paths
- type Property
- type PuffApp
- func (a *PuffApp) AllRoutes() []*Route
- func (a *PuffApp) Close() error
- func (a *PuffApp) Delete(path string, fields any, handleFunc func(*Context)) *Route
- func (a *PuffApp) GenerateDefinitions(paths Paths) map[string]*Schema
- func (a *PuffApp) GenerateOpenAPISpec()
- func (a *PuffApp) GeneratePathsTags() (*Paths, *[]Tag)
- func (a *PuffApp) Get(path string, fields any, handleFunc func(*Context)) *Route
- func (a *PuffApp) IncludeRouter(r *Router)
- func (a *PuffApp) ListenAndServe(listenAddr string) error
- func (a *PuffApp) Patch(path string, fields any, handleFunc func(*Context)) *Route
- func (a *PuffApp) Post(path string, fields any, handleFunc func(*Context)) *Route
- func (a *PuffApp) Put(path string, fields any, handleFunc func(*Context)) *Route
- func (a *PuffApp) Shutdown(ctx context.Context) error
- func (a *PuffApp) Use(m Middleware)
- func (a *PuffApp) WebSocket(path string, fields any, handleFunc func(*Context)) *Route
- type RedirectResponse
- type Reference
- type RequestBodyOrReference
- type Response
- type ResponseDefinition
- type Responses
- type Route
- type Router
- func (r *Router) AllRoutes() []*Route
- func (r *Router) Delete(path string, fields any, handleFunc func(*Context)) *Route
- func (r *Router) Get(path string, fields any, handleFunc func(*Context)) *Route
- func (r *Router) IncludeRouter(rt *Router)
- func (r *Router) Patch(path string, fields any, handleFunc func(*Context)) *Route
- func (r *Router) Post(path string, fields any, handleFunc func(*Context)) *Route
- func (r *Router) Put(path string, fields any, handleFunc func(*Context)) *Route
- func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)
- func (r *Router) String() string
- func (r *Router) Use(m Middleware)
- func (r *Router) WebSocket(path string, fields any, handleFunc func(*Context)) *Route
- type Schema
- type SchemaDefinition
- type SecurityRequirement
- type Server
- type ServerSideEvent
- type ServerVariable
- type SlogHandler
- type StreamingResponse
- type SwaggerUIConfig
- type Tag
Constants ¶
This section is empty.
Variables ¶
var DefaultJSONLoggerConfig = LoggerConfig{ UseJSON: true, Level: slog.LevelDebug, TimeFormat: time.StampNano, Colorize: false, }
var DefaultLoggerConfig = LoggerConfig{ UseJSON: false, Level: slog.LevelInfo, TimeFormat: time.UnixDate, Colorize: true, }
var Schemas = make(SchemaDefinition)
Functions ¶
func DefaultJSONLogger ¶
DefaultLogger returns a slog.Logger which will use the default json logger.
func DefaultLogger ¶
DefaultLogger returns a slog.Logger which will use the default text logger.
func ExpectedButNotFound ¶
func FieldTypeError ¶
func InvalidJSONError ¶
func NewLogger ¶
func NewLogger(c *LoggerConfig) *slog.Logger
NewLogger creates a new *slog.Logger provided the LoggerConfig. Use this function if the default loggers; DefaultLogger and DefaultJSONLogger are not satisfactory.
func RandomNanoID ¶
func RandomNanoID() string
RandomNanoID generates a random NanoID with format LLLL-NNNN. IMPORTANT: THIS FUNCTION IS NOT CRYPTOGRAPHICALLY SECURE. DO NOT USE THIS TO GENERATE TOKENS WITH AUTHORITY (instead see RandomToken).
func RandomToken ¶
RandomToken generates a crytographically secure random base64 token with the provided length.
func ResponseType ¶
func UnexpectedJSONKey ¶
func Unprocessable ¶
func Unprocessable(w http.ResponseWriter, r *http.Request)
Types ¶
type AppConfig ¶
type AppConfig struct { // Name is the application name Name string // Version is the application version. Version string // DocsURL is the Router prefix for Swagger documentation. Can be "" to disable Swagger documentation. DocsURL string // TLSPublicCertFile specifies the file for the TLS certificate (usually .pem or .crt). TLSPublicCertFile string // TLSPrivateKeyFile specifies the file for the TLS private key (usually .key). TLSPrivateKeyFile string // OpenAPI configuration. Gives users access to the OpenAPI spec generated. Can be manipulated by the user. OpenAPI *OpenAPI // SwaggerUIConfig is the UI specific configuration. SwaggerUIConfig *SwaggerUIConfig // LoggerConfig is the application logger config. LoggerConfig *LoggerConfig // DisableOpenAPIGeneration controls whether an OpenAPI schema will be generated. DisableOpenAPIGeneration bool }
AppConfig defines PuffApp parameters.
type Components ¶
type Components struct { Schemas *SchemaDefinition `json:"schemas,omitempty"` Responses map[string]any `json:"responses,omitempty"` Parameters map[string]any `json:"parameters,omitempty"` Examples map[string]any `json:"examples,omitempty"` RequestBodies map[string]any `json:"requestBodies,omitempty"` Headers map[string]any `json:"headers,omitempty"` SecuritySchemes map[string]any `json:"securitySchemes,omitempty"` Links map[string]any `json:"links,omitempty"` Callbacks map[string]any `json:"callbacks,omitempty"` PathItems map[string]any `json:"pathItems,omitempty"` }
Components struct holds reusable objects for different aspects of the OAS.
func NewComponents ¶
func NewComponents(a *PuffApp) *Components
type Contact ¶
type Contact struct { Name string `json:"name,omitempty"` URL string `json:"url,omitempty"` Email string `json:"email,omitempty"` }
Contact struct contains contact information for the API.
type Context ¶
type Context struct { // Request is the underlying *http.Request object. Request *http.Request // ResponseWriter is the underlying http.ResponseWriter object. ResponseWriter http.ResponseWriter // WebSocket represents WebSocket connection and its related context, connection, and events. // WebSocket will be nil if the route does not use websockets. WebSocket *websocket.Conn // LoggerConfig LoggerConfig LoggerConfig // contains filtered or unexported fields }
Context provides functionality for the route.
func NewContext ¶
func (*Context) BadRequest ¶
BadRequest returns a json response with status code 400 a key error and a value of the formatted string from message and the arguments following.
func (*Context) Forbidden ¶
Forbidden returns a json response with status code 403 a key error and a value of the formatted string from message and the arguments following.
func (*Context) Get ¶
Get gets a value from Context with the key passed in. It returns nil if the value is not found.
func (*Context) GetBearerToken ¶
GetBearerToken gets the Bearer token if it exists. This will work if the request contains an Authorization header that has this syntax: Bearer this_token_here.
func (*Context) GetCookie ¶
GetCookie retrives a cookie from the context with key "k". If not found, it will return an empty string.
func (*Context) GetFormFile ¶
GetFormFile returns the multipart file and the multipart file header associated with the key. It will only provide the first file associated with that form key. It may return an error that is not nil.
func (*Context) GetFormValue ¶
GetFormValue retrives the value of a form key named k. If not found, it will return an empty string.
func (*Context) GetQueryParam ¶
GetQueryParam retrives the value of a query param from k. If not found, it will return an empty string.
func (*Context) GetRequestHeader ¶
GetRequestHeader gets the value of a request header with key k. It returns an empty string if not found.
func (*Context) GetRequestID ¶
GetRequestID gets the X-Request-ID if set (empty string if not set). puff/middleware provides a tracing middleware the sets X-Request-ID.
func (*Context) GetResponseHeader ¶
GetResponseHeader gets the value of a response header with key k. It returns an empty string if not found.
func (*Context) GetStatusCode ¶
GetStatusCode returns the status code. If response not written, returns default 0.
func (*Context) InternalServerError ¶
InternalServerError returns a json response with status code 500 with a key error and a value of the formatted string from message and the arguments following.
func (*Context) NotFound ¶
NotFound returns a json response with status code 404 with a key error and a value of the formatted string from message and the arguments following.
func (*Context) SendResponse ¶
SendResponse sends res back to the client. Any errors at this point will be logged and sending a response will fail.
func (*Context) SetContentType ¶
SetContentType sets the content type of the response.
func (*Context) SetCookie ¶
SetCookie writes a new cookie to the request with key "k" and value "v". Invalid cookies will be silently dropped. Invalid characters will also be silently dropped. Ex. SetCookie with value ""HELLO WORLD"". The quotation marks are invalid characters, therefore the final cookie will be "HELLO WORLD" instead.
func (*Context) SetResponseHeader ¶
SetResponseHeader sets the value of the response header k to v.
func (*Context) SetStatusCode ¶
SetStatusCode sets the status code of the response.
type ExternalDocumentation ¶
type ExternalDocumentation struct { Description string `json:"description"` URL string `json:"url"` }
ExternalDocumentation struct provides external documentation for the API.
type FileResponse ¶
FileResponse represents a response that sends a file.
func (FileResponse) GetContentType ¶
func (f FileResponse) GetContentType() string
func (FileResponse) GetStatusCode ¶
func (f FileResponse) GetStatusCode() int
GetStatusCode returns the status code of the file response.
func (*FileResponse) Handler ¶
func (f *FileResponse) Handler() func(*Context)
Handler returns a handler function for serving the file response.
func (FileResponse) WriteContent ¶
func (f FileResponse) WriteContent(c *Context) error
WriteContent serves the file from the provided path.
type GenericResponse ¶
GenericResponse represents a response with plain text content.
func (GenericResponse) GetContentType ¶
func (g GenericResponse) GetContentType() string
func (GenericResponse) GetStatusCode ¶
func (g GenericResponse) GetStatusCode() int
GetStatusCode returns the status code of the generic response.
func (GenericResponse) WriteContent ¶
func (g GenericResponse) WriteContent(c *Context) error
GetContent returns the content of the generic response.
type HTMLResponse ¶
type HTMLResponse struct { StatusCode int // Content to render if TemplateFile is not used. Content string // TemplateFile is the path to the template file to use. TemplateFile string // Template is the inline template string. (optional) Template string // Data to pass to the template. Data any }
HTMLResponse represents a response with HTML content. It supports both file-based templates and inline string templates.
func (HTMLResponse) GetContentType ¶
func (h HTMLResponse) GetContentType() string
func (HTMLResponse) GetStatusCode ¶
func (h HTMLResponse) GetStatusCode() int
GetStatusCode returns the status code of the HTML response.
func (HTMLResponse) WriteContent ¶
func (h HTMLResponse) WriteContent(c *Context) error
WriteContent writes the HTML content to the response. It checks whether to use an inline template or a template file.
type HandlerFunc ¶
type HandlerFunc func(*Context)
type Info ¶
type Info struct { Title string `json:"title"` Summary string `json:"summary"` // Description is an html string that describes the API service. Do *NOT* include <Doctype> or <html> tags. Description string `json:"description"` TermsOfService string `json:"termsOfService"` Contact *Contact `json:"contact"` License *License `json:"license"` Version string `json:"version"` }
Info struct provides metadata about the API.
type JSONResponse ¶
JSONResponse represents a response with JSON content.
func (JSONResponse) GetContentType ¶
func (j JSONResponse) GetContentType() string
func (JSONResponse) GetStatusCode ¶
func (j JSONResponse) GetStatusCode() int
GetStatusCode returns the status code of the JSON response.
func (JSONResponse) WriteContent ¶
func (j JSONResponse) WriteContent(c *Context) error
GetContent returns the content of the JSON response.
type License ¶
type License struct { Name string `json:"name"` Identifier string `json:"identifier,omitempty"` Url string `json:"url,omitempty"` }
License struct contains license information for the API.
type Link ¶
type Link struct { OperationRef string `json:"operationRef,omitempty"` OperationID string `json:"operationId,omitempty"` Parameters any `json:"parameters,omitempty"` RequestBody any `json:"requestBody,omitempty"` Description string `json:"description,omitempty"` Server Server `json:"server,omitempty"` }
type LoggerConfig ¶
type LoggerConfig struct { // UseJSON will enable/disable JSON mode for the logger. UseJSON bool // Indent will control whether to use MarshalIndent or Marshal for logging. Indent bool // Level is the logger level. Logger will only write to stdout if log record is above or equivalent to Level. Level slog.Level // TimeFormat is the textual representation of the timestamp. It is passed as an argument to time.Format() TimeFormat string // AddSource is equivalent to slog.HandlerOptions.AddSource AddSource bool // Colorize enables or disables pretty logging dependant on LogLevel. Colorize bool }
LoggerConfig is used to dictate logger behavior.
type MediaType ¶
type MediaType struct { Schema *Schema `json:"schema"` Example any `json:"example,omitempty"` Examples map[string]any `json:"examples,omitempty"` }
MediaType struct describes a media type object in OpenAPI.
type Middleware ¶
type Middleware func(next HandlerFunc) HandlerFunc
type OpenAPI ¶
type OpenAPI struct { SpecVersion string `json:"openapi"` Info *Info `json:"info"` JSONSchemaDialect string `json:"jsonSchemaDialect"` Servers *[]Server `json:"servers"` Paths *Paths `json:"paths"` Webhooks map[string]any `json:"webhooks"` Components *Components `json:"components"` Security *[]SecurityRequirement `json:"security"` Tags *[]Tag `json:"tags"` ExternalDocs *ExternalDocumentation `json:"externalDocs"` // contains filtered or unexported fields }
OpenAPI struct represents the root of the OpenAPI document.
func NewOpenAPI ¶
type OpenAPIResponse ¶
type OpenAPIResponse struct { Description string `json:"description"` Headers map[string]Header `json:"headers,omitempty"` Content map[string]MediaType `json:"content,omitempty"` Links map[string]Link `json:"links,omitempty"` }
OpenAPIResponse struct describes possible responses in OpenAPI.
type Operation ¶
type Operation struct { Tags []string `json:"tags"` Summary string `json:"summary"` Description string `json:"description"` ExternalDocs ExternalDocumentation `json:"externalDocs"` OperationID string `json:"operationId"` Parameters []Parameter `json:"parameters"` RequestBody *RequestBodyOrReference `json:"requestBody,omitempty"` Responses map[string]OpenAPIResponse `json:"responses"` Callbacks map[string]Callback `json:"callbacks"` Deprecated bool `json:"deprecated"` Security *[]SecurityRequirement `json:"security,omitempty"` Servers *[]Server `json:"servers,omitempty"` }
Operation struct describes an operation in a PathItem.
type Parameter ¶
type Parameter struct { Name string `json:"name"` In string `json:"in"` Description string `json:"description"` Required bool `json:"required"` Type string `json:"type"` Deprecated bool `json:"deprecated"` AllowEmptyValue bool `json:"allowEmptyValue"` Style string `json:"style"` Explode bool `json:"explode"` AllowReserved bool `json:"allowReserved"` Schema *Schema `json:"schema"` }
Parameter struct describes a parameter in OpenAPI.
type PathItem ¶
type PathItem struct { Ref string `json:"$ref"` Summary string `json:"summary"` Description string `json:"description"` Get *Operation `json:"get,omitempty"` Put *Operation `json:"put,omitempty"` Post *Operation `json:"post,omitempty"` Delete *Operation `json:"delete,omitempty"` Options *Operation `json:"options,omitempty"` Head *Operation `json:"head,omitempty"` Patch *Operation `json:"patch,omitempty"` Trace *Operation `json:"trace,omitempty"` Servers *[]Server `json:"servers,omitempty"` Parameters *[]Parameter `json:"parameters,omitempty"` }
PathItem struct describes operations available on a single path.
type Property ¶
type Property struct { Type string `json:"type"` Format string `json:"format"` Example any `json:"example"` }
Property defines a property in the OpenAPI spec that defines information about a property (parameters, definitions, etc).
type PuffApp ¶
type PuffApp struct { // Config is the underlying application configuration. Config *AppConfig // RootRouter is the application's default router. RootRouter *Router // Server is the http.Server that will be used to serve requests. Server *http.Server }
func DefaultApp ¶
func (*PuffApp) AllRoutes ¶
AllRoutes returns all routes registered in the PuffApp, including those in sub-routers. This function provides an aggregated view of all routes in the application.
func (*PuffApp) Delete ¶
Delete registers an HTTP DELETE route in the PuffApp's root router.
Parameters: - path: The URL path of the route. - fields: Optional fields associated with the route. - handleFunc: The handler function that will be executed when the route is accessed.
func (*PuffApp) GenerateDefinitions ¶
GenerateDefinitions is a helper function that takes a list of Paths and generates the OpenAPI schema for each path.
func (*PuffApp) GenerateOpenAPISpec ¶
func (a *PuffApp) GenerateOpenAPISpec()
GenerateOpenAPISpec is responsible for taking the PuffApp configuration and turning it into an OpenAPI json.
func (*PuffApp) GeneratePathsTags ¶
GeneratePathsTags is a helper function to auto-define OpenAPI tags and paths if you would like to customize OpenAPI schema. Returns (paths, tags) to populate the 'Paths' and 'Tags' attribute of OpenAPI
func (*PuffApp) Get ¶
Get registers an HTTP GET route in the PuffApp's root router.
Parameters: - path: The URL path of the route. - fields: Optional fields associated with the route. - handleFunc: The handler function that will be executed when the route is accessed.
func (*PuffApp) IncludeRouter ¶
Add a Router to the main app. Under the hood attaches the router to the App's RootRouter
func (*PuffApp) ListenAndServe ¶
ListenAndServe starts the PuffApp server on the specified address. Before starting, it patches all routes, adds OpenAPI documentation routes (if available), and sets up logging.
If TLS certificates are provided (TLSPublicCertFile and TLSPrivateKeyFile), the server starts with TLS enabled; otherwise, it runs a standard HTTP server.
Parameters: - listenAddr: The address the server will listen on (e.g., ":8080").
func (*PuffApp) Patch ¶
Patch registers an HTTP PATCH route in the PuffApp's root router.
Parameters: - path: The URL path of the route. - fields: Optional fields associated with the route. - handleFunc: The handler function that will be executed when the route is accessed.
func (*PuffApp) Post ¶
Post registers an HTTP POST route in the PuffApp's root router.
Parameters: - path: The URL path of the route. - fields: Optional fields associated with the route. - handleFunc: The handler function that will be executed when the route is accessed.
func (*PuffApp) Put ¶
Put registers an HTTP PUT route in the PuffApp's root router.
Parameters: - path: The URL path of the route. - fields: Optional fields associated with the route. - handleFunc: The handler function that will be executed when the route is accessed.
func (*PuffApp) Shutdown ¶
Shutdown calls shutdown on the underlying server with a non-nil empty context.
func (*PuffApp) Use ¶
func (a *PuffApp) Use(m Middleware)
Use registers a middleware function to be used by the root router of the PuffApp. The middleware will be appended to the list of middlewares in the root router.
Parameters: - m: Middleware function to be added.
func (*PuffApp) WebSocket ¶
WebSocket registers a WebSocket route in the PuffApp's root router. This route allows the server to handle WebSocket connections at the specified path.
Parameters: - path: The URL path of the WebSocket route. - fields: Optional fields associated with the route. - handleFunc: The handler function to handle WebSocket connections.
type RedirectResponse ¶
type RedirectResponse struct { // StatusCode provides the 3xx status code of the redirect response. Default: 308. StatusCode int // To provides the URL to redirect the client to. To string }
RedirectResponse represents a response that sends a redirect to the client.
func (RedirectResponse) GetContentType ¶
func (r RedirectResponse) GetContentType() string
GetContentType returns the content type for the redirect response. It will always return an empty string since there is no body to describe in content typc.ResponseWriter.
func (RedirectResponse) GetStatusCode ¶
func (r RedirectResponse) GetStatusCode() int
GetStatusCode returns the status code for the redirect response. If the status code is not provided, or not valid for a redirect, it will default to 308.
func (RedirectResponse) WriteContent ¶
func (r RedirectResponse) WriteContent(c *Context) error
WriteContent writes the header Location to redirect the client to.
type RequestBodyOrReference ¶
type RequestBodyOrReference struct { Reference string `json:"$ref,omitempty"` Description string `json:"description,omitempty"` Content map[string]MediaType `json:"content,omitempty"` Required bool `json:"required,omitempty"` }
RequestBodyOrReference is a union type representing either a Request Body Object or a Reference Object.
type Response ¶
type Response interface { GetStatusCode() int GetContentType() string WriteContent(*Context) error }
Response is an interface that all response types should implement.
type ResponseDefinition ¶
ResponseDefinition represents a definition of a response for a specific HTTP status code. It is used to map an HTTP status code to the corresponding response type for a route. Puff uses this to automatically generate Swagger documentation.
Fields:
- StatusCode: The HTTP status code associated with this response (e.g., http.StatusOK for success,http.StatusNotFound for not found).
- ResponseType: The Go type that defines the response body (e.g., a struct). This type is used to generate the corresponding Swagger schema. The type should not be an instance; just the a function returning reflect.Type (e.g., `puff.ResponseType[Pizza]`).
func DefineResponse ¶
func DefineResponse(statusCode int, ResponseType func() reflect.Type) ResponseDefinition
DefineResponse creates a ResponseDefinition mapping an HTTP status code to the corresponding response type for a route.
Example:
app.Get("/pizza", handler).WithResponses( puff.DefineResponse(http.StatusOK, PizzaResponse), puff.DefineResponse(http.StatusNotFound, ErrorResponse), )
Parameters:
- statusCode: The HTTP status code that this response corresponds to.
- ResponseType: The Go type that represents the response body. This should be the type (not an instance) of the struct that defines the response schema.
Returns: - A ResponseDefinition that maps the provided status code to the response type.
type Responses ¶
Responses type maps together the HTTPStatusCode with a function returning the reflect.Type
type Route ¶
type Route struct { Description string WebSocket bool Protocol string Path string Handler func(*Context) Fields any // Router points to the router the route belongs to. Will always be the closest router in the tree. Router *Router // Responses are the schemas associated with a specific route. Have preference over parent router defined routes. // Preferably set Responses using the WithResponse/WithResponses method on Route. Responses Responses // contains filtered or unexported fields }
func (*Route) GenerateResponses ¶
func (r *Route) GenerateResponses()
GenerateResponses is responsible for generating the 'responses' attribute in the OpenAPI schema. Since responses can be specified at multiple levels, responses at the route level will be given the most specificity.
func (*Route) GetFullPath ¶
func (*Route) WithResponse ¶
WithResponse registers a single response type for a specific HTTP status code for the route. This method is used exclusively for generating Swagger documentation, allowing users to specify the response type that will be represented in the Swagger API documentation when this status code is encountered.
Example usage:
app.Get("/pizza", func(c puff.Context) { c.SendResponse(puff.JSONResponse{http.StatusOK, PizzaResponse{Name: "Margherita", Price: 10, Size: "Medium"}}) }).WithResponse(http.StatusOK, puff.ResponseType[PizzaResponse])
Parameters:
- statusCode: The HTTP status code that this response corresponds to.
- ResponseType: The Go type that represents the structure of the response body. This should be the type (not an instance) of the struct that defines the response schema.
Returns: - The updated Route object to allow method chaining.
func (*Route) WithResponses ¶
func (r *Route) WithResponses(responses ...ResponseDefinition) *Route
WithResponses registers multiple response types for different HTTP status codes for the route. This method is used exclusively for generating Swagger documentation, allowing users to define various response types based on the possible outcomes of the route's execution, as represented in the Swagger API documentation.
Example usage:
app.Get("/pizza", func(c puff.Context) { ~ logic here if found { c.SendResponse(puff.JSONResponse{http.StatusOK, PizzaResponse{Name: "Margherita", Price: 10, Size: "Medium"}}) } else { c.SendResponse(puff.JSONResponse{http.StatusNotFound, ErrorResponse{Message: "Not Found"}}) } }).WithResponses( puff.DefineResponse(http.StatusOK, puff.ResponseType[PizzaResponse]), puff.DefineResponse(http.StatusNotFound, puff.ResponseType[ErrorResponse]), )
Parameters:
- responses: A variadic list of ResponseDefinition objects that define the mapping between HTTP status codes and their corresponding response types. Each ResponseDefinition includes a status code and a type representing the response body structure.
Returns: - The updated Route object to allow method chaining.
type Router ¶
type Router struct { Name string Prefix string //(optional) prefix, all Routes underneath will have paths that start with the prefix automatically Routers []*Router Routes []*Route Middlewares []*Middleware Tag string Description string // Responses is a map of status code to puff.Response. Possible Responses for routes can be set at the Router (root as well), // and Route level, however responses directly set on the route will have the highest specificity. Responses Responses // contains filtered or unexported fields }
Router defines a group of routes that share the same prefix and middlewares.
func (*Router) AllRoutes ¶
AllRoutes returns all routes attached to a router as well as routes attached to the subrouters For just the routes attached to a router, use `Routes` attribute on Router
func (*Router) IncludeRouter ¶
func (*Router) Use ¶
func (r *Router) Use(m Middleware)
Use adds a middleware to the router's list of middlewares. Middleware functions can be used to intercept requests and responses, allowing for functionality such as logging, authentication, and error handling to be applied to all routes managed by this router.
Example usage:
router := puff.NewRouter() router.Use(myMiddleware) router.Get("/endpoint", myHandler)
Parameters: - m: A Middleware function that will be applied to all routes in this router. TODO: dont know if below is actually accurate. cant think Note: Middleware functions are executed in the order they are added. If multiple middlewares are registered, they will be executed sequentially for each request handled by the router.
type Schema ¶
type Schema struct { // Define your schema fields based on your specific requirements // Example fields could include type, format, properties, etc. // This can be expanded based on the needs of your application. Type string `json:"type,omitempty"` Format string `json:"format,omitempty"` Minimum string `json:"minimum,omitempty"` Items *Schema `json:"items,omitempty"` Ref string `json:"$ref,omitempty"` Properties map[string]*Schema `json:"properties,omitempty"` AdditionalProperties *Schema `json:"additionalProperties,omitempty"` Required []string `json:"required,omitempty"` Examples []any `json:"examples,omitempty"` }
Schema struct represents a schema object in OpenAPI.
type SchemaDefinition ¶
type SecurityRequirement ¶
type Server ¶
type Server struct { URL string `json:"url"` Description string `json:"description"` Variables map[string]ServerVariable `json:"variables"` }
Server struct represents a server object in OpenAPI.
type ServerVariable ¶
type SlogHandler ¶
SlogHandler is puff's implementation of structured logging. It wraps golang's slog package.
func NewSlogHandler ¶
func NewSlogHandler(config LoggerConfig) *SlogHandler
NewSlogHandler returns a new puff.SlogHandler given a LoggerConfig and slog.Handler
func (*SlogHandler) SetLevel ¶
func (h *SlogHandler) SetLevel(level slog.Level)
SetLevel changes the puff.SlogHandler level to the one specified.
type StreamingResponse ¶
type StreamingResponse struct { StatusCode int // StreamHandler is a function that takes in a pointer to a channel. // The channel should be written to with a ServerSideEvent to write // to the response. It should be closed once done writing. StreamHandler func(*chan ServerSideEvent) }
StreamingResponse represents a response that streams content.
func (StreamingResponse) GetContentType ¶
func (s StreamingResponse) GetContentType() string
func (StreamingResponse) GetStatusCode ¶
func (s StreamingResponse) GetStatusCode() int
GetStatusCode returns the status code of the streaming response.
func (StreamingResponse) Handler ¶
func (s StreamingResponse) Handler() func(*Context)
func (StreamingResponse) WriteContent ¶
func (s StreamingResponse) WriteContent(c *Context) error
GetContent returns the content of the streaming response.
type SwaggerUIConfig ¶
type SwaggerUIConfig struct { // Title of the Swagger Page Title string // URL of OpenAPI JSON URL string // One of the 7 themes supported by Swagger, e.g 'nord'. Theme string // Filter controls whether to display a tag-based filter on the OpenAPI UI Filter bool // RequestDuration controls whether to display the request duration after firing a request. RequestDuration bool // FaviconURL is the location of favicon image to display FaviconURL string }
SwaggerUIConfig is the subset of the SwaggerUI configurables that Puff supports. To learn more, please read [SwaggerDocs](https://swagger.io/docs/open-source-tools/swagger-ui/usage/configuration/).
type Tag ¶
type Tag struct { Name string `json:"name"` Description string `json:"description"` ExternalDocs ExternalDocumentation `json:"externalDocs,omitempty"` }
Tag struct represents a tag used by the OpenAPI document.
Source Files
¶
Directories
¶
Path | Synopsis |
---|---|
Package middleware provides middlewares for handling common web application requirements.
|
Package middleware provides middlewares for handling common web application requirements. |