httpserver

package module
v0.5.3 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2024 License: MIT Imports: 16 Imported by: 1

README

A Simple Golang Server with Graceful shutdown

Note This library doesn't handle routing

Usage

Check out example directory!

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultLoggerProduction = slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
		Level: slog.LevelInfo,
	}))
	DefaultLoggerDevelopment = slog.New(tint.NewHandler(os.Stdout, &tint.Options{
		Level:      slog.LevelDebug,
		TimeFormat: time.TimeOnly,
	}))
)

Functions

func T

func T()

Types

type Mode

type Mode string
const (
	ModeProduction  Mode = "production"
	ModeDevelopment Mode = "development"
)

type Option

type Option func(*Server)

func DevelopmentMode

func DevelopmentMode() Option

DevelopmentMode listens for file changes and restarts accordingly (as well as running go generate) and the default logger is pretty printer.

func ListenOn

func ListenOn(listen string) Option

ListenOn the host and the port (e.g: localhost:3000 or :8080) The default value depends on the server mode: In Development = localhost:8080 (http only) In Production = 0.0.0.0:80 & 0.0.0.0:443 (if TLS is enabled)

func ProductionMode

func ProductionMode() Option

ProductionMode don't listen for file changes and the default logger is a JSON logger.

func WithLogger

func WithLogger(logger *slog.Logger) Option

WithLogger configures error and server logger.

func WithMode

func WithMode(mode Mode) Option

WithMode allows the user to specify the server mode by value. DevelopmentMode() and ProductionMode() options are just a syntactic sugar around this function.

func WithServerTimeouts

func WithServerTimeouts(readTimeout, writeTimeout, idleTimeout time.Duration) Option

WithServerTimeouts configures "net/http".Server ReadTimeout, WriteTimeout and IdleTimeout.

func WithShutdownTimeout

func WithShutdownTimeout(timeout time.Duration) Option

WithShutdownTimeout so server doesn't exceed the provided duration after receiving a stop signal.

type Server

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

func New

func New(handler http.Handler, options ...Option) *Server

func (*Server) AddShutdownHook

func (s *Server) AddShutdownHook(hook func(context.Context) error)

func (*Server) Context

func (s *Server) Context() context.Context

func (*Server) Run

func (s *Server) Run() error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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