server

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2026 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	ListenAddress       string        `env:"HTTP_SERVER_LISTENADDRESS"`
	ReadTimeout         time.Duration `env:"HTTP_SERVER_READTIMEOUT"`
	ReadHeaderTimeout   time.Duration `env:"HTTP_SERVER_READHEADERTIMEOUT"`
	WriteTimeout        time.Duration `env:"HTTP_SERVER_WRITETIMEOUT"`
	IdleTimeout         time.Duration `env:"HTTP_SERVER_IDLETIMEOUT"`
	MaxHeaderBytes      int           `env:"HTTP_SERVER_MAXHEADERBYTES"`
	ShutdownTimeout     time.Duration `env:"HTTP_SERVER_SHUTDOWNTIMEOUT"`
	ServiceName         string        `env:"HTTP_SERVER_SERVICENAME"`
	FileUploadMaxMemory int64         `env:"HTTP_SERVER_FILEUPLOADMAXMEMORY"`
	TLSEnabled          bool          `env:"HTTP_SERVER_TLSENABLED"`
	TLSListenAddress    string        `env:"HTTP_SERVER_TLSLISTENADDRESS"`
	TLSCertFile         string        `env:"HTTP_SERVER_TLSCERTFILE"`
	TLSKeyFile          string        `env:"HTTP_SERVER_TLSKEYFILE"`
}

type DirectoryEntry

type DirectoryEntry struct {
	Name    string    `json:"name"`
	IsDir   bool      `json:"isDir"`
	Size    int64     `json:"size"`
	ModTime time.Time `json:"modTime"`
	URL     string    `json:"url"`
}

type DirectoryIndexingType

type DirectoryIndexingType uint
const (
	DirectoryIndexingTypeNone DirectoryIndexingType = iota // No indexing (0)
	DirectoryIndexingTypeHTML                              // HTML listing
	DirectoryIndexingTypeJSON                              // JSON listing
)

type DirectoryListing

type DirectoryListing struct {
	Path    string           `json:"path"`
	Parent  string           `json:"parent,omitempty"`
	Entries []DirectoryEntry `json:"entries"`
}

type FileUploadHandlerConfig

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

FileUploadHandlerConfig holds the configuration for the file upload handler.

type FileUploadHandlerOption

type FileUploadHandlerOption func(*FileUploadHandlerConfig)

func WithFileUploadHandlerPostprocessor

func WithFileUploadHandlerPostprocessor(
	processor FileUploadPostprocessor,
) FileUploadHandlerOption

WithFileUploadHandlerPostprocessor sets a postprocessor that modifies the response.

func WithFilenamePrependType

func WithFilenamePrependType(
	prependType FilenamePrependType,
) FileUploadHandlerOption

WithFilenamePrependType sets the type of prefix to add to uploaded filenames.

type FileUploadPostprocessor

type FileUploadPostprocessor func(
	response map[string]any,
	request *http.Request,
) (map[string]any, error)

FileUploadPostprocessor defines the function signature for processing file upload responses.

type FilenamePrependType

type FilenamePrependType uint8
const (
	// FilenamePrependTypeNone does not add any prefix to the filename.
	FilenamePrependTypeNone FilenamePrependType = iota
	// FilenamePrependTypeDateTime prepends date and time in Y_M_D_H_I_S format.
	FilenamePrependTypeDateTime
	// FilenamePrependTypeUUID prepends a UUID4 to the filename (default).
	FilenamePrependTypeUUID
)

type Group

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

func NewGroup

func NewGroup(
	mux *http.ServeMux,
	prefix string,
	logger *slog.Logger,
	middlewares ...middleware.Middleware,
) *Group

func (*Group) DELETE

func (g *Group) DELETE(
	pattern string,
	handler http.HandlerFunc,
	middlewares ...middleware.Middleware,
)

func (*Group) GET

func (g *Group) GET(
	pattern string,
	handler http.HandlerFunc,
	middlewares ...middleware.Middleware,
)

func (*Group) Group

func (g *Group) Group(
	subPrefix string,
	middlewares ...middleware.Middleware,
) *Group

func (*Group) Handle

func (g *Group) Handle(
	method, pattern string,
	handler http.Handler,
	middlewares ...middleware.Middleware,
)

Handle registers a handler for the given method and pattern.

func (*Group) HandleFunc

func (g *Group) HandleFunc(
	method, pattern string,
	handler http.HandlerFunc,
	middlewares ...middleware.Middleware,
)

HandleFunc is a convenience method for registering http.HandlerFunc.

func (*Group) OPTIONS

func (g *Group) OPTIONS(
	pattern string,
	handler http.HandlerFunc,
	middlewares ...middleware.Middleware,
)

func (*Group) PATCH

func (g *Group) PATCH(
	pattern string,
	handler http.HandlerFunc,
	middlewares ...middleware.Middleware,
)

func (*Group) POST

func (g *Group) POST(
	pattern string,
	handler http.HandlerFunc,
	middlewares ...middleware.Middleware,
)

func (*Group) PUT

func (g *Group) PUT(
	pattern string,
	handler http.HandlerFunc,
	middlewares ...middleware.Middleware,
)

func (*Group) Use

func (g *Group) Use(middlewares ...middleware.Middleware)

type GroupConfig

type GroupConfig struct {
	Path        string
	Middlewares []middleware.Middleware
	Routes      []RouteConfig
	Groups      []GroupConfig // Nested groups
}

type RouteConfig

type RouteConfig struct {
	Method  string           // http.MethodGet, http.MethodPost, etc.
	Path    string           // "/users/{id}", etc.
	Handler http.HandlerFunc // Handler function
}

type Router

type Router struct {
	// Global middleware settings
	GlobalMiddlewares []middleware.Middleware

	// Multiple static file routes
	Static []StaticRouteConfig

	// Route groups
	Groups []GroupConfig
	// contains filtered or unexported fields
}

type Server

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

func New

func New() (*Server, error)

func NewWithConfig

func NewWithConfig(
	config Config,
) (*Server, error)

func NewWithConfigAndLogger

func NewWithConfigAndLogger(
	config Config,
	logger *slog.Logger,
) (*Server, error)

func NewWithLogger

func NewWithLogger(
	logger *slog.Logger,
) (*Server, error)

func (*Server) EchoHandler

func (s *Server) EchoHandler(
	w http.ResponseWriter,
	r *http.Request,
)

EchoHandler echoes back request information (useful for testing).

func (*Server) FileUploadHandler

func (s *Server) FileUploadHandler(
	uploadsDir string,
	opts ...FileUploadHandlerOption,
) http.HandlerFunc

FileUploadHandler returns a handler for file uploads to the specified directory.

func (*Server) GetHTTPListenerAddr

func (s *Server) GetHTTPListenerAddr() net.Addr

GetHTTPListenerAddr returns the HTTP listener address if the server is running.

func (*Server) GetHTTPSListenerAddr

func (s *Server) GetHTTPSListenerAddr() net.Addr

GetHTTPSListenerAddr returns the HTTPS listener address if the server is running with TLS.

func (*Server) GetListenerAddr

func (s *Server) GetListenerAddr() net.Addr

GetListenerAddr returns the HTTP listener address if the server is running Deprecated: Use GetHTTPListenerAddr() instead.

func (*Server) GetLogger

func (s *Server) GetLogger() *slog.Logger

func (*Server) GetMux

func (s *Server) GetMux() *http.ServeMux

func (*Server) GetRootGroup

func (s *Server) GetRootGroup() *Group

func (*Server) HealthHandler

func (s *Server) HealthHandler(
	w http.ResponseWriter,
	_ *http.Request,
)

HealthHandler provides a basic health check endpoint.

func (*Server) Start

func (s *Server) Start(
	ctx context.Context,
	router *Router,
) error

Start starts the HTTP server and blocks until context is cancelled or Stop is called.

func (*Server) Stop

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

Stop stops the HTTP server gracefully - can only be called once

type StaticRouteConfig

type StaticRouteConfig struct {
	Dir                   string                // Directory to serve files from
	Path                  string                // URL path prefix to serve on
	DirectoryIndexingType DirectoryIndexingType // Directory indexing type
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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