biu

package module
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: May 10, 2018 License: LGPL-3.0 Imports: 33 Imported by: 0

README

BIU

a set of toolkits for go-restful.

GitHub release GoDoc Go Report Card

Installation

go get -u github.com/tuotoo/biu

Examples

pkg-examples

Contributing

All our projects follow the GitFlow branching model, from development to release. If you are not familiar with it, there are several guides and tutorials to make you understand what it is about.

Thanks

Documentation

Overview

Example
package main

import (
	"github.com/emicklei/go-restful"
	"github.com/tuotoo/biu"
)

// Foo controller
type Foo struct{}

// WebService implements CtlInterface
func (ctl Foo) WebService(ws biu.WS) {
	ws.Route(ws.GET("/").Doc("Get Bar").
		Param(ws.QueryParameter("num", "number").DataType("integer")).
		DefaultReturns("Bar", Bar{}), &biu.RouteOpt{
		ID: "2F996F8F-9D08-4BE0-9D4D-ACB328D8F387",
		To: ctl.getBar,
		Errors: map[int]string{
			100: "num not Number",
		},
	})

	// add more routes as you like:
	// ws.Route(ws.POST("/foo"),nil)
	// ...
}

// Bar is the response of getBar
type Bar struct {
	Msg string `json:"msg"`
	Num int    `json:"num"`
}

func (ctl Foo) getBar(ctx biu.Ctx) {
	num, err := ctx.Query("num").Int()
	ctx.Must(err, 100)

	ctx.ResponseJSON(Bar{Msg: "bar", Num: num})
}

func main() {
	restful.Filter(biu.LogFilter())
	biu.AddServices("/v1", nil,
		biu.NS{
			NameSpace:  "foo",
			Controller: Foo{},
			Desc:       "Foo Controller",
		},
	)
	// Note: you should add swagger service after adding services.
	// swagger document will be available at http://localhost:8080/v1/swagger
	swaggerService := biu.NewSwaggerService(biu.SwaggerInfo{
		Title:        "Foo Bar",
		Description:  "Foo Bar Service",
		ContactName:  "Tuotoo",
		ContactEmail: "jqs7@tuotoo.com",
		ContactURL:   "https://tuotoo.com",
		Version:      "1.0.0",
		RoutePrefix:  "/v1",
	})
	restful.Add(swaggerService)
	biu.Run(":8080", nil)
}
Output:

Index

Examples

Constants

View Source
const (
	// MIME_HTML_FORM is application/x-www-form-urlencoded header
	MIME_HTML_FORM = "application/x-www-form-urlencoded"
	// MIME_FILE_FORM is multipart/form-data
	MIME_FILE_FORM = "multipart/form-data"
)

Variables

View Source
var CommonResponse = func(w http.ResponseWriter,
	routeID string, code int, message string, data interface{}) {
	if err := writeJSON(w, http.StatusOK, CommonResp{
		Code:    code,
		Message: message,
		Data:    data,
		RouteID: routeID,
	}); err != nil {
		Warn().Err(err).Msg("json encode")
	}
}

CommonResponse is a response func. just replace it if you'd like to custom response.

View Source
var DisableErrHandler bool

DisableErrHandler disables error handler using errc.

Functions

func AddServices

func AddServices(prefix string, opt *GlobalServiceOpt, wss ...NS)

AddServices adds services with namespace.

func AuthFilter

func AuthFilter(code int) restful.FilterFunction

AuthFilter checks if request contains JWT, and sets UserID in Attribute if exists,

func CheckError

func CheckError(err error, log *LogWrap) bool

CheckError is a convenience method to check error is nil. If error is nil, it will return true, else it will log the error and return false

func CheckToken added in v0.1.0

func CheckToken(token string) (userID string, err error)

CheckToken accept a jwt token and returns the uid in token.

func ContainsError

func ContainsError(w http.ResponseWriter, RouteSignature string, err error, code int) bool

ContainsError is a convenience method to check error is nil. If error is nil, it will return false, else it will log the error, make a CommonResp response and return true. if code is 0, it will use err.Error() as CommonResp.message.

func Filter

func Filter(f func(ctx Ctx)) restful.FilterFunction

Filter transform a biu handler to a restful.FilterFunction

func Handle

func Handle(f func(ctx Ctx)) restful.RouteFunction

Handle transform a biu handler to a restful.RouteFunction.

func LogFilter

func LogFilter() restful.FilterFunction

LogFilter logs

{
	remote_addr,
	method,
	uri,
	proto,
	status_code,
	content_length,
}

for each request

func NewSwaggerService

func NewSwaggerService(info SwaggerInfo) *restful.WebService

NewSwaggerService creates a swagger webservice in /swagger

func ParseToken added in v0.1.0

func ParseToken(token string) (*jwt.Token, error)

ParseToken parse a token string.

func RefreshToken added in v0.1.0

func RefreshToken(token string) (newToken string, err error)

RefreshToken accepts a valid token and returns a new token with new expire time.

func ResponseError

func ResponseError(w http.ResponseWriter, routeID string, msg string, code int)

ResponseError is a convenience method to response an error code and message. It uses jsoniter for marshalling the value.

func ResponseJSON

func ResponseJSON(w http.ResponseWriter, routeID string, v interface{})

ResponseJSON is a convenience method for writing a value wrap in CommonResp as JSON. It uses jsoniter for marshalling the value.

func Run

func Run(addr string, cfg *RunConfig)

Run starts up a web server with default container.

func SetLoggerLevel

func SetLoggerLevel(level zerolog.Level)

SetLoggerLevel sets the log level of logger.

func SetLoggerOutput

func SetLoggerOutput(w io.Writer)

SetLoggerOutput sets the output of logger.

func Sign

func Sign(userID string) (token string, err error)

Sign returns a signed jwt string.

Example
biu.JWTTimeout(4 * time.Second).
	JWTSecret(func(userID string) (secret []byte, err error) {
		return []byte("hello world"), nil
	}).
	JWTRefreshTimeout(5 * time.Second)
token, _ := biu.Sign("user")
ctx := &biu.Ctx{
	Request: &restful.Request{
		Request: &http.Request{
			Header: map[string][]string{
				"Authorization": {token},
			},
		},
	},
}
u1, err := ctx.IsLogin()
if err != nil {
	panic(err)
}
fmt.Println(u1)
u2, err := biu.CheckToken(token)
if err != nil {
	panic(err)
}
fmt.Println(u2)
time.Sleep(time.Second * 2)
newToken, err := biu.RefreshToken(token)
if err != nil {
	panic(err)
}
_, err = biu.CheckToken(newToken)
if err != nil {
	panic(err)
}

time.Sleep(time.Second * 3)
// token is expired, newToken is still valid
_, err = ctx.IsLogin()
fmt.Println(err != nil)
_, err = biu.CheckToken(token)
fmt.Println(err != nil)
_, err = biu.CheckToken(newToken)
if err != nil {
	panic(err)
}
time.Sleep(time.Second)
// cant refresh token if refresh timeout is reached
_, err = biu.RefreshToken(newToken)
fmt.Println(err != nil)

ctx2 := &biu.Ctx{
	Request: &restful.Request{
		Request: &http.Request{
			Header: map[string][]string{
				"Authorization": {"wtf"},
			},
		},
	},
}
_, err = ctx2.IsLogin()
fmt.Println(err != nil)
Output:

user
user
true
true
true
true

func UseColorLogger added in v0.3.0

func UseColorLogger()

func UseConsoleLogger added in v0.3.1

func UseConsoleLogger()

func WrapHandler

func WrapHandler(f func(ctx Ctx)) http.HandlerFunc

WrapHandler wraps a biu handler to http.HandlerFunc

Types

type ColorWriter added in v0.3.0

type ColorWriter struct {
	WithColor bool
}

func (ColorWriter) Write added in v0.3.0

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

type CommonResp

type CommonResp struct {
	Code    int         `json:"code"`
	Message string      `json:"message"`
	Data    interface{} `json:"data"`
	RouteID string      `json:"route_id,omitempty"`
}

CommonResp with code, message and data

type Container

type Container struct{ *restful.Container }

Container of restful

func New

func New() Container

New creates a new restful container.

func (*Container) AddServices

func (c *Container) AddServices(prefix string, opt *GlobalServiceOpt, wss ...NS)

AddServices adds services with namespace for container.

func (*Container) NewSwaggerService

func (c *Container) NewSwaggerService(info SwaggerInfo) *restful.WebService

NewSwaggerService creates a swagger webservice in /swagger

func (*Container) NewTestServer added in v0.2.0

func (c *Container) NewTestServer() *TestServer

NewTestServer returns a Test Server.

func (*Container) Run

func (c *Container) Run(addr string, cfg *RunConfig)

Run starts up a web server for container.

type CtlInterface

type CtlInterface interface {
	WebService(WS)
}

CtlInterface is the interface of controllers

type Ctx

type Ctx struct {
	*restful.Request
	*restful.Response
	*restful.FilterChain
	ErrCatcher errc.Catcher
}

Ctx wrap *restful.Request and *restful.Response in one struct.

func (*Ctx) Bind added in v0.2.1

func (ctx *Ctx) Bind(obj interface{}) error

Bind checks the Content-Type to select a binding engine automatically, Depending the "Content-Type" header different bindings are used:

"application/json" --> JSON binding
"application/xml"  --> XML binding

otherwise --> returns an error. It parses the request's body as JSON if Content-Type == "application/json" using JSON or XML as a JSON input. It decodes the json payload into the struct specified as a pointer. It writes a 400 error and sets Content-Type header "text/plain" in the response if input is not valid.

func (*Ctx) BindJSON added in v0.2.1

func (ctx *Ctx) BindJSON(obj interface{}) error

BindJSON is a shortcut for ctx.BindWith(obj, binding.JSON).

func (*Ctx) BindQuery added in v0.2.1

func (ctx *Ctx) BindQuery(obj interface{}) error

BindQuery is a shortcut for ctx.BindWith(obj, binding.Query).

func (*Ctx) BindWith added in v0.2.1

func (ctx *Ctx) BindWith(obj interface{}, b binding.Binding) error

BindWith binds the passed struct pointer using the specified binding engine. See the binding package.

func (*Ctx) BodyParameterValues added in v0.2.0

func (ctx *Ctx) BodyParameterValues(name string) ([]string, error)

BodyParameterValues returns the array of parameter in a POST form body.

func (*Ctx) ContainsError

func (ctx *Ctx) ContainsError(err error, code int, v ...interface{}) bool

ContainsError is a convenience method to check error is nil. If error is nil, it will return false, else it will log the error, make a CommonResp response and return true. if code is 0, it will use err.Error() as CommonResp.message.

func (*Ctx) ErrMsg added in v0.2.0

func (ctx *Ctx) ErrMsg(code int) string

ErrMsg returns the message of a error code in current route.

func (*Ctx) Form added in v0.2.1

func (ctx *Ctx) Form(name string) Parameter

Form reads form parameter with name.

func (*Ctx) Header added in v0.2.1

func (ctx *Ctx) Header(name string) Parameter

Header reads header parameter with name.

func (*Ctx) IP added in v0.2.0

func (ctx *Ctx) IP() string

IP returns the IP address of request.

func (*Ctx) IsLogin

func (ctx *Ctx) IsLogin() (userID string, err error)

IsLogin gets JWT token in request by OAuth2Extractor, and parse it with CheckToken.

func (*Ctx) Must added in v0.3.0

func (ctx *Ctx) Must(err error, code int, v ...interface{})

Must causes a return from a function if err is not nil.

func (*Ctx) MustBind added in v0.3.0

func (ctx *Ctx) MustBind(obj interface{}, code int, v ...interface{})

MustBind is a shortcur for ctx.Must(ctx.Bind(obj), code, v...)

func (*Ctx) MustBindJSON added in v0.3.0

func (ctx *Ctx) MustBindJSON(obj interface{}, code int, v ...interface{})

MustBindJSON is a shortcur for ctx.Must(ctx.BindJSON(obj), code, v...)

func (*Ctx) MustBindQuery added in v0.3.0

func (ctx *Ctx) MustBindQuery(obj interface{}, code int, v ...interface{})

MustBindQuery is a shortcur for ctx.Must(ctx.BindQuery(obj), code, v...)

func (*Ctx) MustBindWith added in v0.3.0

func (ctx *Ctx) MustBindWith(obj interface{}, b binding.Binding, code int, v ...interface{})

MustBindWith is a shortcur for ctx.Must(ctx.BindWith(obj, b), code, v...)

func (*Ctx) Path added in v0.2.1

func (ctx *Ctx) Path(name string) Parameter

Path reads path parameter with name.

func (*Ctx) Proxy added in v0.2.0

func (ctx *Ctx) Proxy() []string

Proxy returns the proxy endpoints behind a request.

func (*Ctx) Query added in v0.2.1

func (ctx *Ctx) Query(name string) Parameter

Query reads query parameter with name.

func (*Ctx) Redirect added in v0.2.1

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

Redirect replies to the request with a redirect to url.

func (*Ctx) ResponseError

func (ctx *Ctx) ResponseError(msg string, code int)

ResponseError is a convenience method to response an error code and message. It uses jsoniter for marshalling the value.

func (*Ctx) ResponseJSON

func (ctx *Ctx) ResponseJSON(v interface{})

ResponseJSON is a convenience method for writing a value wrap in CommonResp as JSON. It uses jsoniter for marshalling the value.

func (*Ctx) ResponseStdErrCode

func (ctx *Ctx) ResponseStdErrCode(code int, v ...interface{})

ResponseStdErrCode is a convenience method response a code with msg in Code Desc.

func (*Ctx) RouteID added in v0.2.0

func (ctx *Ctx) RouteID() string

RouteID returns the RouteID of current route.

func (*Ctx) RouteSignature added in v0.2.0

func (ctx *Ctx) RouteSignature() string

RouteSignature returns the signature of current route. Example: /v1/user/login POST

func (*Ctx) UserID

func (ctx *Ctx) UserID() string

UserID returns UserID stored in attribute.

type GlobalServiceOpt added in v0.3.3

type GlobalServiceOpt struct {
	Filters []restful.FilterFunction
	Errors  map[int]string
}

type LogWrap added in v0.3.0

type LogWrap struct {
	*zerolog.Event
	Level zerolog.Level
}

func Debug

func Debug() *LogWrap

Debug starts a new log with debug level.

func Error

func Error() *LogWrap

Error starts a new log with error level.

func Fatal

func Fatal() *LogWrap

Fatal starts a new log with fatal level.

func Info

func Info() *LogWrap

Info starts a new log with info level.

func Panic

func Panic() *LogWrap

Panic starts a new log with panic level.

func Warn

func Warn() *LogWrap

Warn starts a new log with warn level.

func (*LogWrap) AnErr added in v0.3.0

func (l *LogWrap) AnErr(key string, err error) *LogWrap

AnErr adds the field key with err as a string to the *Event context. If err is nil, no field is added.

func (*LogWrap) Array added in v0.3.0

func (l *LogWrap) Array(key string, arr zerolog.LogArrayMarshaler) *LogWrap

Array adds the field key with an array to the event context. Use zerolog.Arr() to create the array or pass a type that implement the LogArrayMarshaler interface.

func (*LogWrap) Bool added in v0.3.0

func (l *LogWrap) Bool(key string, b bool) *LogWrap

Bool adds the field key with val as a bool to the *Event context.

func (*LogWrap) Bools added in v0.3.0

func (l *LogWrap) Bools(key string, b []bool) *LogWrap

Bools adds the field key with val as a []bool to the *Event context.

func (*LogWrap) Bytes added in v0.3.0

func (l *LogWrap) Bytes(key string, val []byte) *LogWrap

Bytes adds the field key with val as a string to the *Event context.

Runes outside of normal ASCII ranges will be hex-encoded in the resulting JSON.

func (*LogWrap) Dur added in v0.3.0

func (l *LogWrap) Dur(key string, d time.Duration) *LogWrap

Dur adds the field key with duration d stored as zerolog.DurationFieldUnit. If zerolog.DurationFieldInteger is true, durations are rendered as integer instead of float.

func (*LogWrap) Durs added in v0.3.0

func (l *LogWrap) Durs(key string, d []time.Duration) *LogWrap

Durs adds the field key with duration d stored as zerolog.DurationFieldUnit. If zerolog.DurationFieldInteger is true, durations are rendered as integer instead of float.

func (*LogWrap) Err added in v0.3.0

func (l *LogWrap) Err(err error) *LogWrap

Err adds the field "error" with err as a string to the *Event context. If err is nil, no field is added. To customize the key name, change zerolog.ErrorFieldName.

func (*LogWrap) Errs added in v0.3.0

func (l *LogWrap) Errs(key string, errs []error) *LogWrap

Errs adds the field key with errs as an array of strings to the *Event context. If err is nil, no field is added.

func (*LogWrap) Fields added in v0.3.0

func (l *LogWrap) Fields(fields map[string]interface{}) *LogWrap

Fields is a helper function to use a map to set fields using type assertion.

func (*LogWrap) Float32 added in v0.3.0

func (l *LogWrap) Float32(key string, f float32) *LogWrap

Float32 adds the field key with f as a float32 to the *Event context.

func (*LogWrap) Float64 added in v0.3.0

func (l *LogWrap) Float64(key string, f float64) *LogWrap

Float64 adds the field key with f as a float64 to the *Event context.

func (*LogWrap) Floats32 added in v0.3.0

func (l *LogWrap) Floats32(key string, f []float32) *LogWrap

Floats32 adds the field key with f as a []float32 to the *Event context.

func (*LogWrap) Floats64 added in v0.3.0

func (l *LogWrap) Floats64(key string, f []float64) *LogWrap

Floats64 adds the field key with f as a []float64 to the *Event context.

func (*LogWrap) Int added in v0.3.0

func (l *LogWrap) Int(key string, i int) *LogWrap

Int adds the field key with i as a int to the *Event context.

func (*LogWrap) Int16 added in v0.3.0

func (l *LogWrap) Int16(key string, i int16) *LogWrap

Int16 adds the field key with i as a int16 to the *Event context.

func (*LogWrap) Int32 added in v0.3.0

func (l *LogWrap) Int32(key string, i int32) *LogWrap

Int32 adds the field key with i as a int32 to the *Event context.

func (*LogWrap) Int64 added in v0.3.0

func (l *LogWrap) Int64(key string, i int64) *LogWrap

Int64 adds the field key with i as a int64 to the *Event context.

func (*LogWrap) Int8 added in v0.3.0

func (l *LogWrap) Int8(key string, i int8) *LogWrap

Int8 adds the field key with i as a int8 to the *Event context.

func (*LogWrap) Interface added in v0.3.0

func (l *LogWrap) Interface(key string, i interface{}) *LogWrap

Interface adds the field key with i marshaled using reflection.

func (*LogWrap) Ints added in v0.3.0

func (l *LogWrap) Ints(key string, i []int) *LogWrap

Ints adds the field key with i as a []int to the *Event context.

func (*LogWrap) Ints16 added in v0.3.0

func (l *LogWrap) Ints16(key string, i []int16) *LogWrap

Ints16 adds the field key with i as a []int16 to the *Event context.

func (*LogWrap) Ints32 added in v0.3.0

func (l *LogWrap) Ints32(key string, i []int32) *LogWrap

Ints32 adds the field key with i as a []int32 to the *Event context.

func (*LogWrap) Ints64 added in v0.3.0

func (l *LogWrap) Ints64(key string, i []int64) *LogWrap

Ints64 adds the field key with i as a []int64 to the *Event context.

func (*LogWrap) Ints8 added in v0.3.0

func (l *LogWrap) Ints8(key string, i []int8) *LogWrap

Ints8 adds the field key with i as a []int8 to the *Event context.

func (LogWrap) Msg added in v0.3.0

func (l LogWrap) Msg(msg string)

Msg sends the *LogWrap with msg added as the message field if not empty.

NOTICE: once this method is called, the *LogWrap should be disposed. Calling Msg twice can have unexpected result.

func (*LogWrap) Object added in v0.3.0

func (l *LogWrap) Object(key string, obj zerolog.LogObjectMarshaler) *LogWrap

Object marshals an object that implement the LogObjectMarshaler interface.

func (*LogWrap) Str added in v0.3.0

func (l *LogWrap) Str(key, val string) *LogWrap

Str adds the field key with val as a string to the *Event context.

func (*LogWrap) Strs added in v0.3.0

func (l *LogWrap) Strs(key string, vals []string) *LogWrap

Strs adds the field key with vals as a []string to the *Event context.

func (*LogWrap) Time added in v0.3.0

func (l *LogWrap) Time(key string, t time.Time) *LogWrap

Time adds the field key with t formated as string using zerolog.TimeFieldFormat.

func (*LogWrap) TimeDiff added in v0.3.0

func (l *LogWrap) TimeDiff(key string, t time.Time, start time.Time) *LogWrap

TimeDiff adds the field key with positive duration between time t and start. If time t is not greater than start, duration will be 0. Duration format follows the same principle as Dur().

func (*LogWrap) Times added in v0.3.0

func (l *LogWrap) Times(key string, t []time.Time) *LogWrap

Times adds the field key with t formated as string using zerolog.TimeFieldFormat.

func (*LogWrap) Timestamp added in v0.3.0

func (l *LogWrap) Timestamp() *LogWrap

Timestamp adds the current local time as UNIX timestamp to the *Event context with the "time" key. To customize the key name, change zerolog.TimestampFieldName.

func (*LogWrap) Uint added in v0.3.0

func (l *LogWrap) Uint(key string, i uint) *LogWrap

Uint adds the field key with i as a uint to the *Event context.

func (*LogWrap) Uint16 added in v0.3.0

func (l *LogWrap) Uint16(key string, i uint16) *LogWrap

Uint16 adds the field key with i as a uint16 to the *Event context.

func (*LogWrap) Uint32 added in v0.3.0

func (l *LogWrap) Uint32(key string, i uint32) *LogWrap

Uint32 adds the field key with i as a uint32 to the *Event context.

func (*LogWrap) Uint64 added in v0.3.0

func (l *LogWrap) Uint64(key string, i uint64) *LogWrap

Uint64 adds the field key with i as a uint64 to the *Event context.

func (*LogWrap) Uint8 added in v0.3.0

func (l *LogWrap) Uint8(key string, i uint8) *LogWrap

Uint8 adds the field key with i as a uint8 to the *Event context.

func (*LogWrap) Uints added in v0.3.0

func (l *LogWrap) Uints(key string, i []uint) *LogWrap

Uints adds the field key with i as a []int to the *Event context.

func (*LogWrap) Uints16 added in v0.3.0

func (l *LogWrap) Uints16(key string, i []uint16) *LogWrap

Uints16 adds the field key with i as a []int16 to the *Event context.

func (*LogWrap) Uints32 added in v0.3.0

func (l *LogWrap) Uints32(key string, i []uint32) *LogWrap

Uints32 adds the field key with i as a []int32 to the *Event context.

func (*LogWrap) Uints64 added in v0.3.0

func (l *LogWrap) Uints64(key string, i []uint64) *LogWrap

Uints64 adds the field key with i as a []int64 to the *Event context.

func (*LogWrap) Uints8 added in v0.3.0

func (l *LogWrap) Uints8(key string, i []uint8) *LogWrap

Uints8 adds the field key with i as a []int8 to the *Event context.

type NS

type NS struct {
	NameSpace    string       // url parent of controller
	Controller   CtlInterface // controller implement CtlInterface
	Desc         string       // description of controller of namespace
	ExternalDesc string       // external documentation of controller
	ExternalURL  string       // external url of ExternalDesc
}

NS contains configuration of a namespace

type Parameter added in v0.2.1

type Parameter struct {
	Value []string
	// contains filtered or unexported fields
}

Parameter wrap parameter value in a request.

func (Parameter) Bool added in v0.2.1

func (p Parameter) Bool() (bool, error)

Bool converts a parameter value to bool.

func (Parameter) BoolArray added in v0.2.1

func (p Parameter) BoolArray() ([]bool, error)

BoolArray converts a parameter value to bool array.

func (Parameter) BoolDefault added in v0.2.1

func (p Parameter) BoolDefault(defaultValue bool) bool

BoolDefault converts a parameter value to bool with default value.

func (Parameter) Float32 added in v0.2.1

func (p Parameter) Float32() (float32, error)

Float32 converts a parameter value to float32.

func (Parameter) Float32Array added in v0.2.1

func (p Parameter) Float32Array() ([]float32, error)

Float32Array converts a parameter value to float32 array.

func (Parameter) Float32Default added in v0.2.1

func (p Parameter) Float32Default(defaultValue float32) float32

Float32Default converts a parameter value to float32 with default value.

func (Parameter) Float64 added in v0.2.1

func (p Parameter) Float64() (float64, error)

Float64 converts a parameter value to float64.

func (Parameter) Float64Array added in v0.2.1

func (p Parameter) Float64Array() ([]float64, error)

Float64Array converts a parameter value to float64 array.

func (Parameter) Float64Default added in v0.2.1

func (p Parameter) Float64Default(defaultValue float64) float64

Float64Default converts a parameter value to float64 with default value.

func (Parameter) Int added in v0.2.1

func (p Parameter) Int() (int, error)

Int converts a parameter value to int.

func (Parameter) Int16 added in v0.2.1

func (p Parameter) Int16() (int16, error)

Int16 converts a parameter value to int16.

func (Parameter) Int16Array added in v0.2.1

func (p Parameter) Int16Array() ([]int16, error)

Int16Array converts a parameter value to int16 array.

func (Parameter) Int16Default added in v0.2.1

func (p Parameter) Int16Default(defaultValue int16) int16

Int16Default converts a parameter value to int16 with default value.

func (Parameter) Int32 added in v0.2.1

func (p Parameter) Int32() (int32, error)

Int32 converts a parameter value to int32.

func (Parameter) Int32Array added in v0.2.1

func (p Parameter) Int32Array() ([]int32, error)

Int32Array converts a parameter value to int32 array.

func (Parameter) Int32Default added in v0.2.1

func (p Parameter) Int32Default(defaultValue int32) int32

Int32Default converts a parameter value to int32 with default value.

func (Parameter) Int64 added in v0.2.1

func (p Parameter) Int64() (int64, error)

Int64 converts a parameter value to int64.

func (Parameter) Int64Array added in v0.2.1

func (p Parameter) Int64Array() ([]int64, error)

Int64Array converts a parameter value to int64 array.

func (Parameter) Int64Default added in v0.2.1

func (p Parameter) Int64Default(defaultValue int64) int64

Int64Default converts a parameter value to int64 with default value.

func (Parameter) Int8 added in v0.2.1

func (p Parameter) Int8() (int8, error)

Int8 converts a parameter value to int8.

func (Parameter) Int8Array added in v0.2.1

func (p Parameter) Int8Array() ([]int8, error)

Int8Array converts a parameter value to int8 array.

func (Parameter) Int8Default added in v0.2.1

func (p Parameter) Int8Default(defaultValue int8) int8

Int8Default converts a parameter value to int8 with default value.

func (Parameter) IntArray added in v0.2.1

func (p Parameter) IntArray() ([]int, error)

IntArray converts a parameter value to int array.

func (Parameter) IntDefault added in v0.2.1

func (p Parameter) IntDefault(defaultValue int) int

IntDefault converts a parameter value to int with default value.

func (Parameter) String added in v0.2.1

func (p Parameter) String() (string, error)

String converts a parameter value to string.

func (Parameter) StringArray added in v0.2.1

func (p Parameter) StringArray() ([]string, error)

StringArray converts a parameter value to string array.

func (Parameter) StringDefault added in v0.2.1

func (p Parameter) StringDefault(defaultValue string) string

StringDefault converts a parameter value to string with default value.

func (Parameter) Time added in v0.2.1

func (p Parameter) Time(layout string) (time.Time, error)

Time parse a parameter value to time.Time with given layout.

func (Parameter) TimeArray added in v0.2.1

func (p Parameter) TimeArray(layout string) ([]time.Time, error)

TimeArray parse a parameter value to time.Time array.

func (Parameter) TimeDefault added in v0.2.1

func (p Parameter) TimeDefault(layout string, defaultValue time.Time) time.Time

TimeDefault parse a parameter value to time.Time with given layout with default value.

func (Parameter) Uint added in v0.2.1

func (p Parameter) Uint() (uint, error)

Uint converts a parameter value to uint.

func (Parameter) Uint16 added in v0.2.1

func (p Parameter) Uint16() (uint16, error)

Uint16 converts a parameter value to uint16.

func (Parameter) Uint16Array added in v0.2.1

func (p Parameter) Uint16Array() ([]uint16, error)

Uint16Array converts a parameter value to uint16 array.

func (Parameter) Uint16Default added in v0.2.1

func (p Parameter) Uint16Default(defaultValue uint16) uint16

Uint16Default converts a parameter value to uint16 with default value.

func (Parameter) Uint32 added in v0.2.1

func (p Parameter) Uint32() (uint32, error)

Uint32 converts a parameter value to uint32.

func (Parameter) Uint32Array added in v0.2.1

func (p Parameter) Uint32Array() ([]uint32, error)

Uint32Array converts a parameter value to uint32 array.

func (Parameter) Uint32Default added in v0.2.1

func (p Parameter) Uint32Default(defaultValue uint32) uint32

Uint32Default converts a parameter value to uint32 with default value.

func (Parameter) Uint64 added in v0.2.1

func (p Parameter) Uint64() (uint64, error)

Uint64 converts a parameter value to uint64.

func (Parameter) Uint64Array added in v0.2.1

func (p Parameter) Uint64Array() ([]uint64, error)

Uint64Array converts a parameter value to uint64 array.

func (Parameter) Uint64Default added in v0.2.1

func (p Parameter) Uint64Default(defaultValue uint64) uint64

Uint64Default converts a parameter value to uint64 with default value.

func (Parameter) Uint8 added in v0.2.1

func (p Parameter) Uint8() (uint8, error)

Uint8 converts a parameter value to uint8.

func (Parameter) Uint8Array added in v0.2.1

func (p Parameter) Uint8Array() ([]uint8, error)

Uint8Array converts a parameter value to uint8 array.

func (Parameter) Uint8Default added in v0.2.1

func (p Parameter) Uint8Default(defaultValue uint8) uint8

Uint8Default converts a parameter value to uint8 with default value.

func (Parameter) UintArray added in v0.2.1

func (p Parameter) UintArray() ([]uint, error)

UintArray converts a parameter value to uint array.

func (Parameter) UintDefault added in v0.2.1

func (p Parameter) UintDefault(defaultValue uint) uint

UintDefault converts a parameter value to uint with default value.

type RouteOpt

type RouteOpt struct {
	ID              string
	To              func(ctx Ctx)
	Auth            bool
	NeedPermissions []string
	Errors          map[int]string
}

RouteOpt contains some options of route.

type RunConfig

type RunConfig struct {
	BeforeShutDown func()
	AfterShutDown  func()
}

RunConfig is the running config of container.

type Setter added in v0.1.0

type Setter struct{}

Setter is a setter for setting global options.

func JWTRefreshTimeout added in v0.1.0

func JWTRefreshTimeout(timeout time.Duration) Setter

JWTRefreshTimeout sets refresh timeout for JWT.

func JWTSecret added in v0.1.0

func JWTSecret(f func(userID string) (secret []byte, err error)) Setter

JWTSecret sets secret for JWT.

func JWTTimeout added in v0.1.0

func JWTTimeout(timeout time.Duration) Setter

JWTTimeout sets timeout for JWT.

func (Setter) JWTRefreshTimeout added in v0.1.0

func (Setter) JWTRefreshTimeout(timeout time.Duration) Setter

JWTRefreshTimeout sets refresh timeout for JWT.

func (Setter) JWTSecret added in v0.1.0

func (Setter) JWTSecret(f func(userID string) (secret []byte, err error)) Setter

JWTSecret sets secret for JWT.

func (Setter) JWTTimeout added in v0.1.0

func (Setter) JWTTimeout(timeout time.Duration) Setter

JWTTimeout sets timeout for JWT.

type SwaggerInfo

type SwaggerInfo struct {
	Title          string
	Description    string
	TermsOfService string
	ContactName    string
	ContactURL     string
	ContactEmail   string
	LicenseName    string
	LicenseURL     string
	Version        string
	WebServicesURL string
	DisableCORS    bool
	// route prefix of swagger service
	// swagger service will running under
	// http://<api>/<RoutePrefix>/swagger
	RoutePrefix string
}

SwaggerInfo contains configuration of swagger documents.

type TestServer added in v0.2.0

type TestServer struct {
	*httptest.Server
}

TestServer wraps a httptest.Server

func NewTestServer added in v0.2.0

func NewTestServer() *TestServer

NewTestServer returns a Test Server.

func (*TestServer) WithT added in v0.2.0

func (s *TestServer) WithT(t *testing.T) *httpexpect.Expect

WithT accept testing.T and returns httpexpect.Expect

type WS

type WS struct {
	*restful.WebService
}

WS extends *restful.WebService

func (WS) Route

func (ws WS) Route(builder *restful.RouteBuilder, opt *RouteOpt)

Route creates a new Route using the RouteBuilder and add to the ordered list of Routes.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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