httpsrv

package
v0.24.0 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2020 License: Apache-2.0 Imports: 12 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.

Configuration

The HTTP server can be configured through these environment variables:

  • HTTP_ADDR the HTTP server listener's network address (default: 0.0.0.0:80)

Examples

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

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"
	"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(&http.Server{
		Handler:     handler,
		ReadTimeout: 1 * time.Second,
	}).Run(ctx)
}
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 := &rest.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,
	}

	// DefaultCORS contains the default CORS settings used when starting an httpsrv.Server
	DefaultCORS = CORS{

		AllowOrigin: "*",
		AllowHeaders: []string{
			"Authorization",
			"Origin",
			"Accept",
			"Content-Type",
			"X-Requested-With",
		},
		AllowMethods: []string{
			http.MethodGet,
			http.MethodHead,
			http.MethodPost,
			http.MethodPut,
			http.MethodPatch,
			http.MethodDelete,
		},
	}
)

Functions

func CORSHandler added in v0.23.3

func CORSHandler(c CORS, next http.Handler) http.HandlerFunc

CORSHandler updates headers on CORS preflight requests and actual CORS requests.

func HealthHandler

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

HealthHandler responds with service health.

func WrapperHandler

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

WrapperHandler returns the wrapper handler for the http server.

Types

type CORS added in v0.23.3

type CORS struct {
	AllowOrigin  string
	AllowHeaders []string
	AllowMethods []string
}

CORS defines a struct which allows configuring the CORS settings for httpsrv.Server

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
	CORS   CORS
	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(server *http.Server) *Server

New sets up a new HTTP server.

func NewDefault added in v0.5.0

func NewDefault(handler http.Handler) *Server

NewDefault returns a 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) Halt added in v0.21.0

func (gs *Server) Halt(ctx context.Context) error

Halt will attempt to gracefully shut down the server.

func (*Server) Run

func (gs *Server) Run(_ context.Context) 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