web

package
v0.3.16 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package web contains web server functions and types

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetCtxValue

func GetCtxValue[T any](r *http.Request, key string) (T, bool)

GetCtxValue gets the value from the context and type coerces into type T

func Heartbeat

func Heartbeat(w http.ResponseWriter, _ *http.Request)

Heartbeat is a health check web endpoint

func LoggingMiddleware

func LoggingMiddleware(next http.Handler) http.Handler

LoggingMiddleware prints the request timestamp, method, path, and duration

func RegisterRoutes

func RegisterRoutes[A any](this A, mux *http.ServeMux, verb string, handlers HandlerMap[A], middlewares ...Middleware) int

RegisterRoutes adds handlers to the router

func RegisterTasks

func RegisterTasks[A any](this A, mux *http.ServeMux, verb string, tasks HandlerTask[A], middlewares ...Middleware) int

RegisterTasks adds handlers to the routers from the tasks

func RequestBody

func RequestBody[T any](r *http.Request) (T, error)

RequestBody reads the request body into struct type T

func SendActionResponse

func SendActionResponse(w http.ResponseWriter, rq *my.Request, err error)

SendActionResponse sends a Response to the client indicating whether the action was successful

func SendDataResponse

func SendDataResponse(w http.ResponseWriter, rq *my.Request, data any, err error)

SendDataResponse sends a data Response to the client

func SendError

func SendError(w http.ResponseWriter, rq *my.Request, err error)

SendError sends a Response to the client indicating an error occurred

func SendJSON

func SendJSON[T any](w http.ResponseWriter, statusCode int, data T)

SendJSON sends a JSON response to the client

Types

type Config

type Config struct {
	Base string
	Port int
}

func LoadConfig

func LoadConfig(path string) (*Config, error)

LoadConfig loads the web Config from path

type Handler

type Handler = http.HandlerFunc

type HandlerFn

type HandlerFn[A any] = func(A) Handler

type HandlerMap

type HandlerMap[A any] = map[string]HandlerFn[A]

type HandlerTask

type HandlerTask[A any] = map[string]task[A]

type Middleware

type Middleware = func(next http.Handler) http.Handler

func StackMiddlewares

func StackMiddlewares(middlewares ...Middleware) Middleware

StackMiddlewares combines multiple middlewares into a single middleware

type Response

type Response struct {
	Data any
	OK   bool
	Msg  string
}

type Server

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

func NewServer

func NewServer(cfg *Config) *Server

NewServer creates a new web Server

func (*Server) Run

func (s *Server) Run()

Run starts the web server

func (*Server) SetHandler

func (s *Server) SetHandler(handler http.Handler)

SetHandler sets the server's http handler

Jump to

Keyboard shortcuts

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