server

package
v0.1.9 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2025 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package server provides HTTP server functionality with routing capabilities, middleware support, and session management built on top of standard Go net/http. It offers a simple API for building web applications with clean routing, middleware chains, and error handling.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Error

func Error(w http.ResponseWriter, err error, HTTPStatus int)

Error writes an HTTP error response, logging the error message. Unlike http.Error, this function determines the Content-Type dynamically depending on whether a message is found in the errorMessageMap for the given HTTPStatus. If no error message is registered, it defaults to the error's message content type.

func Errorf

func Errorf(w http.ResponseWriter, HTTPStatus int, message string, args ...any)

Errorf is a convenient function to write an HTTP error response with a formatted message it under the hood uses fmt.Errorf and then calls Error to write the response and log the error.

func InCtxMiddleware

func InCtxMiddleware(key string, value interface{}) func(http.Handler) http.Handler

InCtxMiddleware allows to specify a key/value that should be set on each request context. This is useful for services that could be used by the handlers.

func New

func New(options ...Option) *mux

New creates a new server with the given options and default middleware.

Types

type Middleware

type Middleware func(http.Handler) http.Handler

Middleware is a function that receives a http.Handler and returns a http.Handler that can be used to wrap the original handler with some functionality.

type Option

type Option func(*mux)

Options for the server

func WithAssets

func WithAssets(embedded fs.FS, servingPath string) Option

func WithErrorMessage

func WithErrorMessage(status int, message string) Option

func WithHost

func WithHost(host string) Option

WithHost allows to specify the host to run the server at if not specified it defaults to 0.0.0.0

func WithPort

func WithPort(port string) Option

WithPort allows to specify the port to run the server at when not specified it defaults to 3000

func WithSession

func WithSession(secret, name string, options ...session.Option) Option

WithSession allows to set the session within the application.

type Router

type Router interface {
	// Use allows to specify a middleware that should be executed for all the handlers
	Use(middleware ...Middleware)

	// ResetMiddleware clears the list of middleware on the router by setting the baseMiddleware.
	ResetMiddleware()

	// Handle allows to register a new handler for a specific pattern
	Handle(pattern string, handler http.Handler)

	// HandleFunc allows to register a new handler function for a specific pattern
	HandleFunc(pattern string, handler http.HandlerFunc)

	// Folder allows to serve static files from a directory
	Folder(prefix string, fs fs.FS)

	// Group allows to create a new group of routes with a common prefix
	Group(prefix string, fn func(Router))
}

Router is the interface that wraps the basic methods for a router

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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