server

package module
v0.0.0-...-439bdec Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2016 License: Apache-2.0 Imports: 16 Imported by: 0

README

middleware-server

Lightweight server module, handling http middlewares.

Docs

http://godoc.org/github.com/giantswarm/middleware-server

Install
$ go get github.com/giantswarm/middleware-server
Import
import "github.com/giantswarm/middleware-server"
Usage

See the examples

make build-examples
Responders

http://godoc.org/github.com/giantswarm/middleware-server#Response

Access Logging

There is a access logging implemented by default when setting a logger.

# format: date time file:line: [level] METHOD path code bytes milliseconds
2014/05/28 12:51:22 logaccess.go:56: [INFO] GET /v1/hello-world 200 11 0

Documentation

Index

Constants

View Source
const (
	StatusHealthy   = "healthy"
	StatusUnhealthy = "unhealthy"
)
View Source
const (
	DefaultCloseListenerDelay = 0
	DefaultOsExitDelay        = 5
	DefaultOsExitCode         = 0

	RequestIDKey    = "request-id"
	RequestIDHeader = "X-Request-ID"
)

Variables

This section is empty.

Functions

func IsStatusHealthy

func IsStatusHealthy(status string) bool

func NewIDFactory

func NewIDFactory() func() string

func NewLogAccessHandler

func NewLogAccessHandler(reporter, preHTTP, postHTTP AccessReporter, next http.Handler) http.Handler

NewLogAccessHandler executes the next handler and logs the requests statistics afterwards to the logger.

Types

type AccessEntry

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

func (*AccessEntry) Duration

func (ae *AccessEntry) Duration() time.Duration

func (*AccessEntry) Request

func (ae *AccessEntry) Request() *http.Request

func (*AccessEntry) RequestMethod

func (ae *AccessEntry) RequestMethod() string

func (*AccessEntry) RequestURI

func (ae *AccessEntry) RequestURI() string

func (*AccessEntry) RouteName

func (ae *AccessEntry) RouteName() string

func (*AccessEntry) Size

func (ae *AccessEntry) Size() int64

func (*AccessEntry) StatusCode

func (ae *AccessEntry) StatusCode() int

type AccessReporter

type AccessReporter func(entry *AccessEntry)

func DefaultAccessReporter

func DefaultAccessReporter(ctx requestcontext.Ctx, logger requestcontext.Logger) AccessReporter

func ExtendedAccessReporter

func ExtendedAccessReporter(ctx requestcontext.Ctx, logger requestcontext.Logger) AccessReporter

ExtendedAccessReporter createsan access logger that logs everything that DefaultAccessReporter does with the User-Agent added to that

type Context

type Context struct {
	// Contains all placeholders from the route.
	MuxVars map[string]string

	// Helper to quickly write results to the `http.ResponseWriter`.
	Response Response

	// A middleware should call Next() to signal that no problem was encountered
	// and the next middleware in the chain can be executed after this middleware
	// finished. Always returns `nil`, so it can be convieniently used with
	// return to quit the middleware.
	Next func() error

	// The app context for this request. Gets prefilled by the
	// CtxConstructor, if set in the server.
	App     interface{}
	Request requestcontext.Ctx
}

Context is a map getting through all middlewares.

func (*Context) RequestID

func (c *Context) RequestID() string

RequestID returns ID for the current request. If none is found, an empty string is returned.

func (*Context) SetRequestID

func (c *Context) SetRequestID(ID string)

SetRequestID overwrites the request ID of the current request with the given ID.

type CtxConstructor

type CtxConstructor func() interface{}

type HealthInfo

type HealthInfo struct {
	Status   string       `json:"status"`
	App      string       `json:"app"`
	Version  string       `json:"version"`
	Backends []HealthInfo `json:"backends"`
}

type Healthchecker

type Healthchecker func() (HealthInfo, error)

func (Healthchecker) Status

func (hc Healthchecker) Status() (HealthInfo, error)

Status just accumulates the backends status to calculate the main status.

type Middleware

type Middleware func(res http.ResponseWriter, req *http.Request, ctx *Context) error

Middleware is a http handler method.

func NewHealthcheckMiddleware

func NewHealthcheckMiddleware(hc Healthchecker) Middleware

NewHealthcheckMiddleware provides a middleware that responds JSON formatted information about a service. E.g. one can register this under /healthcheck.

func NewWelcomeMiddleware

func NewWelcomeMiddleware(appName, version string) Middleware

NewWelcomeMiddleware provides a middleware that responds human readable information about a service. E.g. one can register this under /.

type Response

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

func (*Response) Error

func (response *Response) Error(message string, code int) error

func (*Response) Forbidden

func (response *Response) Forbidden() error

Forbidden sends the http.StatusForbidden status. Use it to signal that the requestee has no access to the given resource (but auth itself worked).

func (*Response) Json

func (response *Response) Json(result interface{}, code int) error

func (*Response) NoContent

func (response *Response) NoContent() error

func (*Response) PlainText

func (response *Response) PlainText(content string, code int) error

func (*Response) Redirect

func (response *Response) Redirect(location string, code int) error

func (*Response) Unauthorized

func (response *Response) Unauthorized(scheme string) error

Unauthorized sends the http.StatusUnauthorized status code. Use this to signal the requestee that the authentication failed.

type Server

type Server struct {
	Logger requestcontext.Logger

	Router *mux.Router

	IDFactory func() string
	// contains filtered or unexported fields
}

func NewServer

func NewServer(host, port string) *Server

func (*Server) Close

func (s *Server) Close()

func (*Server) Closing

func (s *Server) Closing() bool

Closing returns true when the server is shutting down, false otherwise.

func (*Server) ExitProcess

func (s *Server) ExitProcess()

func (*Server) ExtendAccessLogging

func (s *Server) ExtendAccessLogging()

ExtendAccessLogging turns on the usage of ExtendedAccessLogger

func (*Server) Listen

func (s *Server) Listen()

func (*Server) NewMiddlewareHandler

func (s *Server) NewMiddlewareHandler(middlewares []Middleware) http.Handler

NewMiddlewareHandler wraps the middlewares in a http.Handler. The handler, on activation, calls each middleware in order, if no error was returned and `ctx.Next()` was called. If a middleware wants to finish the processing, it can just write to the `http.ResponseWriter` or use the `ctx.Response` for convienience.

func (*Server) RegisterRoutes

func (s *Server) RegisterRoutes(mux *http.ServeMux, prefix string)

func (*Server) Serve

func (s *Server) Serve(method, urlPath string, middlewares ...Middleware)

func (*Server) ServeNotFound

func (s *Server) ServeNotFound(middlewares ...Middleware)

func (*Server) ServeStatic

func (s *Server) ServeStatic(urlPath, fsPath string)

ServeStatis registers a middleware that serves files from the filesystem. Example: s.ServeStatic("/v1/public", "./public_html/v1/")

func (*Server) SetAppContext

func (s *Server) SetAppContext(ctxConstructor CtxConstructor)

SetAppContext sets the CtxConstructor object, that is called for every request to provide the initial `Context.App` value, which is available to every middleware.

func (*Server) SetCloseListenerDelay

func (s *Server) SetCloseListenerDelay(d int)

SetCloseListenerDelay sets the time to delay closing the TCP listener when calling `s.Close()`.

func (*Server) SetLogColor

func (s *Server) SetLogColor(color bool)

func (*Server) SetLogLevel

func (s *Server) SetLogLevel(level string)

func (*Server) SetLogger

func (s *Server) SetLogger(logger requestcontext.Logger)

SetLogger sets the logger object to which the server logs every request.

func (*Server) SetOsExitCode

func (s *Server) SetOsExitCode(c int)

SetOsExitCode sets the exit code used in `os.Exit(c)` when calling `s.Close()`.

func (*Server) SetOsExitDelay

func (s *Server) SetOsExitDelay(d int)

SetOsExitDelay sets the time to delay exiting the process when calling `s.Close()`.

func (*Server) SetPostHTTPHandler

func (s *Server) SetPostHTTPHandler(reporter AccessReporter)

func (*Server) SetPreHTTPHandler

func (s *Server) SetPreHTTPHandler(reporter AccessReporter)

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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