rester

module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2020 License: Apache-2.0

README

Rester report card GitHub release GoDoc view Go网络编程群

Fast and concise RESTful web framework based on fasthttp.

Example

package main

import "github.com/henrylee2cn/rester"

type Echo1Ctl struct {
	rester.BaseCtl
	skip bool
}

func (ctl *Echo1Ctl) Any(args struct {
	A string `query:"a"`
}) {
	ctl.Logger().Printf("Echo1Ctl: a=%s", args.A)
	if !ctl.skip {
		ctl.SetUserValue("a", args.A)
	}
}

type Echo2Ctl struct {
	Echo1Ctl
}

func (ctl *Echo2Ctl) GET(args struct {
	B []string `query:"b"`
}) {
	ctl.Logger().Printf("Echo2Ctl: b=%v", args.B)
	ctl.OK(rester.H{
		"a": ctl.UserValue("a"),
		"b": args.B,
	})
}

func main() {
	engine := rester.New()
	engine.Control("/", new(Echo2Ctl))
	engine.ControlFrom("/from", func() rester.Controller {
		return &Echo2Ctl{
			Echo1Ctl{skip: true},
		}
	})
	err := engine.ListenAndServe(":8080")
	if err != nil {
		panic(err)
	}
	// request:
	//  GET http://localhost:8080/?a=x&b=y&b=z
	// log:
	//  - Echo1Ctl: a=x
	//  - Echo2Ctl: b=[y z]
	// response:
	//  {"a":"x","b":["y","z"]}

	// request:
	//  GET http://localhost:8080/from?a=x&b=y&b=z
	// log:
	//  - Echo1Ctl: a=x
	//  - Echo2Ctl: b=[y z]
	// response:
	//  {"a":null,"b":["y","z"]}
}

More examples

Binding

binding doc

Controller

Design of method call chain for anonymous field of controller

Struct Method Chain

Directories

Path Synopsis
Package chain convert the method of nested fields in structure to call chain function.
Package chain convert the method of nested fields in structure to call chain function.
example

Jump to

Keyboard shortcuts

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