rex

package module
v0.5.6 Latest Latest
Warning

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

Go to latest
Published: May 9, 2019 License: MIT Imports: 27 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) AddHeader added in v0.1.5

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

func (*Context) BasicAuthUser added in v0.5.0

func (ctx *Context) BasicAuthUser() acl.BasicAuthUser

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) FormBool

func (ctx *Context) FormBool(key string, defaultValue bool) bool

func (*Context) FormFloat added in v0.4.0

func (ctx *Context) FormFloat(key string, defaultValue float64) float64

func (*Context) FormInt

func (ctx *Context) FormInt(key string, defaultValue int64) int64

func (*Context) FormString

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

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

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

func (*Context) MustUser added in v0.4.0

func (ctx *Context) MustUser() 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(t string, data interface{})

func (*Context) Session

func (ctx *Context) Session() *Session

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) User

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

func (*Context) Write

func (ctx *Context) Write(p []byte) (n int, err error)

func (*Context) WriteString

func (ctx *Context) WriteString(s string) (n int, err error)

func (*Context) Zip added in v0.4.0

func (ctx *Context) Zip(path 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 {
	// Prefix to add base path at beginning of each route path
	// For example if the Prefix equals "v2", the route path "/path" will be "/v2/path"
	Prefix string

	// 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) SetTemplate added in v0.5.3

func (rest *REST) SetTemplate(template *template.Template)

SetTemplate sets template

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 ACL added in v0.5.1

func ACL(getUser func(ctx *Context) acl.User) 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(cors 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 SessionManager added in v0.5.0

func SessionManager(manager session.Manager) RESTHandle

func Static added in v0.5.0

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

type Session added in v0.5.1

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

func (*Session) Delete added in v0.5.1

func (s *Session) Delete(key string)

func (*Session) Flush added in v0.5.1

func (s *Session) Flush()

func (*Session) Get added in v0.5.1

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

func (*Session) Has added in v0.5.1

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

func (*Session) SID added in v0.5.1

func (s *Session) SID() string

func (*Session) Set added in v0.5.1

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

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 URL

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