server

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2025 License: AGPL-3.0 Imports: 8 Imported by: 0

README

Server Package

The server package provides a simple and configurable HTTP server implementation with support for graceful shutdown, different operating modes, and configurable logging levels.

Features

  • Configurable Server Options: Set host, port, URL, handler, mode, and log level
  • Multiple Operating Modes: Production and testing modes
  • Configurable Logging: Debug, info, error, and none log levels
  • Graceful Shutdown: Handles OS signals (SIGINT, SIGTERM) for clean server termination
  • Colorized Logging: Uses cfmt for better visibility of server status

Usage

Basic Example
package main

import (
    "net/http"
    "your-project/server"
)

func main() {
    // Define a simple handler
    handler := func(w http.ResponseWriter, r *http.Request) {
        w.Write([]byte("Hello, World!"))
    }

    // Start the server
    server.Start(server.Options{
        Host:    "localhost",
        Port:    "8080",
        URL:     "http://localhost:8080",
        Handler: handler,
        Mode:    server.ProductionMode,
        LogLevel: server.LogLevelInfo,
    })
}
Configuration Options

The Options struct provides the following configuration options:

Option Type Description Default
Host string The host to bind the server to Required
Port string The port to bind the server to Required
URL string The URL displayed in logs Optional
Handler http.HandlerFunc The HTTP handler function Required
Mode string Server mode (production/testing) "production"
LogLevel LogLevel Logging level "info"
Log Levels

The package supports the following log levels:

  • LogLevelDebug: Detailed debugging information
  • LogLevelInfo: General information about server operations
  • LogLevelError: Error messages only
  • LogLevelNone: No logging
Operating Modes
  • ProductionMode: Standard production mode with normal error handling
  • TestingMode: Special mode for testing with different error handling

Testing

The package includes a test file (start_test.go) that demonstrates how to test the server functionality, including:

  • Starting the server
  • Making requests to verify it's running
  • Gracefully shutting down the server
  • Verifying the server has shut down

Dependencies

  • github.com/mingrammer/cfmt: Colorized formatting for logs

License

This package is part of the main project and subject to its license terms.

Documentation

Index

Constants

View Source
const DefaultMode = ProductionMode

DefaultMode is the default mode for the server.

View Source
const ProductionMode = "production"
View Source
const TestingMode = "testing"

Variables

This section is empty.

Functions

This section is empty.

Types

type LogLevel added in v0.2.0

type LogLevel string

LogLevel represents the level of logging.

const (
	// LogLevelDebug is the debug logging level.
	LogLevelDebug LogLevel = "debug"
	// LogLevelInfo is the info logging level.
	LogLevelInfo LogLevel = "info"
	// LogLevelError is the error logging level.
	LogLevelError LogLevel = "error"
	// LogLevelNone is the none logging level.
	LogLevelNone LogLevel = "none"
)

type Options

type Options struct {
	Host     string
	Port     string
	URL      string // optional, displayed in logs
	Handler  func(w http.ResponseWriter, r *http.Request)
	Mode     string   // optional, default is production, can be development or testing
	LogLevel LogLevel // optional, default is "info", can be "debug", "info", "error", or "none"
}

Options represents the configuration for the web server.

type Server added in v0.10.0

type Server struct {
	*http.Server
}

func New added in v0.10.0

func New(address string, handler func(w http.ResponseWriter, r *http.Request)) *Server

func Start

func Start(options Options) (server *Server, err error)

StartWebServerbserver starts the web server at the specified host and port and listens for incoming requests.

Example:

StartWebServer(Options{
 Host: "localhost",
 Port: "8080",
 Handler: func(w http.ResponseWriter, r *http.Request) {},
 Mode: "production",
})

Parameters: - none

Returns: - none

func (*Server) Start added in v0.10.0

func (s *Server) Start() error

Jump to

Keyboard shortcuts

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