rex

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2019 License: MIT Imports: 25 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"`
	CacheDir string   `json:"cacheDir"`
	CacheURL string   `json:"cacheUrl"`
	Hosts    []string `json:"hosts"`
}

type CORSHeaders added in v0.4.0

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

func PublicCORS

func PublicCORS() *CORSHeaders

func (*CORSHeaders) Apply added in v0.4.0

func (cors *CORSHeaders) Apply(w http.ResponseWriter)

type Config

type Config struct {
	Debug             bool              `json:"debug"`
	Port              uint16            `json:"port"`
	HTTPS             HTTPSConfig       `json:"https"`
	Root              string            `json:"root"`
	ServerName        string            `json:"serverName"`
	CustomHTTPHeaders map[string]string `json:"customHTTPHeaders"`
	SessionCookieName string            `json:"sessionCookieName"`
	HostRedirectRule  string            `json:"hostRedirectRule"`
	ReadTimeout       uint32            `json:"readTimeout"`
	WriteTimeout      uint32            `json:"writeTimeout"`
	MaxHeaderBytes    uint32            `json:"maxHeaderBytes"`
	NotFoundHandler   http.Handler      `json:"-"`
	SessionManager    session.Manager   `json:"-"`
	Logger            *log.Logger       `json:"-"`
	AccessLogger      *log.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) 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(filepath string)

func (*Context) FormArray added in v0.4.0

func (ctx *Context) FormArray(key string) (a []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) 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) 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) Session

func (ctx *Context) Session() (sess 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) SetUser

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

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 Mux

type Mux struct {
	Config
	// contains filtered or unexported fields
}

Mux is a HTTP request multiplexer. The inner router provides by github.com/julienschmidt/httprouter.

func (*Mux) RegisterREST added in v0.4.0

func (mux *Mux) RegisterREST(rest *REST)

RegisterREST registers a REST instacce

func (*Mux) ServeHTTP

func (mux *Mux) ServeHTTP(w http.ResponseWriter, r *http.Request)

type REST added in v0.4.0

type REST struct {
	Prefix   string
	IsGlobal bool
	// contains filtered or unexported fields
}

func New added in v0.4.0

func New(prefixs ...string) *REST

func (*REST) Delete added in v0.4.0

func (s *REST) Delete(endpoint string, handles ...RESTHandle)

func (*REST) Get added in v0.4.0

func (s *REST) Get(endpoint string, handles ...RESTHandle)

func (*REST) Handle added in v0.4.0

func (s *REST) Handle(method string, endpoint string, handles []RESTHandle)

func (*REST) Head added in v0.4.0

func (s *REST) Head(endpoint string, handles ...RESTHandle)

func (*REST) Options added in v0.4.0

func (s *REST) Options(endpoint string, handles ...RESTHandle)

func (*REST) Patch added in v0.4.0

func (s *REST) Patch(endpoint string, handles ...RESTHandle)

func (*REST) Post added in v0.4.0

func (s *REST) Post(endpoint string, handles ...RESTHandle)

func (*REST) Put added in v0.4.0

func (s *REST) Put(endpoint string, handles ...RESTHandle)

func (*REST) Use added in v0.4.0

func (s *REST) Use(middlewares ...RESTHandle)

type RESTHandle added in v0.4.0

type RESTHandle func(ctx *Context)

func BasicAuth added in v0.4.0

func BasicAuth(realm string, check func(user string, pass string) (ok bool, err error)) RESTHandle

func CORS

func CORS(cors *CORSHeaders) RESTHandle

func SetPrivileges added in v0.4.0

func SetPrivileges(privileges ...string) RESTHandle

type ResponseWriter

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

A ResponseWriter is used by an rex mux to construct an HTTP response.

func (*ResponseWriter) Header

func (w *ResponseWriter) Header() http.Header

Header returns the header map that will be sent by WriteHeader.

func (*ResponseWriter) Hijack added in v0.2.3

func (w *ResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error)

Hijack lets the caller take over the connection.

func (*ResponseWriter) Write

func (w *ResponseWriter) Write(p []byte) (n int, err error)

Write writes the data to the connection as part of an HTTP reply.

func (*ResponseWriter) WriteHeader

func (w *ResponseWriter) WriteHeader(status int)

WriteHeader sends an HTTP response header with the provided status code.

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) 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
basic command
hello command

Jump to

Keyboard shortcuts

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