Documentation
¶
Overview ¶
Package rest provides convenience functions for the default http.ServeMux.
Index ¶
- Constants
- func BasicAuthent(handler http.Handler, realm string, ...) http.Handler
- func Chain(f http.HandlerFunc, middlewares ...Middleware) http.HandlerFunc
- func ChainHandler(f http.Handler, middlewares ...Middleware) http.Handler
- func DELETE(mux *http.ServeMux, path string, ...)
- func GET(mux *http.ServeMux, path string, ...)
- func GenerateTLSCertificate(conf CertConf) error
- func IntFromURL(url *url.URL, position int) (int64, error)
- func JSON(w http.ResponseWriter, res interface{}, code int)
- func OPTIONS(mux *http.ServeMux, path string, ...)
- func PATCH(mux *http.ServeMux, path string, ...)
- func POST(mux *http.ServeMux, path string, ...)
- func PUT(mux *http.ServeMux, path string, ...)
- func RequestLogger(handler http.Handler, logger, errlog *log.Logger) http.Handler
- func String(w http.ResponseWriter, res string, code int)
- func StringFromURL(url *url.URL, position int) (string, error)
- func TRACE(mux *http.ServeMux, path string, ...)
- type CertConf
- type Middleware
- type Response
- type SubjectAltName
Constants ¶
const ( ContentType = "Content-Type" ContentTypePlainText = "text/plain; charset=utf-8" ContentTypeJSON = "application/json" )
ContentType constants
Variables ¶
This section is empty.
Functions ¶
func BasicAuthent ¶
func BasicAuthent(handler http.Handler, realm string, authorized func(user, password string) bool) http.Handler
BasicAuthent adds the BasicAuthentMiddleware to the given handler.
func Chain ¶
func Chain(f http.HandlerFunc, middlewares ...Middleware) http.HandlerFunc
Chain executes all given middleware functions for a given http.HandlerFunc.
func ChainHandler ¶
func ChainHandler(f http.Handler, middlewares ...Middleware) http.Handler
ChainHandler like Chain for http.Handlers, it executes all middleware on the given http.Handler
func GenerateTLSCertificate ¶
GenerateTLSCertificate generates a TLS cert specified by the given configuration asynchronously.
Caution: Before using this consider using a secure certificate from https://letsencrypt.org/. Since a self signed certificate is less secure than a proper one. Mainly because the identity of the server using this self signed certificate cannot be confirmed. Which makes it especially vulnerable for man in the middle attacks. Do not use this with public servers!
For more information see: https://en.wikipedia.org/wiki/Self-signed_certificate#Security_issues. https://en.wikipedia.org/wiki/Man-in-the-middle_attack
For https://letsencrypt.org/ in go. You can use: https://github.com/ericchiang/letsencrypt to get the cert.
func IntFromURL ¶
IntFromURL extracts the int value on the given position counted backwards beginning by 0. E. g. given the path /foo/bar/10/20 position 0 would be 20 position 1 would be 10.
func JSON ¶
func JSON(w http.ResponseWriter, res interface{}, code int)
JSON writes a JSON response to the client. If the given interface can not be marshaled an error message with Status InternalServerError is returned.
func RequestLogger ¶
RequestLogger adds the RequestLoggerMiddleware to the given handler.
func String ¶
func String(w http.ResponseWriter, res string, code int)
String writes the given string to the client as content type plain/text UTF-8.
func StringFromURL ¶
StringFromURL extracts the string value on the given position counted backwards beginning by 0. E. g. given the path /foo/bar position 0 would be 'bar' position 1 would be 'foo'.
Types ¶
type CertConf ¶
type CertConf struct { ValidFor time.Duration RASBits int Subject pkix.Name CertOut func() (io.WriteCloser, error) // Will write to the writer and then close. KeyOut func() (io.WriteCloser, error) // Will write to the writer and then close. Random io.Reader HostAddrs []string SubjectAltName SubjectAltName }
CertConf holds the TLS certificate configuration.
type Middleware ¶
type Middleware func(http.HandlerFunc) http.HandlerFunc
Middleware defines the http.HandlerFunc as middleware.
func BasicAuthentMiddleware ¶
func BasicAuthentMiddleware(realm string, authorized func(user, password string) bool) Middleware
BasicAuthentMiddleware is a basic authentication middleware that checks authentication against the given authorized function.
func Method ¶
func Method(method string) Middleware
Method returns a middleware that restricts the allowed http method to the given string.
func RequestLoggerMiddleware ¶
func RequestLoggerMiddleware(logger, errlog *log.Logger) Middleware
RequestLoggerMiddleware is a middleware that logs incoming request to the provided loggers.
type Response ¶
type Response struct { Status int StartTime time.Time ResBuf *bytes.Buffer // contains filtered or unexported fields }
Response can be used to capture the status code, running time and error error response (if present) of a given request.
func NewResponse ¶
func NewResponse(w http.ResponseWriter) *Response
NewResponse returns an with StartTime now and status 200 initialised Response.
func (*Response) Write ¶
Write wraps the call to the http.ResponseWriter. In case of an error it captures up to 50 bytes of the error response.
func (*Response) WriteHeader ¶
WriteHeader wraps the call to http.ResponseWriter and captures the status code.