minigo

package module
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2020 License: MIT Imports: 15 Imported by: 0

README

Minigo

A fast, small but enough api framework for go.

It is heavily influenced by Gin.

Overview

  • Router, group router
  • Request context
  • Handlers
  • Just support json input and output
  • Just suport POST
  • Validate

Installation

step 1.

go get -u github.com/oldrain/minigo

step 2.

import "github.com/oldrain/minigo"

Quick start

File:

example.go

Code:

package main

import (
    "github.com/oldrain/minigo"
)

func main() {
    api := minigo.Default()

    api.Post("/hey", func(ctx *minigo.Context) {
        ctx.JSON("Hi~")
    })

    _ = api.Run(":9527")
}

Request:

curl -X POST -H "Content-Type: application/json" http://127.0.0.1:9527/hey

Response:

{"code":200,"msg":"Success","data":"Hi~","ts":"2019-01-01 00:00:00"}

Examples

Using Router and POST
func main() {
    api := minigo.Default()

    // user
    user := api.Group("/user")
    user.Post("/info", func(ctx *minigo.Context) {
        ctx.JSON("user.info")
    })

    // user.favorite
    favorite := user.Group("/favorite")
    favorite.Post("/list", func(ctx *minigo.Context) {
        ctx.JSON("user.favorite.list")
    })

     _ = api.Run(":9527")
}
Using handlers
func main() {
    api := minigo.Default()

    // user
    user := api.Group("/user", before)
    user.Post("/info", userInfo)

    // user.favorite
    favorite := user.Group("/favorite", before)
    favorite.Post("/list", userFavoriteList)
    favorite.LastUse(after)

     _ = api.Run(":9527")
}

func before(ctx *minigo.Context) {
    // do something
}

func after(ctx *minigo.Context) {
    // logging
}

func userInfo(ctx *minigo.Context) {
    ctx.JSON("user.info")
}

func userFavoriteList(ctx *minigo.Context) {
    ctx.JSON("user.favorite.list")
}

Input && Validate && Output
type UserInfoIn struct {
	Id int `json:"id" regexp:"^[2-4]$" tips:"Invalid id"`
	Name string `json:"name" regexp:"^.{2,8}$" tips:"Invalid name"`
	Email string `json:"email" regexp:"^.+@.+\\..+$" tips:"Invalid email"`
}

type UserInfoOut struct {
	User *UserInfoIn `json:"user"`
}

func main() {
	api := minigo.Default()

	// user
	user := api.Group("/user")
	user.Post("/info", userInfo)

	_ = api.Run(":9527")
}

func userInfo(ctx *minigo.Context) {
	userInfoIn := new(UserInfoIn)

	_ = ctx.BindJSON(&userInfoIn)

	validate := minigo.NewValidate()
	err := validate.Do(userInfoIn)

	if err != nil {
		ctx.Error(10000, err.Error())
		return
	}

	userInfoOut := new(UserInfoOut)
	userInfoOut.User = userInfoIn

	ctx.JSON(userInfoOut)
}
Others
Customization handler
func before(ctx *minigo.Context) {
    // do something
    // if ok ctx.Continue()
    // if err ctx.Abort() or ctx.AbortWithError()
}
Request header
func userInfo(ctx *minigo.Context) {
    token := ctx.GetInHeader("token")
    ...
}
Context get and set
func before(ctx *minigo.Context) {
    ctx.set("key", "value")
    ...
}

func userInfo(ctx *minigo.Context) {
    value := ctx.getString("key")
    ...
}

Testing

File:

example.go

CMD:

hey -n 100000 -c 10 -m POST -H "Content-Type: application/json" http://127.0.0.1:9527/hey

Result:

Summary:
  Total:	4.8541 secs
  Slowest:	0.0128 secs
  Fastest:	0.0001 secs
  Average:	0.0005 secs
  Requests/sec:	20601.1037

  Total data:	6800000 bytes
  Size/request:	68 bytes

Response time histogram:
  0.000 [1]	|
  0.001 [98798]	|■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
  0.003 [1063]	|
  0.004 [102]	|
  0.005 [21]	|
  0.006 [7]	|
  0.008 [5]	|
  0.009 [1]	|
  0.010 [0]	|
  0.012 [1]	|
  0.013 [1]	|


Latency distribution:
  10% in 0.0002 secs
  25% in 0.0003 secs
  50% in 0.0004 secs
  75% in 0.0006 secs
  90% in 0.0008 secs
  95% in 0.0009 secs
  99% in 0.0015 secs

Details (average, fastest, slowest):
  DNS+dialup:	0.0000 secs, 0.0001 secs, 0.0128 secs
  DNS-lookup:	0.0000 secs, 0.0000 secs, 0.0000 secs
  req write:	0.0000 secs, 0.0000 secs, 0.0044 secs
  resp wait:	0.0004 secs, 0.0001 secs, 0.0128 secs
  resp read:	0.0000 secs, 0.0000 secs, 0.0059 secs

Status code distribution:
  [200]	100000 responses

Documentation

Index

Constants

View Source
const Version = "v1.0.3"

Variables

This section is empty.

Functions

This section is empty.

Types

type Api

type Api struct {
	Router
	// contains filtered or unexported fields
}

func Default

func Default() *Api

func New

func New() *Api

func (*Api) Run

func (api *Api) Run(addr string) (err error)

func (*Api) ServeHTTP

func (api *Api) ServeHTTP(res http.ResponseWriter, req *http.Request)

func (*Api) Use

func (api *Api) Use(middleware ...HandlerFunc) *Router

type Context

type Context struct {
	Input  Input
	Output Output

	KV map[string]interface{}
	// contains filtered or unexported fields
}

func (*Context) Abort

func (ctx *Context) Abort()

func (*Context) AbortWithError

func (ctx *Context) AbortWithError(code int, msg string)

func (*Context) BindJSON

func (ctx *Context) BindJSON(obj interface{}) (err error)

input

func (*Context) ClientIP

func (ctx *Context) ClientIP() string

func (*Context) Continue

func (ctx *Context) Continue()

func (*Context) Error

func (ctx *Context) Error(code int, msg string)

func (*Context) Get

func (ctx *Context) Get(key string) (value interface{}, ok bool)

func (*Context) GetBool

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

func (*Context) GetFloat32

func (ctx *Context) GetFloat32(key string) (i float32)

func (*Context) GetFloat64

func (ctx *Context) GetFloat64(key string) (i float64)

func (*Context) GetInBody

func (ctx *Context) GetInBody() string

func (*Context) GetInHeader

func (ctx *Context) GetInHeader(key string) string

func (*Context) GetInt

func (ctx *Context) GetInt(key string) (i int)

func (*Context) GetInt32

func (ctx *Context) GetInt32(key string) (i int32)

func (*Context) GetInt64

func (ctx *Context) GetInt64(key string) (i int64)

func (*Context) GetOutBody

func (ctx *Context) GetOutBody() string

func (*Context) GetOutHeader

func (ctx *Context) GetOutHeader(key string) string

func (*Context) GetString

func (ctx *Context) GetString(key string) (s string)

func (*Context) GetUint

func (ctx *Context) GetUint(key string) (i uint)

func (*Context) GetUint32

func (ctx *Context) GetUint32(key string) (i uint32)

func (*Context) GetUint64

func (ctx *Context) GetUint64(key string) (i uint64)

func (*Context) JSON

func (ctx *Context) JSON(data interface{})

output

func (*Context) Method

func (ctx *Context) Method() string

func (*Context) Path

func (ctx *Context) Path() string

func (*Context) Set

func (ctx *Context) Set(key string, value interface{})

KV

func (*Context) SetInHeader

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

func (*Context) SetOutBody

func (ctx *Context) SetOutBody(body string)

func (*Context) SetOutHeader

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

type HandlerFunc

type HandlerFunc func(ctx *Context)

func DefaultRecovery

func DefaultRecovery() HandlerFunc

func Logger

func Logger() HandlerFunc

func Recovery

func Recovery() HandlerFunc

type HandlerFuncChain

type HandlerFuncChain []HandlerFunc

type IGroup

type IGroup interface {
	IRoute

	Group()
}

type IRoute

type IRoute interface {
	Use()

	Post()
}

type Input

type Input struct {
	Request *http.Request
	// contains filtered or unexported fields
}

func (*Input) GetBody

func (in *Input) GetBody() string

func (*Input) GetHeader

func (in *Input) GetHeader(key string) string

func (*Input) SetHeader

func (in *Input) SetHeader(key, value string)

type OJson

type OJson struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
	Ts   string      `json:"ts"`
}

type Output

type Output struct {
	Response http.ResponseWriter
	// contains filtered or unexported fields
}

func (*Output) GetHeader

func (out *Output) GetHeader(key string) string

func (*Output) RenderJson

func (out *Output) RenderJson(oJson OJson) error

func (*Output) SetHeader

func (out *Output) SetHeader(key, value string)

type RouteInfo

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

type Router

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

func (*Router) BasePath

func (router *Router) BasePath() string

func (*Router) Group

func (router *Router) Group(relativePath string, handlers ...HandlerFunc) *Router

func (*Router) LastUse

func (router *Router) LastUse(middleware ...HandlerFunc) *Router

func (*Router) Post

func (router *Router) Post(relativePath string, handlers ...HandlerFunc) *Router

func (*Router) Use

func (router *Router) Use(middleware ...HandlerFunc) *Router

type Validate

type Validate struct{}

func NewValidate

func NewValidate() *Validate

func (*Validate) Do

func (validate *Validate) Do(ptr interface{}) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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