Documentation ¶
Index ¶
- Constants
- Variables
- func GetURL(conf *Config, path string) string
- func NewHTTPRequest(conf *Config, method, path string, req *Request) (*http.Request, error)
- func NewHTTPRequestWithURL(method, url string, req *Request) (*http.Request, error)
- type AuthFunc
- type Config
- type Context
- type Error
- type HandlerFunc
- type Request
- type Response
- type Server
- func (s *Server) Close() error
- func (s *Server) HandleFunc(pattern string, handler HandlerFunc)
- func (s *Server) ListenAndServe() error
- func (s *Server) Mux() *http.ServeMux
- func (s *Server) ParseRequest(logger log.Logger, w http.ResponseWriter, r *http.Request, args interface{}) bool
- func (s *Server) RequireBasicAuth(logger log.Logger, handler HandlerFunc, auth AuthFunc) HandlerFunc
- func (s *Server) RequireHTTPMethods(logger log.Logger, handler HandlerFunc, methods ...string) HandlerFunc
- func (s *Server) RespondError(logger log.Logger, w http.ResponseWriter, err *Error)
- func (s *Server) RespondResult(logger log.Logger, w http.ResponseWriter, result interface{})
- type TLSConfig
Constants ¶
const ( ErrCodeMethodNotAllowed = 1 ErrCodeInternalServerError = iota ErrCodeFailedToParseRequest = iota ErrCodeAccessDenied = iota ErrCodeMax = 100 )
Common server error codes.
Variables ¶
var ( ErrMethodNotAllowed = &Error{ Status: http.StatusMethodNotAllowed, Code: ErrCodeMethodNotAllowed, Message: "HTTP method not allowed", } ErrInternalServerError = &Error{ Status: http.StatusInternalServerError, Code: ErrCodeInternalServerError, Message: "internal server error", } ErrFailedToParseRequest = &Error{ Status: http.StatusBadRequest, Code: ErrCodeFailedToParseRequest, Message: "failed to parse request", } ErrAccessDenied = &Error{ Status: http.StatusForbidden, Code: ErrCodeAccessDenied, Message: "access denied", } )
Common server errors.
Functions ¶
func NewHTTPRequest ¶
NewHTTPRequest creates a new HTTP request from a given server request.
Types ¶
type HandlerFunc ¶
type HandlerFunc func(w http.ResponseWriter, r *http.Request, ctx *Context)
HandlerFunc is an HTTP request handler function which receives additional context.
type Request ¶
type Request struct {
Args json.RawMessage `json:"args,omitempty"`
}
Request is a server request.
type Response ¶
type Response struct { Error *Error `json:"error,omitempty"` Result json.RawMessage `json:"result,omitempty"` }
Response is a server reply.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server is an HTTP server.
func (*Server) HandleFunc ¶
func (s *Server) HandleFunc(pattern string, handler HandlerFunc)
HandleFunc registers a handler function for a given pattern.
func (*Server) ListenAndServe ¶
ListenAndServe starts to listen and to serve requests.
func (*Server) ParseRequest ¶
func (s *Server) ParseRequest(logger log.Logger, w http.ResponseWriter, r *http.Request, args interface{}) bool
ParseRequest parses request handling possible errors.
func (*Server) RequireBasicAuth ¶
func (s *Server) RequireBasicAuth(logger log.Logger, handler HandlerFunc, auth AuthFunc) HandlerFunc
RequireBasicAuth wraps a given handler function inside a handler with basic access authentication.
func (*Server) RequireHTTPMethods ¶
func (s *Server) RequireHTTPMethods(logger log.Logger, handler HandlerFunc, methods ...string) HandlerFunc
RequireHTTPMethods wraps a given handler function inside an HTTP method validating handler.
func (*Server) RespondError ¶
RespondError sends a response with a given error.
func (*Server) RespondResult ¶
func (s *Server) RespondResult(logger log.Logger, w http.ResponseWriter, result interface{})
RespondResult sends a response with a given result.