Help us shape the future of Go! Take the 2026 Go Developer Survey

boilerserv

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2019 License: MIT Imports: 6 Imported by: 0

README

boilerserv Build Status GoDoc

boilserv is a small library which helps you to setup an absolute boilerplate http server for go. It can start on any given port provided by a listener and shuts down gracefully.

Server Configuration

Server is configured by implementing the ServerConfigurator interface and providing it the StartServer function.

Because the server configuration is implemented by yourself you are not forced to use any specific router or logger. The choice is yours.

Example
MyServerConfigurator
type MyServerConfigurator struct {
    listener net.Listener
}

func NewMyServerConfigurator(port int) (*MyServerConfigurator, error) {
    listener, err := net.Listen("tcp", fmt.Springf(":%d", port))
    if err != nil {
    	return nil, err
    }
	
    return &MyServerConfigurator{
        listener: listener,
    }, nil
}

func (sc MyServerConfigurator) Listener() net.Listener {
    return sc.listener
}

func (MyServerConfigurator) ConfigureRoutes() http.Handler {
    mux := http.ServeMux{}
    mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        w.WriteHeader(http.StatusOk)
    })
    
    return &mux
}

func (sc *MyServerConfigurator) LogError(message string, port int) {
    log.Println(message)
}

func (sc *MyServerConfigurator) LogInfo(message string, port int) {
    log.Println(message)
}

Usage
serverConfigurator, err := NewMyServerConfigurator(8080)
if err != nil {
	log.Fatal(err)
}

server, err := boilerserv.StartServer(context.Background(), serverConfigurator)
if err != nil {
	log.Fatal(err)
}

// Block the routine
_ = server.ListenAndHandleShutdown(context.Background())

LICENSE

MIT License

Copyright (c) 2019 Patric Vormstein

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Documentation

Overview

Package boilerserv provides a boilerplate implementation for a http server.

It also tries not to be opinionated about your packages (e.g. router, logger), so you can use your own desired dependencies.

Example:

type MyServerConfigurator struct {
	listener net.Listener
}

func NewMyServerConfigurator(port int) (*MyServerConfigurator, error) {
	listener, err := net.Listen("tcp", fmt.Springf(":%d", port))
	if err != nil {
		return nil, err
	}

	return &MyServerConfigurator{
		listener: listener,
	}, nil
}

func (sc MyServerConfigurator) Listener() net.Listener {
	return sc.listener
}

func (MyServerConfigurator) ConfigureRoutes() http.Handler {
	mux := http.ServeMux{}
	mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusOk)
	})

	return &mux
}

func (sc *MyServerConfigurator) LogError(message string, port int) {
	log.Println(message)
}

func (sc *MyServerConfigurator) LogInfo(message string, port int) {
	log.Println(message)
}

func main() {
	serverConfigurator, err := NewMyServerConfigurator(8080)
	if err != nil {
		log.Fatal(err)
	}

	server, err := boilerserv.StartServer(context.Background(), serverConfigurator)
	if err != nil {
		log.Fatal(err)
	}

	// Block the routine
	_ = server.ListenAndHandleShutdown(context.Background())
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type HttpServer

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

HttpServer wraps a http.Server and can handle graceful shutdown automatically. It implements the Server interface.

func StartServer

func StartServer(ctx context.Context, configurator ServerConfigurator) (*HttpServer, error)

StartServer starts a new HttpServer by providing an implementation of a ServerConfigurator.

func (*HttpServer) ListenAndHandleShutdown

func (s *HttpServer) ListenAndHandleShutdown(ctx context.Context) error

ListenAndHandleShutdown listens on the provided port of the provided net.Listener. It will handle shutdown when receiving an interruption signal.

func (*HttpServer) ShutdownGracefully

func (s *HttpServer) ShutdownGracefully(ctx context.Context) error

ShutdownGracefully will shutdown the HttpServer gracefully.

type Server

type Server interface {
	ShutdownGracefully(ctx context.Context) error
	ListenAndHandleShutdown(ctx context.Context) error
}

Server is an interface for any server that listens and can be shutdown. This interface can be used for testing.

type ServerConfigurator

type ServerConfigurator interface {
	Listener() net.Listener
	ConfigureRoutes() http.Handler
	LogError(message string, port int)
	LogInfo(message string, port int)
}

ServerConfigurator is an interface which can be used to configure the server.

Jump to

Keyboard shortcuts

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