rex

package module
v0.9.3 Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2019 License: MIT Imports: 26 Imported by: 1

README

REX

REX provides a simple & light-weight REST server in Golang.

GoDoc

Example
package main

import (
	"github.com/ije/rex"
)

func main() {
	rex.Get("/hello/:name", func(ctx *rex.Context) {
		ctx.Ok("Hello, " + ctx.URL.Param("name"))
	})
	
	rex.Start(8080)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Delete added in v0.9.0

func Delete(path string, handles ...Handle)

Delete is a shortcut for router.Handle("DELETE", path, handles)

func Get added in v0.9.0

func Get(path string, handles ...Handle)

Get is a shortcut for router.Handle("GET", path, handles)

func Group added in v0.9.3

func Group(prefix string, callback func(*REST))

Group creates a nested REST

func Head(path string, handles ...Handle)

Head is a shortcut for router.Handle("HEAD", path, handles)

func NotFound added in v0.9.0

func NotFound(handles ...Handle)

NotFound handles the requests that are not routed

func Options added in v0.9.0

func Options(path string, handles ...Handle)

Options is a shortcut for router.Handle("OPTIONS", path, handles)

func Patch added in v0.9.0

func Patch(path string, handles ...Handle)

Patch is a shortcut for router.Handle("PATCH", path, handles)

func Post added in v0.9.0

func Post(path string, handles ...Handle)

Post is a shortcut for router.Handle("POST", path, handles)

func Put added in v0.9.0

func Put(path string, handles ...Handle)

Put is a shortcut for router.Handle("PUT", path, handles)

func Serve

func Serve(config Config)

Serve serves the rex server

func Start added in v0.9.0

func Start(port uint16)

Start starts an HTTP server.

func StartAutoTLS added in v0.9.0

func StartAutoTLS(port uint16, hosts ...string)

StartAutoTLS starts an HTTPS server using autocert with Let's Encrypto SSL

func StartTLS added in v0.9.0

func StartTLS(port uint16, certFile string, keyFile string)

StartTLS starts an HTTPS server.

func Use added in v0.9.0

func Use(middlewares ...Handle)

Use appends middleware to the REST middleware stack.

func UseSessionPool added in v0.9.3

func UseSessionPool(pool session.Pool)

UseSessionPool sets a SessionPool middleware.

func UseSessionSIDStore added in v0.9.3

func UseSessionSIDStore(sidStore session.SIDStore)

UseSessionSIDStore sets a SessionSIDStore middleware.

Types

type ACLUser added in v0.8.1

type ACLUser interface {
	Permissions() []string
}

A ACLUser contains a Permissions method that returns the acl permission id list

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 the options to support autocert by Let's Encrypto SSL.

type BasicUser added in v0.8.1

type BasicUser struct {
	Name     string
	Password string
}

BasicUser represents a http Basic-Auth user that contains the username and password

type CORSOptions added in v0.5.0

type CORSOptions struct {
	AllowOrigin      string
	AllowMethods     []string
	AllowHeaders     []string
	ExposeHeaders    []string
	AllowCredentials bool
	MaxAge           int // in seconds
}

CORSOptions contains the options to CORS.

type Config

type Config struct {
	Port           uint16    `json:"port"`
	TLS            TLSConfig `json:"tls"`
	ReadTimeout    uint32    `json:"readTimeout"`
	WriteTimeout   uint32    `json:"writeTimeout"`
	MaxHeaderBytes uint32    `json:"maxHeaderBytes"`
	Logger         Logger    `json:"-"`
	AccessLogger   Logger    `json:"-"`
}

Config contains the options to run REX server.

type Context

type Context struct {
	W   http.ResponseWriter
	R   *http.Request
	URL *URL
	// contains filtered or unexported fields
}

func (*Context) ACLUser added in v0.6.0

func (ctx *Context) ACLUser() ACLUser

func (*Context) AddHeader added in v0.1.5

func (ctx *Context) AddHeader(key string, value string)

func (*Context) BasicUser added in v0.4.0

func (ctx *Context) BasicUser() BasicUser

func (*Context) End

func (ctx *Context) End(status int, a ...string)

func (*Context) Error

func (ctx *Context) Error(err error)

func (*Context) File added in v0.4.0

func (ctx *Context) File(filename string)

func (*Context) FormFile added in v0.7.5

func (ctx *Context) FormFile(key string) (multipart.File, *multipart.FileHeader, error)

func (*Context) FormString

func (ctx *Context) FormString(key string, defaultValue ...string) string

func (*Context) FormValue added in v0.6.1

func (ctx *Context) FormValue(key string, defaultValue ...string) FormValue

func (*Context) FormValues

func (ctx *Context) FormValues(key string) (a []string)

func (*Context) GetCookie added in v0.1.9

func (ctx *Context) GetCookie(name string) (cookie *http.Cookie, err error)

func (*Context) GetHeader added in v0.8.0

func (ctx *Context) GetHeader(key string) string

func (*Context) HTML added in v0.7.3

func (ctx *Context) HTML(html string)

func (*Context) IfModified added in v0.2.1

func (ctx *Context) IfModified(modtime time.Time, then func())

func (*Context) IfNotMatch added in v0.5.0

func (ctx *Context) IfNotMatch(etag string, then func())

func (*Context) JSON added in v0.7.3

func (ctx *Context) JSON(v interface{})

func (*Context) JSONError added in v0.7.4

func (ctx *Context) JSONError(err error)

func (*Context) Next added in v0.4.0

func (ctx *Context) Next()

func (*Context) Ok added in v0.4.0

func (ctx *Context) Ok(text string)

func (*Context) ParseMultipartForm

func (ctx *Context) ParseMultipartForm(maxMemoryBytes int64)

func (*Context) Redirect

func (ctx *Context) Redirect(url string, status int)

func (*Context) RemoteIP

func (ctx *Context) RemoteIP() string

func (*Context) RemoveCookie

func (ctx *Context) RemoveCookie(cookie *http.Cookie)

func (*Context) RemoveCookieByName added in v0.8.0

func (ctx *Context) RemoveCookieByName(name string)

func (*Context) Render added in v0.5.0

func (ctx *Context) Render(template Template, data interface{})

func (*Context) RenderHTML added in v0.5.8

func (ctx *Context) RenderHTML(html string, data interface{})

func (*Context) Session

func (ctx *Context) Session() *ContextSession

func (*Context) SetACLUser added in v0.9.2

func (ctx *Context) SetACLUser(user ACLUser)

func (*Context) SetCookie

func (ctx *Context) SetCookie(cookie *http.Cookie)

func (*Context) SetHeader added in v0.1.5

func (ctx *Context) SetHeader(key string, value string)

func (*Context) StoreValue added in v0.9.2

func (ctx *Context) StoreValue(key string, value interface{})

func (*Context) Value added in v0.9.2

func (ctx *Context) Value(key string) (value interface{}, ok bool)

func (*Context) Zip added in v0.4.0

func (ctx *Context) Zip(path string)

type ContextSession added in v0.6.1

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

func (*ContextSession) Delete added in v0.6.1

func (s *ContextSession) Delete(key string)

func (*ContextSession) Flush added in v0.6.1

func (s *ContextSession) Flush()

func (*ContextSession) Get added in v0.6.1

func (s *ContextSession) Get(key string) interface{}

func (*ContextSession) Has added in v0.6.1

func (s *ContextSession) Has(key string) bool

func (*ContextSession) SID added in v0.6.1

func (s *ContextSession) SID() string

func (*ContextSession) Set added in v0.6.1

func (s *ContextSession) Set(key string, value interface{})

type FormValue added in v0.6.1

type FormValue string

func (FormValue) Bool added in v0.6.1

func (value FormValue) Bool() bool

func (FormValue) Float64 added in v0.6.1

func (value FormValue) Float64() (float64, error)

func (FormValue) Int64 added in v0.6.1

func (value FormValue) Int64() (int64, error)

func (FormValue) String added in v0.6.1

func (value FormValue) String() string

type Handle added in v0.8.0

type Handle func(ctx *Context)

Handle defines a function to handle route requests.

func ACL added in v0.5.1

func ACL(permissions ...string) Handle

ACL returns a ACL middleware.

func BasicAuth added in v0.4.0

func BasicAuth(authFunc func(name string, password string) (ok bool, err error)) Handle

BasicAuth returns a Basic HTTP Authorization middleware.

func BasicAuthWithRealm added in v0.8.0

func BasicAuthWithRealm(realm string, authFunc func(name string, password string) (ok bool, err error)) Handle

BasicAuthWithRealm returns a Basic HTTP Authorization middleware with realm.

func CORS

func CORS(opts CORSOptions) Handle

CORS returns a CORS middleware.

func Header(key string, value string) Handle

Header is REX middleware to set http header

func Static added in v0.5.0

func Static(root string, fallbackPath ...string) Handle

Static returns a file static serve middleware.

type InvalidError added in v0.7.4

type InvalidError struct {
	Code    int
	Message string
}

InvalidError is an error with code

func Invalid added in v0.7.4

func Invalid(code int, message string) *InvalidError

Invalid returns a new InvalidError

func (*InvalidError) Error added in v0.7.4

func (err *InvalidError) Error() string

Error implements the error type

type Logger added in v0.5.0

type Logger interface {
	Println(v ...interface{})
	Printf(format string, v ...interface{})
}

Logger is a Logger contains Println and Printf methods

type REST added in v0.4.0

type REST struct {

	// Logger to log requests
	AccessLogger Logger

	// Logger to log errors
	Logger Logger

	// If enabled, errors will be sent to the client/browser,
	// this should be disable in production.
	SendError bool
	// contains filtered or unexported fields
}

REST is a http Handler which contains the router, middlewares and configuration settings

func New added in v0.4.0

func New() *REST

New returns a new REST

func (*REST) Delete added in v0.4.0

func (rest *REST) Delete(path string, handles ...Handle)

Delete is a shortcut for router.Handle("DELETE", path, handles)

func (*REST) Get added in v0.4.0

func (rest *REST) Get(path string, handles ...Handle)

Get is a shortcut for router.Handle("GET", path, handles)

func (*REST) Group added in v0.9.0

func (rest *REST) Group(prefix string, callback func(*REST))

Group creates a nested REST

func (*REST) Handle added in v0.4.0

func (rest *REST) Handle(method string, path string, handles ...Handle)

Handle registers a new request handle with the given method and path.

func (*REST) Head added in v0.4.0

func (rest *REST) Head(path string, handles ...Handle)

Head is a shortcut for router.Handle("HEAD", path, handles)

func (*REST) Host added in v0.9.1

func (rest *REST) Host(host string) *REST

Host returns a nested REST with host

func (*REST) NotFound added in v0.5.0

func (rest *REST) NotFound(handles ...Handle)

NotFound handles the requests that are not routed

func (*REST) Options added in v0.4.0

func (rest *REST) Options(path string, handles ...Handle)

Options is a shortcut for router.Handle("OPTIONS", path, handles)

func (*REST) Patch added in v0.4.0

func (rest *REST) Patch(path string, handles ...Handle)

Patch is a shortcut for router.Handle("PATCH", path, handles)

func (*REST) Post added in v0.4.0

func (rest *REST) Post(path string, handles ...Handle)

Post is a shortcut for router.Handle("POST", path, handles)

func (*REST) Prefix added in v0.4.0

func (rest *REST) Prefix(prefix string) *REST

Prefix returns a nested REST with prefix

func (*REST) Put added in v0.4.0

func (rest *REST) Put(path string, handles ...Handle)

Put is a shortcut for router.Handle("PUT", path, handles)

func (*REST) ServeHTTP added in v0.5.0

func (rest *REST) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements the http Handler interface.

func (*REST) Use added in v0.4.0

func (rest *REST) Use(middlewares ...Handle)

Use appends middleware to the REST middleware stack.

func (*REST) UseSessionPool added in v0.9.3

func (rest *REST) UseSessionPool(pool session.Pool)

UseSessionPool sets a SessionPool middleware.

func (*REST) UseSessionSIDStore added in v0.9.3

func (rest *REST) UseSessionSIDStore(sidStore session.SIDStore)

UseSessionSIDStore sets a SessionSIDStore middleware.

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 the options to support https.

type Template added in v0.5.8

type Template interface {
	Execute(wr io.Writer, data interface{}) error
}

Template is a interface contains Execute method

type URL

type URL struct {
	Params    router.Params
	RoutePath string
	*url.URL
}

A URL is a *url.URL with httprouter.Params

func (*URL) Param added in v0.4.0

func (url *URL) Param(name string) string

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.

Directories

Path Synopsis
examples
acl-auth command
basic-auth command
group-router command
hello command
json command
not-found command
session command
static command
zip command

Jump to

Keyboard shortcuts

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