rex

package module
v0.7.1 Latest Latest
Warning

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

Go to latest
Published: May 23, 2019 License: MIT Imports: 28 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() {
	rest := rex.New()

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

	rex.Serve(rex.Config{
		Port: 8080,
	})
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Serve

func Serve(config Config)

Serve serves the rex server

Types

type AutoTLSConfig added in v0.4.0

type AutoTLSConfig struct {
	Enable   bool           `json:"enable"`
	Hosts    []string       `json:"hosts"`
	CacheDir string         `json:"cacheDir"`
	Cache    autocert.Cache `json:"-"`
}

type CORSOptions added in v0.5.0

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

func PublicCORS

func PublicCORS() CORSOptions

type Config

type Config struct {
	Port           uint16      `json:"port"`
	HTTPS          HTTPSConfig `json:"https"`
	ReadTimeout    uint32      `json:"readTimeout"`
	WriteTimeout   uint32      `json:"writeTimeout"`
	MaxHeaderBytes uint32      `json:"maxHeaderBytes"`
	Debug          bool        `json:"debug"`
	Logger         Logger      `json:"-"`
	AccessLogger   Logger      `json:"-"`
}

type Context

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

func (*Context) ACLUser added in v0.6.0

func (ctx *Context) ACLUser() acl.User

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() acl.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) 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) Html added in v0.4.0

func (ctx *Context) Html(html []byte)

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.4.0

func (ctx *Context) Json(status int, v interface{})

func (*Context) MustACLUser added in v0.6.0

func (ctx *Context) MustACLUser() acl.User

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(status int, url string)

func (*Context) RemoteIP

func (ctx *Context) RemoteIP() string

func (*Context) RemoveCookie

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

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(text string, data interface{})

func (*Context) Session

func (ctx *Context) Session() *ContextSession

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) 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 CookieSIDtore added in v0.6.2

type CookieSIDtore struct {
	CookieName string
}

func (*CookieSIDtore) Get added in v0.6.2

func (s *CookieSIDtore) Get(ctx *Context) string

func (*CookieSIDtore) Set added in v0.6.2

func (s *CookieSIDtore) Set(ctx *Context, sid string)

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 HTTPSConfig added in v0.3.0

type HTTPSConfig struct {
	Port     uint16        `json:"port"`
	CertFile string        `json:"certFile"`
	KeyFile  string        `json:"keyFile"`
	AutoTLS  AutoTLSConfig `json:"autotls"`
}

type Logger added in v0.5.0

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

type REST added in v0.4.0

type REST struct {

	// Logger to log accesses
	AccessLogger Logger

	// Logger to log  errors
	Logger Logger

	// If enabled, errors will be sent to the client.
	// 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(prefix ...string) *REST

New returns a new REST

func (*REST) Delete added in v0.4.0

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

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 ...RESTHandle)

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

func (*REST) Handle added in v0.4.0

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

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 ...RESTHandle)

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

func (*REST) NotFound added in v0.5.0

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

func (*REST) Options added in v0.4.0

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

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 ...RESTHandle)

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 ...RESTHandle)

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

func (*REST) Put added in v0.4.0

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

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 ...RESTHandle)

Use injects middlewares to REST

type RESTHandle added in v0.4.0

type RESTHandle func(ctx *Context)

RESTHandle defines the handle of route or middleware of REST

func ACLAuth added in v0.5.8

func ACLAuth(getUserFunc func(ctx *Context) (acl.User, error)) RESTHandle

func BasicAuth added in v0.4.0

func BasicAuth(realm string, check func(name string, password string) (ok bool, err error)) RESTHandle

BasicAuth returns a Basic HTTP Authorization middleware.

func CORS

func CORS(opts CORSOptions) RESTHandle

func HTTPS added in v0.5.0

func HTTPS() RESTHandle
func Header(key string, value string) RESTHandle

func Privileges added in v0.5.0

func Privileges(privileges ...string) RESTHandle

func Session added in v0.5.1

func Session(manager SessionManager) RESTHandle

func Static added in v0.5.0

func Static(root string, fallbackPaths ...string) RESTHandle

type SIDStore added in v0.6.2

type SIDStore interface {
	Get(ctx *Context) string
	Set(ctx *Context, sid string)
}

type SessionManager added in v0.5.0

type SessionManager struct {
	SIDStore SIDStore
	Pool     session.Pool
}

type State added in v0.1.4

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

func NewState added in v0.1.4

func NewState() *State

func (*State) Delete added in v0.3.0

func (s *State) Delete(key string)

func (*State) Get added in v0.1.4

func (s *State) Get(key string) (v interface{})

func (*State) Has added in v0.5.0

func (s *State) Has(key string) (ok bool)

func (*State) Set added in v0.1.4

func (s *State) Set(key string, v interface{})

type Template added in v0.5.8

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

type URL

type URL struct {
	Params router.Params
	*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