quicky

package module
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidPrefix  = errors.New("invalid prefix")
	ErrInvalidPattern = errors.New("invalid pattern")
)
View Source
var (
	ErrHTMLResponse   = errors.New("HTML response")
	ErrJSONResponse   = errors.New("JSON response")
	ErrRenderResponse = errors.New("Render response")
)
View Source
var (
	ErrDuplicatePattern = errors.New("pattern already registered")
	ErrNilContext       = errors.New("nil context")
	ErrServerRunning    = errors.New("http server already running")
)
View Source
var ResponseErrFunc = func(w http.ResponseWriter, r *http.Request, err error) {
	_ = r
	slog.Error("response", slog.Any("error", err))
	http.Error(w, "internal server error", http.StatusInternalServerError)
}

ResponseErrFunc logs the response error using slog.Error and returns a http.Error with status InternalServerError.

Reassign ResponseErrFunc to use a custom handler with signature: func(http.ResponseWriter, *http.Request, error).

Functions

func Chain

func Chain(handler http.Handler, middleware ...MiddlewareFunc) http.Handler

Chain applies middleware to the handler in the given stack order.

func Coalesce added in v0.2.2

func Coalesce[T comparable](values ...T) T

Coalesce returns the first non-zero value for type T if one exists, otherwise the zero value is returned.

func HTML added in v0.2.0

func HTML(w http.ResponseWriter, r *http.Request, status int, html string)

HTML writes the given html string as HTML to the response.

func IIF added in v0.2.2

func IIF[T any](condition bool, v1, v2 T) T

IIF is an inline if that returns v1 if the condition is true, otherwise v2 is returned.

func IsHTMX added in v0.2.0

func IsHTMX(r *http.Request) bool

IsHTMX returns true if the request originated from HTMX, otherwise false.

func JSON added in v0.2.0

func JSON(w http.ResponseWriter, r *http.Request, status int, data any)

JSON encodes the given data and writes it to the response with the given status.

func Redirect added in v0.2.0

func Redirect(w http.ResponseWriter, r *http.Request, status int, route string)

Redirect is a HTMX aware redirect.

  • On a non-HTMX initiated request, a standard http.Redirect is performed.
  • On a HTMX initiated request, the Hx-Redirect header is set and the status 200 OK is used.

Reason for status override: https://github.com/bigskysoftware/htmx/issues/2052#issuecomment-1979805051

func Render added in v0.2.0

func Render(w http.ResponseWriter, r *http.Request, status int, tpls ...Renderable)

RenderTempl writes any number of Renderables to the response with the given status.

Types

type MiddlewareFunc

type MiddlewareFunc func(next http.Handler) http.Handler

type Mux added in v0.2.2

type Mux interface {
	Handle(pattern string, handler http.Handler, middleware ...MiddlewareFunc)
	HandleFunc(pattern string, handlerFunc http.HandlerFunc, middleware ...MiddlewareFunc)
	ServeHTTP(w http.ResponseWriter, r *http.Request)
}

Mux is a thin wrapper extending the net/http ServeMux with handler based middleware registration.

func NewMux added in v0.2.2

func NewMux(prefix string) Mux

NewMux returns a prefix scoped Mux. If the prefix is an empty string, it is considered to be a root Mux ("/").

type Renderable added in v0.2.2

type Renderable interface {
	Render(ctx context.Context, buf io.Writer) error
}

type Server

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

func New

func New(opts ...ServerOption) *Server

New returns a new Server instance with default configuration.

Defaults:

  • Addr: ":8000"
  • IdleTimeout: 1 minute
  • MaxHeaderBytes: 1MB
  • ReadHeaderTimeout: 5 seconds
  • ShutdownTimeout: 5 seconds

func (*Server) Group

func (s *Server) Group(pattern string) Mux

Group returns a pattern scoped Mux.

func (*Server) Handle added in v0.2.2

func (s *Server) Handle(pattern string, handler http.Handler, middleware ...MiddlewareFunc)

Handle registers the handler for the given pattern.

If the given pattern conflicts with on that is already registered or if the pattern is invalid, Handle panics.

func (*Server) HandleFunc added in v0.2.2

func (s *Server) HandleFunc(pattern string, handlerFunc http.HandlerFunc, middleware ...MiddlewareFunc)

HandleFunc registers the handlerFunc for the given pattern.

If the given pattern conflicts with on that is already registered or if the pattern is invalid, HandleFunc panics.

func (*Server) ListenAndServe

func (s *Server) ListenAndServe(ctx context.Context) error

ListenAndServe listens on the TCP network address configured to handle requests on incoming connections. Accepted connections are configured to enable TCP keep-alives.

Graceful shutdown is handled automatically via context cancellation.

func (*Server) ListenAndServeTLS

func (s *Server) ListenAndServeTLS(ctx context.Context, certFile, keyFile string) error

ListenAndServeTLS acts identically to ListenAndServe, except that it expects HTTPS connections. Additionally, files containing a certificate and matching private key for the server must be provided. If the certificate is signed by a certificate authority, the certFile should be the concatenation of the server's certificate, any intermediates, and the CA's certificate.

Graceful shutdown is handled automatically via context cancellation.

func (*Server) Use added in v0.2.2

func (s *Server) Use(middleware ...MiddlewareFunc)

Use applies middleware globally to all handlers in the given stack order.

type ServerOption added in v0.2.2

type ServerOption func(*Server)

ServerOption provides a functional way to configure the Server.

func WithHttpServer added in v0.2.2

func WithHttpServer(srv *http.Server) ServerOption

WithHttpServer uses the http.Server as a configuration. All fields are respected except for Handler.

func WithPort added in v0.3.0

func WithPort(port uint16) ServerOption

func WithShutdownTimeout added in v0.2.2

func WithShutdownTimeout(d time.Duration) ServerOption

WithShutdownTimeout specifies the allowed time the server has to gracefully shutdown before force closing. Values that are less than or equal to zero are ignored.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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