server

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2021 License: MIT Imports: 12 Imported by: 0

README

server

Simple wrapper around Golang net/http to start/shut a server

This is a custom wrapper around net/http server which handles serving requests and shut down on the required SYSCALL without having to explicitly write code to handle interrupts to shutdown a server. Also implements the basic SO_REUSEPORT functionality to fork multiple child processes to listen to the same port, and act as multiple workers behind the same port.

Features

  • Create a simple http server
  • Simple interface. One function Serve which handles OS interrupts too, to shut down a server

Installing

go get -u github.com/arriqaaq/server

Example

Here's a full example of a Server that listens:

You can run this example from a terminal:

go run example/main.go
package main

import (
	"fmt"
	lucio "github.com/arriqaaq/server"
	"log"
	"net/http"
	"os"
)

func main() {
	mux := http.NewServeMux()
	mux.HandleFunc("/hello", func(w http.ResponseWriter, req *http.Request) {
		fmt.Fprintf(w, "Hello from %v!\n", os.Getpid())
	})
	server := lucio.NewServer(mux, "0.0.0.0", 8080)
	err := server.Serve()
	log.Println("terminated", os.Getpid(), err)
}

TODO

  • Add more test cases

References

Contact

Farhan @arriqaaq

License

Server is available under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SplitHostPort

func SplitHostPort(addr string) (host string, port int, err error)

SplitHostPort splits a network address into a host and a port. The port is -1 when there is no port to be found

Types

type Server

type Server struct {
	EnabledListeners []string      //the listeners to enable
	CleanupTimeout   time.Duration //grace period for which to wait before shutting down the server
	MaxHeaderSize    int           //controls the maximum number of bytes the server will read parsing the request header's keys and values, including the request line

	SocketPath string //the unix socket to listen on" default:"/var/run/server.sock"

	Host         string        //the IP to listen on" default:"localhost" env:"HOST"
	Port         int           //the port to listen on for insecure connections, defaults to a random value" env:"PORT"
	KeepAlive    time.Duration //sets the TCP keep-alive timeouts on accepted connections. It prunes dead TCP connections
	ReadTimeout  time.Duration //maximum duration before timing out read of the request"
	WriteTimeout time.Duration //maximum duration before timing out write of the response"

	Logger func(string, ...interface{})
	// contains filtered or unexported fields
}

Options taken from swagger docs

func NewServer

func NewServer(handler http.Handler, host string, port int) *Server

NewServer creates a new api server but does not configure it

func (*Server) Fatalf

func (s *Server) Fatalf(f string, args ...interface{})

Fatalf logs message either via defined user logger or via system one if no user logger is defined. Exits with non-zero status after printing

func (*Server) GetHandler

func (s *Server) GetHandler() http.Handler

GetHandler returns a handler useful for testing

func (*Server) HTTPListener

func (s *Server) HTTPListener() (net.Listener, error)

HTTPListener returns the http listener

func (*Server) Listen

func (s *Server) Listen() error

Listen creates the listeners for the server

func (*Server) Logf

func (s *Server) Logf(f string, args ...interface{})

Logf logs message either via defined user logger or via system one if no user logger is defined.

func (*Server) Serve

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

Serve the api

func (*Server) SetHandler

func (s *Server) SetHandler(handler http.Handler)

SetHandler allows for setting a http handler on this server

func (*Server) Shutdown added in v0.2.0

func (s *Server) Shutdown() error

Shutdown server and clean up resources

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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