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 ¶
- func Params(ctx context.Context, key string) string
- type BaseResource
- func (ur BaseResource) Delete(ctx context.Context, r *http.Request) (int, interface{})
- func (ur BaseResource) Get(ctx context.Context, r *http.Request) (int, interface{})
- func (ur BaseResource) Head(ctx context.Context, r *http.Request) (int, interface{})
- func (ur BaseResource) Patch(ctx context.Context, r *http.Request) (int, interface{})
- func (ur BaseResource) Post(ctx context.Context, r *http.Request) (int, interface{})
- func (ur BaseResource) Put(ctx context.Context, r *http.Request) (int, interface{})
- type FileHandlerHook
- type Handler
- type LatencyCounter
- type LatencyStat
- type MiddleFn
- type Middleware
- type Muxer
- type RecoveryWare
- type Resource
- type ResourceHandler
- type ResponseWriter
- type RestfulHandlerAdapter
- type RuntimeWare
- type SentryRecoveryWare
- type Server
- func (s *Server) AddRestfulResource(path string, name string, resource Resource)
- func (s *Server) Assets(path string) string
- func (s *Server) DefaultRouteFuncs() template.FuncMap
- func (s *Server) Delete(path string, name string, handle Handler)
- func (s *Server) EnableAssetsPrefix(prefix string)
- func (s *Server) EnableExtraAssetsJson(jsonFile string)
- func (s *Server) EnableExtraAssetsMapping(assetsMapping map[string]string)
- func (s *Server) Files(path string, root http.FileSystem)
- func (s *Server) FilesWithHook(path string, root http.FileSystem, hook FileHandlerHook)
- func (s *Server) Get(path string, name string, handle Handler)
- func (s *Server) Handle(method, path, name string, handle Handler)
- func (s *Server) Head(path string, name string, handle Handler)
- func (s *Server) MethodNotAllowed(handle Handler)
- func (s *Server) Middleware(ware Middleware)
- func (s *Server) NotFound(handle Handler)
- func (s *Server) Patch(path string, name string, handle Handler)
- func (s *Server) Post(path string, name string, handle Handler)
- func (s *Server) Put(path string, name string, handle Handler)
- func (s *Server) RestfulHandlerAdapter(adapter RestfulHandlerAdapter)
- func (s *Server) Reverse(name string, params ...interface{}) string
- func (s *Server) Run(addr string) error
- func (s *Server) Stop(timeout time.Duration)
- type StatWare
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type BaseResource ¶
type BaseResource struct{}
BaseResource is a stub Resource definition with empty implemetation
type FileHandlerHook ¶
type FileHandlerHook func(w http.ResponseWriter, r *http.Request)
Use FileHandlerHook to alter Response header or something
type Handler ¶
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 ¶
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 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.
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 ¶
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
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.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server is a struct for all kinds of internal data.
func (*Server) AddRestfulResource ¶
AddRestfulResource will register the resource to the path with given routing name
func (*Server) Assets ¶
Assets would reverse the assets url, e.g. s.Assets("images/test.png") gives us "/assets/images/test.png"
func (*Server) DefaultRouteFuncs ¶
DefaultRouteFuncs provides a FuncMap for the renderer includes 'assets' and 'urlReverse' so that you can use those functions inside the templates.
func (*Server) EnableAssetsPrefix ¶
EnableAssetsPrefix can be used to add assets prefix for assets reverse, like CDN host name.
func (*Server) EnableExtraAssetsJson ¶
func (*Server) EnableExtraAssetsMapping ¶
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) Handle ¶
Handle: basic interface which register a http request and handler to the router
func (*Server) MethodNotAllowed ¶
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) RestfulHandlerAdapter ¶
func (s *Server) RestfulHandlerAdapter(adapter RestfulHandlerAdapter)
RestfulHandlerAdapter will set the server's restful handler adapter
func (*Server) Reverse ¶
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"