server

package
v0.0.0-...-2460f69 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package server provides functionality for HTTP server management.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Port            string        // Port on which the server will listen
	ReadTimeout     time.Duration // Maximum duration for reading the entire request
	WriteTimeout    time.Duration // Maximum duration before timing out writes of the response
	IdleTimeout     time.Duration // Maximum amount of time to wait for the next request
	ShutdownTimeout time.Duration // Maximum time to wait for server shutdown
}

Config contains server configuration parameters. It defines all the timeouts and connection settings for the HTTP server.

func NewConfig

func NewConfig(port string, readTimeout, writeTimeout, idleTimeout, shutdownTimeout time.Duration) Config

NewConfig creates a new server configuration from the provided values. It initializes a Config struct with the specified parameters.

Parameters:

  • port: The port on which the server will listen
  • readTimeout: Maximum duration for reading the entire request
  • writeTimeout: Maximum duration before timing out writes of the response
  • idleTimeout: Maximum amount of time to wait for the next request
  • shutdownTimeout: Maximum time to wait for server shutdown

Returns:

  • A Config struct initialized with the provided values

type Server

type Server struct {
	*http.Server // Embedded standard HTTP server
	// contains filtered or unexported fields
}

Server represents an HTTP server with additional functionality. It extends the standard http.Server with logging, context-aware logging, and graceful shutdown capabilities.

func New

func New(cfg Config, handler http.Handler, logger *zap.Logger, contextLogger *logging.ContextLogger) *Server

New creates a new server with the given configuration and handler. It applies middleware to the handler and initializes the HTTP server.

Parameters:

  • cfg: Server configuration parameters
  • handler: HTTP handler for processing requests
  • logger: Logger for server events
  • contextLogger: Context-aware logger for request-scoped logging

Returns:

  • A pointer to a new Server instance

func (*Server) Shutdown

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

Shutdown gracefully shuts down the server. It stops accepting new connections and waits for existing connections to complete processing, up to the configured shutdown timeout.

Parameters:

  • ctx: Context for the shutdown operation, which may be cancelled

Returns:

  • An error if the shutdown fails, or nil on success

func (*Server) Start

func (s *Server) Start()

Start starts the server in a goroutine. It begins listening for HTTP requests in a non-blocking manner. If the server fails to start, it logs a fatal error and terminates the application.

Jump to

Keyboard shortcuts

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