server

package
v2.0.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Jan 23, 2017 License: MIT Imports: 20 Imported by: 0

Documentation

Overview

Package server provides basic wrapping for go web server. Supporting:

  • Context derived from base context for each request
  • Named routing and url reversing
  • Middleware supports
  • Server graceful shutdown
  • Restful resource for json restful api

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Params

func Params(ctx context.Context, key string) string

Params extracts the param from url, e.g. "/hello/:name" -> server.Params(ctx, "name")

Types

type BaseResource

type BaseResource struct{}

BaseResource is a stub Resource definition with empty implemetation

func (BaseResource) Delete

func (ur BaseResource) Delete(ctx context.Context, r *http.Request) (int, interface{})

func (BaseResource) Get

func (ur BaseResource) Get(ctx context.Context, r *http.Request) (int, interface{})

func (BaseResource) Head

func (ur BaseResource) Head(ctx context.Context, r *http.Request) (int, interface{})

func (BaseResource) Patch

func (ur BaseResource) Patch(ctx context.Context, r *http.Request) (int, interface{})

func (BaseResource) Post

func (ur BaseResource) Post(ctx context.Context, r *http.Request) (int, interface{})

func (BaseResource) Put

func (ur BaseResource) Put(ctx context.Context, r *http.Request) (int, interface{})

type FileHandlerHook

type FileHandlerHook func(w http.ResponseWriter, r *http.Request)

Use FileHandlerHook to alter Response header or something

type Handler

type Handler func(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context

Handler is an function signature to be registered to serve routing requests with context support. Server would inject the context to the request handler.

type LatencyCounter

type LatencyCounter struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func NewLatencyCounter

func NewLatencyCounter(size int) *LatencyCounter

func (*LatencyCounter) Add

func (lc *LatencyCounter) Add(latency time.Duration)

func (*LatencyCounter) Stat

func (lc *LatencyCounter) Stat() LatencyStat

type LatencyStat

type LatencyStat struct {
	Latencies []string
	Max       string
	Min       string
	Average   string
	LatP95    string
	LatP75    string
	LatP50    string
}

type MiddleFn

MiddleFn is an adapter to adapt a function to a Middleware interface

func (MiddleFn) ServeHTTP

func (m MiddleFn) ServeHTTP(ctx context.Context, w http.ResponseWriter, r *http.Request, next Handler) context.Context

ServeHTTP adapts the Middleware interface.

type Middleware

type Middleware interface {
	ServeHTTP(ctx context.Context, w http.ResponseWriter, r *http.Request, next Handler) context.Context
}

Middleware is an interface defining the middleware for the server, the middleware should call the next handler to pass the request down, or just return a HttpRedirect request and etc.

func NewRecoveryWare

func NewRecoveryWare(flags ...bool) Middleware

NewRecoveryWare returns a new recovery middleware. Would log the full stack if enable the printStack.

func NewRuntimeWare

func NewRuntimeWare(prefixes []string, trackPageview bool, logInterval ...time.Duration) Middleware

func NewSentryRecoveryWare

func NewSentryRecoveryWare(client *raven.Client, flags ...bool) Middleware

NewSentryRecoveryWare returns a new recovery middleware similar as RecoveryWare but can send messages to the sentry server.

func NewStatWare

func NewStatWare(prefixes ...string) Middleware

NewStatWare returns a new StatWare, some ignored urls can be specified with prefixes which would not be logged.

type Muxer

type Muxer interface {
	Middleware(ware Middleware)
	Handle(method, path, name string, handle Handler)
	Get(path, name string, handle Handler)
	Post(path, name string, handle Handler)
	Put(path, name string, handle Handler)
	Patch(path, name string, handle Handler)
	Delete(path, name string, handle Handler)
	Head(path, name string, handle Handler)
}

type RecoveryWare

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

RecoveryWare is the recovery middleware which can cover the panic situation.

func (*RecoveryWare) ServeHTTP

ServeHTTP implements the Middleware interface, just recover from the panic. Would provide information on the web page if in debug mode.

type Resource

type Resource interface {
	Get(ctx context.Context, r *http.Request) (code int, data interface{})
	Post(ctx context.Context, r *http.Request) (code int, data interface{})
	Put(ctx context.Context, r *http.Request) (code int, data interface{})
	Delete(ctx context.Context, r *http.Request) (code int, data interface{})
	Patch(ctx context.Context, r *http.Request) (code int, data interface{})
	Head(ctx context.Context, r *http.Request) (code int, data interface{})
}

Resouce is an interface to define the basic restful api entry points

type ResourceHandler

type ResourceHandler func(ctx context.Context, r *http.Request) (code int, data interface{})

ResourceHandler is a function type for the restful resources to define a json restful api

type ResponseWriter

type ResponseWriter interface {
	http.ResponseWriter
	http.Flusher
	// Status returns the status code of the response or 0 if the response has not been written.
	Status() int
	// Written returns whether or not the ResponseWriter has been written.
	Written() bool
	// Size returns the size of the response body.
	Size() int
	// Before allows for a function to be called before the ResponseWriter has been written to. This is
	// useful for setting headers or any other operations that must happen before a response has been written.
	Before(func(ResponseWriter))
}

ResponseWriter is a wrapper around http.ResponseWriter that provides extra information about the response. It is recommended that middleware handlers use this construct to wrap a responsewriter if the functionality calls for it.

func NewResponseWriter

func NewResponseWriter(rw http.ResponseWriter) ResponseWriter

NewResponseWriter creates a ResponseWriter that wraps an http.ResponseWriter

type RestfulHandlerAdapter

type RestfulHandlerAdapter func(handle ResourceHandler) Handler

RestfulHandlerAdapter is a function type to adapt a ResourceHandler to Handler

type RuntimeWare

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

RuntimeWare is the statictics middleware which would collect some basic qps, 4xx, 5xx data information

func (*RuntimeWare) ServeHTTP

type SentryRecoveryWare

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

SentryRecoveryWare is the recovery middleware which can cover the panic situation. If a sentry client is in the context, it will send the panic to the sentry server.

func (*SentryRecoveryWare) ServeHTTP

ServeHTTP implements the Middleware interface, just recover from the panic. Would send information to the sentry server.

type Server

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

Server is a struct for all kinds of internal data.

func New

func New(ctx context.Context, isDebug bool) *Server

New a go web server with context as parent context

func (*Server) AddRestfulResource

func (s *Server) AddRestfulResource(path string, name string, resource Resource)

AddRestfulResource will register the resource to the path with given routing name

func (*Server) Assets

func (s *Server) Assets(path string) string

Assets would reverse the assets url, e.g. s.Assets("images/test.png") gives us "/assets/images/test.png"

func (*Server) DefaultRouteFuncs

func (s *Server) DefaultRouteFuncs() template.FuncMap

DefaultRouteFuncs provides a FuncMap for the renderer includes 'assets' and 'urlReverse' so that you can use those functions inside the templates.

func (*Server) Delete

func (s *Server) Delete(path string, name string, handle Handler)

Delete will register a 'DELETE' request handler to the router.

func (*Server) EnableAssetsPrefix

func (s *Server) EnableAssetsPrefix(prefix string)

EnableAssetsPrefix can be used to add assets prefix for assets reverse, like CDN host name.

func (*Server) EnableExtraAssetsJson

func (s *Server) EnableExtraAssetsJson(jsonFile string)

func (*Server) EnableExtraAssetsMapping

func (s *Server) EnableExtraAssetsMapping(assetsMapping map[string]string)

EnableExtraAssetsMapping can be used to set some extra assets mapping data for server, server would look up this mapping for the frontend assets first when reverse an assets url.

func (*Server) Files

func (s *Server) Files(path string, root http.FileSystem)

Files register the static file system or assets to the router

func (*Server) FilesWithHook

func (s *Server) FilesWithHook(path string, root http.FileSystem, hook FileHandlerHook)

func (*Server) Get

func (s *Server) Get(path string, name string, handle Handler)

Get will register a 'GET' request handler to the router.

func (*Server) Handle

func (s *Server) Handle(method, path, name string, handle Handler)

Handle: basic interface which register a http request and handler to the router

func (*Server) Head

func (s *Server) Head(path string, name string, handle Handler)

Head will register a 'HEAD' request handler to the router.

func (*Server) MethodNotAllowed

func (s *Server) MethodNotAllowed(handle Handler)

MethodNotAllowed will register a 405 handler to the router

func (*Server) Middleware

func (s *Server) Middleware(ware Middleware)

Middleware: Register a middleware to a server object.

func (*Server) NotFound

func (s *Server) NotFound(handle Handler)

NotFound wil register a 404 NotFound handler to the router.

func (*Server) Patch

func (s *Server) Patch(path string, name string, handle Handler)

Patch will register a 'PATCH' request handler to the router.

func (*Server) Post

func (s *Server) Post(path string, name string, handle Handler)

Post will register a 'POST' request handler to the router.

func (*Server) Put

func (s *Server) Put(path string, name string, handle Handler)

Put will register a 'PUT' request handler to the router.

func (*Server) RestfulHandlerAdapter

func (s *Server) RestfulHandlerAdapter(adapter RestfulHandlerAdapter)

RestfulHandlerAdapter will set the server's restful handler adapter

func (*Server) Reverse

func (s *Server) Reverse(name string, params ...interface{}) string

Reverse would reverse the named routes with params supported. E.g. we have a routes "/hello/:name" named "Hello", then we can call s.Reverse("Hello", "world") gives us "/hello/world"

func (*Server) Run

func (s *Server) Run(addr string) error

Run the server listen and server at the addr with graceful shutdown supports.

func (*Server) Stop

func (s *Server) Stop(timeout time.Duration)

type StatWare

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

StatWare is the statistics middleware which would log all the access and performation information.

func (*StatWare) ServeHTTP

func (m *StatWare) ServeHTTP(ctx context.Context, w http.ResponseWriter, r *http.Request, next Handler) context.Context

ServeHTTP implements the Middleware interface. Would log all the access, status and performance information.

Jump to

Keyboard shortcuts

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