biu: github.com/tuotoo/biu Index | Examples | Files | Directories

package biu

import "github.com/tuotoo/biu"

Code:

package main

import (
    "github.com/tuotoo/biu"
    "github.com/tuotoo/biu/box"
    "github.com/tuotoo/biu/opt"
)

// 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{}),
        opt.RouteID("example.foo"),
        opt.RouteTo(ctl.getBar),
        opt.RouteErrors(map[int]string{
            100: "num not Number",
        }),
    )

    ws.Route(ws.POST("/").
        Doc("Post Req"),
        opt.RouteID("example.post"),
        opt.RouteAPI(ctl.post),
        opt.RouteErrors(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 box.Ctx) {
    num, err := ctx.Query("num").Int()
    ctx.Must(err, 100)

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

func (ctl Foo) post(ctx box.Ctx, api struct {
    Form struct{ Num int }
}) {
    ctx.ResponseJSON(Bar{Msg: "POST", Num: api.Form.Num})
}

func main() {
    c := biu.New()
    c.Filter(biu.LogFilter())
    c.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 := c.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",
    })
    c.Add(swaggerService)
    c.Run(":8080", nil)
}

Index

Examples

Package Files

container.go context.go go-restful-helper.go swagger.go type.go

Constants

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

var AutoGenPathDoc = false
var DefaultContainer = New(restful.DefaultContainer)

func AddServices Uses

func AddServices(prefix string, opts opt.ServicesFuncArr, wss ...NS)

AddServices adds services with namespace.

func AuthFilter Uses

func AuthFilter(code int, i ...*auth.Instance) restful.FilterFunction

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

func Filter Uses

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

Filter transform a biu handler to a restful.FilterFunction

func FilterWithLogger Uses

func FilterWithLogger(f func(ctx box.Ctx), logger log.ILogger) restful.FilterFunction

func Handle Uses

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

Handle transform a biu handler to a restful.RouteFunction.

func HandleWithLogger Uses

func HandleWithLogger(f func(ctx box.Ctx), logger log.ILogger) restful.RouteFunction

func ListenAndServe Uses

func ListenAndServe(srv *http.Server, addrChan chan<- string) error

ListenAndServe listens on the TCP network address srv.Addr and then calls Serve to handle requests on incoming connections. Accepted connections are configured to enable TCP keep-alives. If srv.Addr is blank, ":http" is used. ListenAndServe always returns a non-nil error.

func LogFilter Uses

func LogFilter() restful.FilterFunction

LogFilter logs

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

for each request

func NewSwaggerService Uses

func NewSwaggerService(info SwaggerInfo) *restful.WebService

NewSwaggerService creates a swagger webservice in /swagger

func Run Uses

func Run(addr string, opts ...opt.RunFunc)

Run starts up a web server with default container.

func WrapHandler Uses

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

WrapHandler wraps a biu handler to http.HandlerFunc

type Container Uses

type Container struct {
    *restful.Container
    // contains filtered or unexported fields
}

Container of restful

func New Uses

func New(container ...*restful.Container) *Container

New creates a new restful container.

func (*Container) AddServices Uses

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

AddServices adds services with namespace for container.

func (*Container) FilterFunc Uses

func (c *Container) FilterFunc(f func(ctx box.Ctx)) restful.FilterFunction

Filter transform a biu handler to a restful.FilterFunction

func (*Container) Handle Uses

func (c *Container) Handle(f func(ctx box.Ctx)) restful.RouteFunction

func (*Container) NewSwaggerService Uses

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

NewSwaggerService creates a swagger webservice in /swagger

func (*Container) NewTestServer Uses

func (c *Container) NewTestServer() *TestServer

NewTestServer returns a Test Server.

func (*Container) Run Uses

func (c *Container) Run(addr string, opts ...opt.RunFunc)

Run starts up a web server for container.

type CtlInterface Uses

type CtlInterface interface {
    WebService(WS)
}

CtlInterface is the interface of controllers

type NS Uses

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 SwaggerInfo Uses

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
    // swagger service will running under
    // http://<api>/<RoutePrefix>/<RouteSuffix>
    // by default the RouteSuffix is swagger
    RoutePrefix string
    RouteSuffix string
}

SwaggerInfo contains configuration of swagger documents.

type TestServer Uses

type TestServer struct {
    *httptest.Server
}

TestServer wraps a httptest.Server

func NewTestServer Uses

func NewTestServer() *TestServer

NewTestServer returns a Test Server.

func (*TestServer) WithT Uses

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

WithT accept testing.T and returns httpexpect.Expect

type WS Uses

type WS struct {
    *restful.WebService
    Container *Container
    // contains filtered or unexported fields
}

WS extends *restful.WebService

func (WS) Route Uses

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

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

Directories

PathSynopsis
auth
box
example
internal
log
opt
param
swagger-go

Package biu imports 25 packages (graph) and is imported by 5 packages. Updated 2020-05-13. Refresh now. Tools for package owners.