rex

package module
v0.1.4 Latest Latest
Warning

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

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

README

REX

REX provides a simple & light-weight REST server in Golang that can debug, build, and host a SPA(single page appliaction).

GoDoc

Example

package main

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

func main() {
    apis := &rex.APIService{}

    apis.Get("/hello/:name", func(ctx *rex.Context) {
        ctx.WriteString("Hello, " + ctx.URL.Params.ByName("name"))
    })

    rex.Serve(&rex.ServerConfig{
        AppRoot: "/var/www/app",
        Port: 8080,
    })
}

Documentation

Overview

Package rex provides provides a REST server that can debug, build, and host a SPA(single page appliaction).

APIService

APIService provides a REST API service with user privileges.

var apis = &rex.APIService {
	Prefix: 'v2',
}

apis.Options('*', rex.PublicCORS)
apis.Head('endpoint', func(ctx *rex.Context) {}, 'privilegeId')
apis.Get('endpoint', func(ctx *rex.Context) {}, 'privilegeId')
apis.POST('endpoint', func(ctx *rex.Context) {}, 'privilegeId')
apis.Put('endpoint', func(ctx *rex.Context) {}, 'privilegeId')
apis.Patch('endpoint', func(ctx *rex.Context) {}, 'privilegeId')
apis.Delete('endpoint', func(ctx *rex.Context) {}, 'privilegeId')
apis.Use(func(ctx *rex.Context, next func()) {} )

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Serve

func Serve(config Config)

Types

type APIHandle

type APIHandle func(*Context)

type APIService

type APIService struct {
	Prefix string
	// contains filtered or unexported fields
}

func NewAPIService

func NewAPIService() *APIService

func NewAPIServiceWithPrefix

func NewAPIServiceWithPrefix(prefix string) *APIService

func (*APIService) Delete

func (s *APIService) Delete(endpoint string, handle APIHandle, privilegeIds ...string)

func (*APIService) Get

func (s *APIService) Get(endpoint string, handle APIHandle, privilegeIds ...string)

func (*APIService) Head

func (s *APIService) Head(endpoint string, handle APIHandle, privilegeIds ...string)

func (*APIService) Options

func (s *APIService) Options(endpoint string, cors *CORS)

func (*APIService) Patch

func (s *APIService) Patch(endpoint string, handle APIHandle, privilegeIds ...string)

func (*APIService) Post

func (s *APIService) Post(endpoint string, handle APIHandle, privilegeIds ...string)

func (*APIService) Put

func (s *APIService) Put(endpoint string, handle APIHandle, privilegeIds ...string)

func (*APIService) Use

func (s *APIService) Use(middleware MiddlewareHandle)

type App

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

func InitApp

func InitApp(dir string, isDebug bool) (app *App, err error)

func (*App) Build

func (app *App) Build() *AppBuildRecord

func (*App) BuildRecords

func (app *App) BuildRecords() []*AppBuildRecord

func (*App) Dir

func (app *App) Dir() string

type AppBuildRecord

type AppBuildRecord struct {
	ID        string
	PackMode  string
	Output    string
	StartTime int64
	EndTime   int64
	Error     string
}

type AppMux

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

func (*AppMux) ServeHTTP

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

type CORS

type CORS struct {
	Origin      string
	Methods     []string
	Headers     []string
	Credentials bool
	MaxAge      int // seconds
}

func PublicCORS

func PublicCORS() *CORS

type Config

type Config struct {
	Port              uint16            `json:"port"`
	AppDir            string            `json:"appDir"`
	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"`
	Debug             bool              `json:"debug"`
	NotFoundHandler   http.Handler      `json:"-"`
	ErrorLogger       *log.Logger       `json:"-"`
	AccessLogger      *log.Logger       `json:"-"`
}

type Context

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

func (*Context) Authenticate

func (ctx *Context) Authenticate(realm string, authHandle func(user string, password string) (ok bool, err error)) (ok bool, err error)

func (*Context) Cookie

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

func (*Context) End

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

func (*Context) Error

func (ctx *Context) Error(err error)

func (*Context) FormBool

func (ctx *Context) FormBool(key string) (b bool)

func (*Context) FormInt

func (ctx *Context) FormInt(key string) (i int, err error)

func (*Context) FormJSON

func (ctx *Context) FormJSON(key string) (value map[string]interface{}, err error)

func (*Context) FormNumber

func (ctx *Context) FormNumber(key string) (n float64, err error)

func (*Context) FormString

func (ctx *Context) FormString(key string) (value string)

func (*Context) FormValues

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

func (*Context) ParseMultipartForm

func (ctx *Context) ParseMultipartForm(maxMemoryBytes int64)

func (*Context) Redirect

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

func (*Context) RemoteIP

func (ctx *Context) RemoteIP() (ip 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) 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) WriteJSON

func (ctx *Context) WriteJSON(data interface{}) (n int, err error)

func (*Context) WriteStatusJSON

func (ctx *Context) WriteStatusJSON(status int, data interface{}) (n int, err error)

func (*Context) WriteString

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

type MiddlewareHandle

type MiddlewareHandle func(*Context, func())

type Mux

type Mux struct {
	App               *App
	Debug             bool
	ServerName        string
	CustomHTTPHeaders map[string]string
	SessionCookieName string
	HostRedirectRule  string
	NotFoundHandler   http.Handler
	SessionManager    session.Manager
	AccessLogger      *log.Logger
	Logger            *log.Logger
	// contains filtered or unexported fields
}

func (*Mux) RegisterAPIService

func (mux *Mux) RegisterAPIService(apis *APIService)

func (*Mux) ServeHTTP

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

type ResponseWriter

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

func (*ResponseWriter) Header

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

func (*ResponseWriter) Status

func (w *ResponseWriter) Status() (status int, writedBytes int)

func (*ResponseWriter) Write

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

func (*ResponseWriter) WriteHeader

func (w *ResponseWriter) WriteHeader(status int)

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) Add added in v0.1.4

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

func (*State) Del added in v0.1.4

func (s *State) Del(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
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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