Documentation
¶
Overview ¶
Package rex provides a simple & light-weight REST server in golang
Index ¶
- func Delete(path string, handles ...Handle)
- func Fallback(handle Handle)
- func Get(path string, handles ...Handle)
- func Head(path string, handles ...Handle)
- func Options(path string, handles ...Handle)
- func Patch(path string, handles ...Handle)
- func Post(path string, handles ...Handle)
- func Put(path string, handles ...Handle)
- func Serve(config ServerConfig)
- func Start(port uint16)
- func StartAutoTLS(port uint16, hosts ...string)
- func StartTLS(port uint16, certFile string, keyFile string)
- func Static(path string, root string, fallbackPath ...string)
- func Trace(path string, handles ...Handle)
- func Use(middlewares ...Handle)
- type ACLUser
- type AutoTLSConfig
- type CORS
- type Context
- func (ctx *Context) ACLUser() ACLUser
- func (ctx *Context) AddHeader(key string, value string)
- func (ctx *Context) Content(name string, modtime time.Time, content io.ReadSeeker)
- func (ctx *Context) DeleteHeader(key string)
- func (ctx *Context) End(status int, a ...string)
- func (ctx *Context) Error(message string, status int)
- func (ctx *Context) File(name string)
- func (ctx *Context) GetCookie(name string) (cookie *http.Cookie, err error)
- func (ctx *Context) GetValue(key string) (interface{}, bool)
- func (ctx *Context) HTML(html string)
- func (ctx *Context) IfModified(modtime time.Time, next func())
- func (ctx *Context) IfNotMatch(etag string, next func())
- func (ctx *Context) JSON(v interface{})
- func (ctx *Context) Next()
- func (ctx *Context) Ok(text string)
- func (ctx *Context) Param(name string) string
- func (ctx *Context) Redirect(url string, status int)
- func (ctx *Context) RemoteIP() string
- func (ctx *Context) RemoveCookie(cookie *http.Cookie)
- func (ctx *Context) RemoveCookieByName(name string)
- func (ctx *Context) Render(template Template, data interface{})
- func (ctx *Context) RenderHTML(html string, data interface{})
- func (ctx *Context) Session() *Session
- func (ctx *Context) SetACLUser(user ACLUser)
- func (ctx *Context) SetCookie(cookie *http.Cookie)
- func (ctx *Context) SetHeader(key string, value string)
- func (ctx *Context) StoreValue(key string, value interface{})
- func (ctx *Context) Write(p []byte) (n int, err error)
- type Form
- func (form *Form) File(key string) (multipart.File, *multipart.FileHeader, error)
- func (form *Form) Get(key string, defaultValue ...string) string
- func (form *Form) GetFloat(key string) (float64, error)
- func (form *Form) GetInt(key string) (int64, error)
- func (form *Form) Require(key string) string
- func (form *Form) RequireFloat(key string) float64
- func (form *Form) RequireInt(key string) int64
- type Handle
- func ACL(permissions ...string) Handle
- func AccessLogger(logger Logger) Handle
- func BasicAuth(auth func(name string, password string) (user ACLUser, err error)) Handle
- func BasicAuthWithRealm(realm string, ...) Handle
- func Cors(cors CORS) Handle
- func ErrorLogger(logger Logger) Handle
- func Header(key string, value string) Handle
- func JSONError() Handle
- func SIDStore(sidStore session.SIDStore) Handle
- func SendError() Handle
- func SessionPool(pool session.Pool) Handle
- type Logger
- type REST
- func (rest *REST) Delete(path string, handles ...Handle)
- func (rest *REST) Fallback(handle Handle)
- func (rest *REST) Get(path string, handles ...Handle)
- func (rest *REST) Group(path string, nest func(*REST)) *REST
- func (rest *REST) Handle(method string, path string, handles ...Handle)
- func (rest *REST) Head(path string, handles ...Handle)
- func (rest *REST) Options(path string, handles ...Handle)
- func (rest *REST) Patch(path string, handles ...Handle)
- func (rest *REST) Post(path string, handles ...Handle)
- func (rest *REST) Put(path string, handles ...Handle)
- func (rest *REST) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (rest *REST) Static(path string, root string, fallbackPath ...string)
- func (rest *REST) Trace(path string, handles ...Handle)
- func (rest *REST) Use(middlewares ...Handle)
- type ServerConfig
- type Session
- type TLSConfig
- type Template
- type URL
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Fallback ¶ added in v0.14.0
func Fallback(handle Handle)
Fallback handles the out-routed requests.
func StartAutoTLS ¶ added in v0.9.0
StartAutoTLS starts a REX server with autocert powered by Let's Encrypto SSL
Types ¶
type ACLUser ¶ added in v0.8.1
type ACLUser interface {
Permissions() []string
}
A ACLUser interface contains the Permissions method that returns the permission IDs
type AutoTLSConfig ¶ added in v0.4.0
type AutoTLSConfig struct {
AcceptTOS bool `json:"acceptTOS"`
Hosts []string `json:"hosts"`
CacheDir string `json:"cacheDir"`
Cache autocert.Cache `json:"-"`
}
AutoTLSConfig contains options to support autocert by Let's Encrypto SSL.
type CORS ¶
type CORS struct {
AllowAllOrigins bool
AllowOrigins []string
AllowMethods []string
AllowHeaders []string
ExposeHeaders []string
AllowCredentials bool
MaxAge int // in seconds
}
CORS contains options to CORS.
type Context ¶
type Context struct {
W http.ResponseWriter
R *http.Request
URL *URL
Form *Form
// contains filtered or unexported fields
}
A Context to handle http requests.
func (*Context) AddHeader ¶ added in v0.1.5
AddHeader adds the key, value pair to the header of response writer.
func (*Context) Content ¶ added in v0.9.10
Content replies to the request using the content in the provided ReadSeeker. The main benefit of ServeContent over io.Copy is that it handles Range requests properly, sets the MIME type, and handles If-Match, If-Unmodified-Since, If-None-Match, If-Modified-Since, and If-Range requests.
func (*Context) DeleteHeader ¶ added in v0.14.0
DeleteHeader deletes the values associated with key.
func (*Context) Error ¶
Error replies to the request a internal server error. if debug is enable, replies the error message.
func (*Context) File ¶ added in v0.4.0
File replies to the request with the contents of the named file or directory.
func (*Context) GetValue ¶ added in v0.11.0
GetValue returns the value stored in the values for a key, or nil if no value is present.
func (*Context) IfModified ¶ added in v0.2.1
IfModified handles caches by modified date.
func (*Context) IfNotMatch ¶ added in v0.5.0
IfNotMatch handles caches by etag.
func (*Context) JSON ¶ added in v0.7.3
func (ctx *Context) JSON(v interface{})
JSON replies to the request as a json.
func (*Context) Param ¶ added in v0.14.0
Param returns the value of the first Param which key matches the given name. If no matching Param is found, an empty string is returned.
func (*Context) Redirect ¶
Redirect replies to the request with a redirect to url, which may be a path relative to the request path.
func (*Context) RemoveCookie ¶
RemoveCookie removes the cookie.
func (*Context) RemoveCookieByName ¶ added in v0.8.0
RemoveCookieByName removes the cookie by name.
func (*Context) Render ¶ added in v0.5.0
Render applies a parsed template with the specified data object, replies to the request.
func (*Context) RenderHTML ¶ added in v0.5.8
RenderHTML applies a unparsed html template with the specified data object, replies to the request.
func (*Context) SetACLUser ¶ added in v0.9.2
SetACLUser sets the acl user
func (*Context) SetHeader ¶ added in v0.1.5
SetHeader sets the header of response writer entries associated with key to the single element value.
func (*Context) StoreValue ¶ added in v0.9.2
StoreValue sets the value for a key.
type Form ¶ added in v0.11.0
A Form to handle http form
func (*Form) Get ¶ added in v0.14.0
Get returns the first value for the named component of the POST, PATCH, or PUT request body, or returns the first value for the named component of the request url query.
func (*Form) RequireFloat ¶ added in v0.11.0
RequireFloat requires a value as float
func (*Form) RequireInt ¶ added in v0.11.0
RequireInt requires a value as int
type Handle ¶ added in v0.8.0
type Handle func(ctx *Context)
Handle defines the REST handle
func AccessLogger ¶ added in v0.13.2
AccessLogger returns a AccessLogger middleware to sets the access logger.
func BasicAuthWithRealm ¶ added in v0.8.0
func BasicAuthWithRealm(realm string, auth func(name string, password string) (user ACLUser, err error)) Handle
BasicAuthWithRealm returns a Basic HTTP Authorization middleware with realm.
func ErrorLogger ¶ added in v0.13.2
ErrorLogger returns a ErrorLogger middleware to sets the error logger.
func JSONError ¶ added in v0.13.2
func JSONError() Handle
JSONError returns a JSONError middleware to pass error as json.
func SIDStore ¶ added in v0.6.2
SIDStore returns a SIDStore middleware to sets sid store for session.
func SessionPool ¶ added in v0.9.4
SessionPool returns a SessionPool middleware to set the session pool.
type Logger ¶ added in v0.5.0
type Logger interface {
Printf(format string, v ...interface{})
}
A Logger interface contains the Printf method.
type REST ¶ added in v0.4.0
type REST struct {
// BasePath to add base path at beginning of each route path
// for example if the BasePath equals "/v2", the given route path "/path" will route "/v2/path"
BasePath string
// contains filtered or unexported fields
}
REST is REST-based router
func (*REST) Delete ¶ added in v0.4.0
Delete is a shortcut for `rest.Handle("DELETE", path, handles)`
func (*REST) Options ¶ added in v0.4.0
Options is a shortcut for `rest.Handle("OPTIONS", path, handles)`
func (*REST) ServeHTTP ¶ added in v0.5.0
func (rest *REST) ServeHTTP(w http.ResponseWriter, r *http.Request)
type ServerConfig ¶ added in v0.12.5
type ServerConfig struct {
Host string `json:"host"`
Port uint16 `json:"port"`
TLS TLSConfig `json:"tls"`
ReadTimeout uint32 `json:"readTimeout"`
WriteTimeout uint32 `json:"writeTimeout"`
MaxHeaderBytes uint32 `json:"maxHeaderBytes"`
}
ServerConfig contains options to run the REX server.
type Session ¶ added in v0.5.1
Session handles sessions for Context
type TLSConfig ¶ added in v0.9.0
type TLSConfig struct {
Port uint16 `json:"port"`
CertFile string `json:"certFile"`
KeyFile string `json:"keyFile"`
AutoTLS AutoTLSConfig `json:"autotls"`
AutoRedirect bool `json:"autoRedirect"`
}
TLSConfig contains options to support https.