server

package
v0.5.3 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2024 License: MIT Imports: 9 Imported by: 1

Documentation

Overview

Package server provides a HTTP server with custom timeouts and error handling.

This package is designed to be used with the wasabi package for handling HTTP requests. It provides a Server struct that wraps the standard http.Server with additional functionality.

The Server struct includes a base context, a net.Listener for accepting connections, and a mutex for thread safety. It also includes a ready channel that can be used to signal when the server is ready to accept connections.

The server uses custom read header and read timeouts, defined as constants. These timeouts help to prevent slow client attacks by limiting the amount of time the server will wait for a client to send its request.

Usage:

s := server.NewServer(":8080")
s.AddChannel(channel.NewChannel("/path", handler))
err := s.Start()
if err != nil {
    log.Fatal(err)
}

This will start a new server on port 8080 with the provided handler.

Index

Constants

View Source
const ReadHeaderTimeoutSeconds = 3
View Source
const ReadTimeoutSeconds = 30

Variables

View Source
var DefaultConfig = Config{
	ReadHeaderTimeout: ReadHeaderTimeoutSeconds * time.Second,
	ReadTimeout:       ReadTimeoutSeconds * time.Second,
}
View Source
var ErrServerAlreadyRunning = fmt.Errorf("server is already running")

Functions

This section is empty.

Types

type Config added in v0.5.0

type Config struct {
	ReadHeaderTimeout time.Duration
	ReadTimeout       time.Duration
}

type Option

type Option func(*Server)

func WithBaseContext

func WithBaseContext(ctx context.Context) Option

BaseContext optionally specifies based context that will be used for all connections. If not specified, context.Background() will be used.

func WithProfilerEndpoint added in v0.5.0

func WithProfilerEndpoint() Option

WithProfilerEndpoint is an option function that enables the profiler endpoint for the server. Enabling the profiler endpoint allows profiling and performance monitoring of the server. The profiler endpoint is available at /debug/pprof/. To use the profiler endpoint, import the net/http/pprof package in your application. Example:

import _ "net/http/pprof"

The profiler endpoint is disabled by default.

func WithReadinessChan added in v0.4.0

func WithReadinessChan(ch chan<- struct{}) Option

WithReadinessChan sets ch to Server and will be closed once the Server is ready to accept connection. Typically used in testing after calling [Run] method and waiting for ch to close, before continuing with test logics.

func WithServerConfig added in v0.5.0

func WithServerConfig(config Config) Option

WithServerConfig is an option function that overrides the default configuration settings of the server. This can give the client more control over the server feature functions

func WithTLS added in v0.5.0

func WithTLS(certFile, keyFile string, config ...*tls.Config) Option

WithTLS is an option function that configures the server to use TLS (Transport Layer Security). It sets the certificate and key file paths, and optionally allows custom TLS configuration. The certificate and key file paths must be provided as arguments. If a custom TLS configuration is provided, it will be applied to the server's handler.

type Server

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

func NewServer

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

NewServer creates new instance of Wasabi server port - port to listen on returns new instance of Server

func (*Server) AddChannel

func (s *Server) AddChannel(channel wasabi.Channel)

AddChannel adds new channel to server

func (*Server) AddHandler added in v0.5.3

func (s *Server) AddHandler(path string, handler http.Handler)

AddHandler adds new handler to server

func (*Server) Addr added in v0.3.0

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

Addr returns the server's network address. If the server is not running, it returns nil.

func (*Server) Close

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

Shutdown gracefully shuts down the server and all its channels. It waits for all channels to be shut down before returning. If the context is canceled before all channels are shut down, it returns the context error. If any error occurs during the shutdown process, it returns the first error encountered.

func (*Server) GetServerConfig added in v0.5.0

func (s *Server) GetServerConfig() Config

Helper to fetch server config. Returns Default config if base ctx has no config set

func (*Server) Run

func (s *Server) Run() (err error)

Run starts the server returns error if server is already running or if server fails to start

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳