httpsrv

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2019 License: Apache-2.0 Imports: 10 Imported by: 0

README

HTTP Server

The package core/workers/httpsrv provides a default set of configuration for hosting a http server in a service.

Examples

Starting server and expose the service
srv := httpsrv.New(handler)
srv.Run(ctx, os.Stdout)
Starting server with a custom http server
srv := httpsrv.New(handler, &http.Server{
    ReadTimeout: 1 * time.Second,
})
srv.Run(ctx, os.Stdout)

Documentation

Overview

Package httpsrv provides a default set of configuration for hosting a http server in a service.

Example
package main

import (
	"context"
	"net/http"
	"os"
	"time"

	"github.com/LUSHDigital/core/workers/httpsrv"
)

var (
	handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(200)
		return
	})
	ctx context.Context
)

func main() {
	go httpsrv.New(handler, nil).Run(ctx, os.Stdout)

	// Start a new server worker with a custom http.Server
	go httpsrv.New(handler, &http.Server{
		ReadTimeout: 1 * time.Second,
	}).Run(ctx, os.Stdout)
}
Output:

Index

Examples

Constants

View Source
const (
	// Port is the default HTTP port.
	Port = 80
)

Variables

View Source
var (
	// NotFoundHandler responds with the default a 404 response.
	NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
		res := &response.Response{
			Code:    http.StatusNotFound,
			Message: http.StatusText(http.StatusNotFound),
		}
		res.WriteTo(w)
	})

	// DefaultHTTPServer represents the default configuration for the http server
	DefaultHTTPServer = http.Server{
		WriteTimeout:      5 * time.Second,
		ReadTimeout:       5 * time.Second,
		IdleTimeout:       5 * time.Second,
		ReadHeaderTimeout: 1 * time.Second,
	}
)

Functions

func HealthHandler

func HealthHandler(now func() time.Time) http.HandlerFunc

HealthHandler responds with service health.

func WrapperHandler

func WrapperHandler(now func() time.Time, next http.Handler) http.HandlerFunc

WrapperHandler returns the wrapper handler for the http server.

Types

type HealthResponse

type HealthResponse struct {
	Latency       string `json:"latency"`
	StackInUse    string `json:"stack_in_use"`
	HeapInUse     string `json:"heap_in_use"`
	HeapAlloc     string `json:"heap_alloc"`
	NumGoRoutines int    `json:"num_go_routines"`
}

HealthResponse contains information about the service health.

type Server

type Server struct {
	Server  *http.Server
	Handler http.Handler
	Now     func() time.Time
	// contains filtered or unexported fields
}

Server represents a collection of functions for starting and running an RPC server.

func New

func New(handler http.Handler, server *http.Server) *Server

New sets up a new HTTP server.

func (*Server) Addr

func (gs *Server) Addr() *net.TCPAddr

Addr will block until you have received an address for your server.

func (*Server) Run

func (gs *Server) Run(ctx context.Context, out io.Writer) error

Run will start the gRPC server and listen for requests.

Jump to

Keyboard shortcuts

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