mserver

package module
v0.0.0-...-cd56c7f Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2018 License: BSD-3-Clause Imports: 14 Imported by: 10

README

GoDoc

mserver

Golang Managed server wrapper for http.Server to handle process SIGINT, SIGKILL, Ctrl+C and provide graceful shutdown of server

Example

package main

import (
    "fmt"
    "net/http"
    "time"

    "github.com/boseji/mserver"
)

func home(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, "Hello World!")
}

func main() {
    http.HandleFunc("/", home)
    // Configure Server on Localhost:8080 and set timeout for 10seconds before force termination
    server := mserver.NewMserver(":8080", 10*time.Second)

    // Start the Server and wait till we receive some events
    server.GracefulStop(true)

    fmt.Println(" Server is now stopped")
}

Documentation

Overview

Package mserver is a wrapper around `net/http` HTTP Server for Graceful shutdown of web server upon SIGINT SIGKILL signals sent to the application or Internal Errors of the web server. It also provides some useful crypto functions to calculate Hashes(Sha, Md5 etc.)

Index

Constants

This section is empty.

Variables

View Source
var ErrServerNotStarted = errors.New("Server Not Started Yet")

Server not started Error code

Functions

func BufToHexString

func BufToHexString(data *bytes.Buffer) string

BufToHexString converts the bytes.Buffer into a Hexadecimal string

func Md5

func Md5(data *bytes.Buffer) *bytes.Buffer

Md5 function get the MD5 Hash from a given bytes.Buffer and returns the result also in bytes.Buffer

func Sha1

func Sha1(data *bytes.Buffer) *bytes.Buffer

Sha1 function get the SHA1 Hash from a given bytes.Buffer and returns the result also in bytes.Buffer As per the FIPS 180-4 : When a message of any length less than 2^64 bits We need to use SHA-1, SHA-224 and SHA-256 That would be 2048 Petabytes

func Sha224

func Sha224(data *bytes.Buffer) *bytes.Buffer

Sha224 function get the SHA2-224 Hash from a given bytes.Buffer and returns the result also in bytes.Buffer

func Sha256

func Sha256(data *bytes.Buffer) *bytes.Buffer

Sha256 function get the SHA2-256 Hash from a given bytes.Buffer and returns the result also in bytes.Buffer

func Sha384

func Sha384(data *bytes.Buffer) *bytes.Buffer

Sha384 function get the SHA2-384 Hash from a given bytes.Buffer and returns the result also in bytes.Buffer

func Sha512

func Sha512(data *bytes.Buffer) *bytes.Buffer

Sha512 function get the SHA2-512 Hash from a given bytes.Buffer and returns the result also in bytes.Buffer

func Sha512_224

func Sha512_224(data *bytes.Buffer) *bytes.Buffer

Sha512_224 function get the SHA2-512/224 Hash from a given bytes.Buffer and returns the result also in bytes.Buffer

func Sha512_256

func Sha512_256(data *bytes.Buffer) *bytes.Buffer

Sha512_256 function get the SHA2-512/256 Hash from a given bytes.Buffer and returns the result also in bytes.Buffer

Types

type Mserver

type Mserver struct {
	Server *http.Server // Instance of the Server

	ShutdownTimeout time.Duration // Timeout before a force shutdown is called
	Error           error         // Last error in operations of the Server
	// contains filtered or unexported fields
}

Mserver or Managed Server is a derivation of the standard HTTP server Provided in Golang standard package `net/http`.

This type provides the facility to stop the HTTP server gracefully and automatically on 3 Events:

1. Ctrl + C is pressed or SIGINT is send to program

2. SIGKILL is send to program

3. Error occurs in Normal Server operation

func NewMserver

func NewMserver(addr string, timeout time.Duration) *Mserver

NewMserver creates a default Instance of the `Mserver` type and then calls the `StartDefaultServer` to begin default server operation.

func (*Mserver) ForceStop

func (p *Mserver) ForceStop()

ForceStop provides a forced way to terminate further processing by sending a false kill signal to a graseful shutdown running function

func (*Mserver) GracefulStop

func (p *Mserver) GracefulStop(waitForInterrupt bool) error

GracefulStop provides a way to stop the server properly with help of `context` package.

The `waitForInterrupt` parameter is used to Listen for SIGINT or SIGKILL sent to the program.

Additionally if an error was reported previous to the function call, it initiates the shutdown of the server. This is done using the member `Mserver.Error` which stores the Last error.

func (*Mserver) StartDefaultServer

func (p *Mserver) StartDefaultServer(addr string, timeout time.Duration)

StartDefaultServer creates a `http.DefaultServeMux` adds it to `http.Server` along with the provided `addr` which is the server address.

The `timeout` is used as wait time before web server is terminated or stopped. It is bassically to force close the server in case it does not respond to a shutdown request. This feature uses the `context` package.

func (*Mserver) StartServer

func (p *Mserver) StartServer(addr string, mux *http.ServeMux, timeout time.Duration)

StartServer creates a server(`http.Server`) using the provided `http.ServeMux` setting it up with the provided `addr` as the server address.

The `mux` can be any type implementing `http.ServeMux` and also the `http.DefaultServeMux`.

The `timeout` is used as wait time before web server is terminated or stopped. It is bassically to force close the server in case it does not respond to a shutdown request. This feature uses the `context` package.

Jump to

Keyboard shortcuts

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