webkit

package
v0.0.0-...-4e09bf3 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2024 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const ErrorKey = "error"

ErrorKey is the key used to store the error in the context.

Variables

View Source
var DefaultErrorHandler = func(ctx *Context, err error) error {
	return ctx.String(http.StatusInternalServerError, err.Error())
}

DefaultErrorHandler is the default error handler that is called when a route handler returns an error.

View Source
var DefaultNotFoundHandler = func(ctx *Context) error {
	return ctx.String(http.StatusNotFound, "Not Found")
}

DefaultNotFoundHandler is the default handler that is called when no route matches the request.

View Source
var PingHandler = func(ctx *Context) error {
	return ctx.String(http.StatusOK, "PONG")
}

PingHandler is a handler that returns a PONG response for health requests.

Functions

This section is empty.

Types

type Context

type Context struct {
	Response http.ResponseWriter
	Request  *http.Request
}

Context represents the context of the current HTTP request. It holds request and response objects, path, path parameters, data and registered handler.

func NewContext

func NewContext(w http.ResponseWriter, r *http.Request) *Context

NewContext creates a new Context instance.

func (*Context) BindJSON

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

BindJSON decodes the JSON request body and stores the result in the value pointed to by v.

func (*Context) Context

func (c *Context) Context() context.Context

Context returns the original request context.

func (*Context) Get

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

Get retrieves a value from the context using a key.

func (*Context) HTML

func (c *Context) HTML(status int, v string) error

HTML writes an HTML response with the provided status code and data. The header is set to text/html; charset=utf-8 TODO: Unit test this func.

func (*Context) IsTLS

func (c *Context) IsTLS() bool

IsTLS returns true if HTTP connection is TLS otherwise false.

func (*Context) IsWebSocket

func (c *Context) IsWebSocket() bool

IsWebSocket returns true if HTTP connection is WebSocket otherwise false.

func (*Context) JSON

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

JSON writes a JSON response with the provided status code and data. The header is set to application/json.

func (*Context) NoContent

func (c *Context) NoContent(status int) error

NoContent sends a response with no response body and the provided status code.

func (*Context) Param

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

Param retrieves a parameter from the route parameters.

func (*Context) Redirect

func (c *Context) Redirect(code int, url string) error

Redirect performs an HTTP redirect to the specified URL with the given status code.

func (*Context) Render

func (c *Context) Render(component templ.Component) error

Render renders a templated component to the response writer.

func (*Context) RenderWithStatus

func (c *Context) RenderWithStatus(status int, component templ.Component) error

RenderWithStatus renders a templated component to the response writer with the specified status code.

func (*Context) Scheme

func (c *Context) Scheme() string

Scheme returns the HTTP protocol scheme, `http` or `https`.

func (*Context) Set

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

Set sets a value into the context using a key.

func (*Context) String

func (c *Context) String(status int, v string) error

String writes a plain text response with the provided status code and data. The header is set to text/plain.

type Error

type Error struct {
	Code    int
	Message string
}

Error represents an HTTP error.

func NewError

func NewError(code int, message string) *Error

NewError creates a new Error type by HTTP code and a user defined message.

func (*Error) Error

func (e *Error) Error() string

Error implements the Error type and returns the formatted error message.

type ErrorHandler

type ErrorHandler func(*Context, error) error

ErrorHandler is a centralized HTTP error handler.

type Handler

type Handler func(c *Context) error

Handler is a function that handles HTTP requests.

func WrapHandler

func WrapHandler(h http.Handler) Handler

WrapHandler is a helper function for wrapping http.Handler and returns a WebKit middleware.

func WrapHandlerFunc

func WrapHandlerFunc(f http.HandlerFunc) Handler

WrapHandlerFunc is a helper function for wrapping http.HandlerFunc and returns a WebKit middleware.

func WrapMiddleware

func WrapMiddleware(next Handler, mw func(http.Handler) http.Handler) Handler

WrapMiddleware wraps `func(http.Handler) http.Handler` into `webkit.Plug“

type Kit

type Kit struct {
	ErrorHandler    ErrorHandler
	NotFoundHandler Handler
	// contains filtered or unexported fields
}

Kit is the top-level framework instance for handling HTTP routes.

Goroutine safety: Do not mutate WebKit instance fields after server has started. Accessing these fields from handlers/middlewares and changing field values at the same time leads to data-races. Adding new routes after the server has been started is also not safe!

func New

func New() *Kit

New creates a new WebKit instance.

func (*Kit) Add

func (k *Kit) Add(method string, pattern string, handler Handler, plugs ...Plug)

Add registers a new route for an HTTP method and path with matching handler in the router with optional route-level plugs.

func (*Kit) Connect

func (k *Kit) Connect(pattern string, handler Handler, plugs ...Plug)

Connect registers a new CONNECT route for a path with matching handler in the router with optional route-level plugs.

func (*Kit) Delete

func (k *Kit) Delete(pattern string, handler Handler, plugs ...Plug)

Delete registers a new DELETE route for a path with matching handler in the router with optional route-level plugs.

func (*Kit) Get

func (k *Kit) Get(pattern string, handler Handler, plugs ...Plug)

Get registers a new GET route for a path with matching handler in the router with optional route-level plugs.

func (*Kit) Head

func (k *Kit) Head(pattern string, handler Handler, plugs ...Plug)

Head registers a new HEAD route for a path with matching handler in the router with optional route-level plugs.

func (*Kit) Mux

func (k *Kit) Mux() chi.Router

Mux returns the underlying Chi Router.

func (*Kit) NotFound

func (k *Kit) NotFound(handler Handler)

NotFound sets a custom http.HandlerFunc for routing paths that could not be found. The default 404 handler is `http.NotFound`.

func (*Kit) Options

func (k *Kit) Options(pattern string, handler Handler, plugs ...Plug)

Options registers a new OPTIONS route for a path with matching handler in the router with optional route-level plugs.

func (*Kit) Patch

func (k *Kit) Patch(pattern string, handler Handler, plugs ...Plug)

Patch registers a new PATCH route for a path with matching handler in the router with optional route-level plugs.

func (*Kit) Plug

func (k *Kit) Plug(plugs ...Plug)

Plug adds a middleware function to the chain. These are called after the funcs that are passed directly to the route-level handlers.

For example: app.Plug(middleware.Logger)

func (*Kit) Post

func (k *Kit) Post(pattern string, handler Handler, plugs ...Plug)

Post registers a new POST route for a path with matching handler in the router with optional route-level plugs.

func (*Kit) Put

func (k *Kit) Put(pattern string, handler Handler, plugs ...Plug)

Put registers a new PUT route for a path with matching handler in the router with optional route-level plugs.

func (*Kit) ServeHTTP

func (k *Kit) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements the http.Handler interface.

func (*Kit) Start

func (k *Kit) Start(address string) error

Start starts the HTTP server.

func (*Kit) Static

func (k *Kit) Static(pattern string, staticDir string, plugs ...Plug)

Static registers a new route with path prefix to serve static files from the provided root directory.

func (*Kit) Trace

func (k *Kit) Trace(pattern string, handler Handler, plugs ...Plug)

Trace registers a new TRACE route for a path with matching handler in the router with optional route-level plugs.

type Plug

type Plug func(handler Handler) Handler

Plug defines a function to process middleware.

type Schema

type Schema struct {
	Name        string `yaml:"name"`
	Title       string `yaml:"title"`
	Description string `yaml:"description"`
	Version     string `yaml:"version"`
	GitHubURL   string `yaml:"github_url"`
	CMS         struct {
		Enabled bool `yaml:"enabled"`
	} `yaml:"payload"`
}

func ParseSchema

func ParseSchema() (*Schema, error)

Jump to

Keyboard shortcuts

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