httpserver

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2023 License: Apache-2.0 Imports: 12 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFound         = errors.New("not_found")
	ErrMethodNotAllowed = errors.New("method_not_allowed")
)

Predefined errors

Functions

func FileServer

func FileServer(dir, prefix string) http.HandlerFunc

File server handler

func FileServerMiddleware

func FileServerMiddleware(dir, prefix string) func(http.Handler) http.Handler

File server middleware

func HealthCheckHandler

func HealthCheckHandler() func(w http.ResponseWriter, r *http.Request)

HealthCheckHandler is a simple health check handler

func MethodNotAllowedHandler

func MethodNotAllowedHandler() func(w http.ResponseWriter, r *http.Request)

MethodNotAllowedHandler is a handler for 405 Method Not Allowed

func NotFoundHandler

func NotFoundHandler() func(w http.ResponseWriter, r *http.Request)

NotFoundHandler is a handler for 404 Not Found

Types

type Logger

type Logger interface {
	Infof(format string, v ...interface{})
}

Logger is an interface for logging.

type Option

type Option func(*Server)

Option is a function that configures the Server.

func WithLogger

func WithLogger(logger Logger) Option

WithLogger sets the logger for the server. If no logger is set, no log messages will be printed.

func WithShutdownTimeout

func WithShutdownTimeout(d time.Duration) Option

WithShutdownTimeout sets the timeout for graceful shutdown. If the timeout is exceeded, the server will be shutdown immediately. Default is 5 seconds.

type Server

type Server struct {
	Server *http.Server
	// contains filtered or unexported fields
}

http.Server wrapper to handle graceful shutdown

func NewServer

func NewServer(addr string, handler http.Handler, opts ...Option) *Server

NewServer creates a new Server instance.

func (*Server) Run

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

Run starts the HTTP server and listens for the operating system interrupt signal. If the interrupt signal is received, the HTTP server is gracefully shutdown.

func (*Server) Shutdown

func (s *Server) Shutdown()

Shutdown gracefully shuts down the HTTP server without interrupt signal. It cancels the context and waits for shutdownTimeout.

Jump to

Keyboard shortcuts

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