rest

package
v0.0.0-...-0c811c1 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2019 License: Apache-2.0 Imports: 7 Imported by: 0

README

RESTful

RESTful tools based on Middleware for micro api

Warning: This is not a framework, because it is dependent on:

  • auth "github.com/ddosakura/starmap/srv/auth/proto" (auth service)
  • api "github.com/micro/go-api/proto" (not common api interface)
  • "github.com/micro/go-micro/errors"
  • client "github.com/micro/go-micro/client"

内置中间件使用顺序

认证顺序 (必须)
  • LoadAuthService
  • JWTCheck
  • RoleCheck/PermCheck/SuperRole
参数解析顺序 (建议)
  • ParamCheck
  • ParamAutoLoad (放在 ParamCheck 前面将无法使用默认值、重名名)
总体使用顺序 (建议)
  • LoadAuthService
  • JWTCheck
    • 须要请求认证服务,但在 RESTful 中的所有请求大都需要检查
  • ParamCheck
  • ParamAutoLoad (放在 ParamCheck 前面将无法使用默认值、重名名)
  • RoleCheck/PermCheck/SuperRole
    • 须要请求认证服务
    • SuperRole 可能需要用 ParamAutoLoad 加载参数

Documentation

Overview

Example
handle := func(ctx context.Context, s *Flow) error {
	fmt.Println("M", s.Rest)
	return s.Success(fmt.Sprintf("M %v", s.Rest))
}

Handle := func(ctx context.Context, req *api.Request, res *api.Response) error {
	return REST(ctx, req, res).
		Chain(LoadAuthService(func(ctx context.Context) (AuthService, bool) { return nil, false })).
		Chain(JWTCheck()).
		Chain(RoleCheck([]string{"admin"}, LogicalAND)).
		// API
		Action(POST).
		Chain(PermCheck([]string{"user:insert"}, LogicalAND)).
		Chain(handle).
		Done().
		// API
		Action(DELETE).
		Chain(PermCheck([]string{"user:delete"}, LogicalAND)).
		Chain(handle).
		Done().
		// API
		Action(GET).
		Chain(PermCheck([]string{"user:select"}, LogicalAND)).
		Chain(handle).
		Done().
		// API
		Action(PUT).
		Chain(PermCheck([]string{"user:update"}, LogicalAND)).
		Chain(handle).
		Done().
		// Finsh
		Final()
}

Handle(context.Background(),
	&api.Request{},
	&api.Response{},
)
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	PccEmptyStr = &PCC{DefaultV: []string{""}}
	PccMust     = &PCC{Must: true}
)

PCC in common use

View Source
var (
	PmInt = func(ps []string) (interface{}, error) {
		return strconv.Atoi(ps[0])
	}
)

ParamModify in common use

View Source
var (
	RESTfulTypeDict = map[string]RESTfulType{
		"GET":    GET,
		"get":    GET,
		"POST":   POST,
		"post":   POST,
		"delete": DELETE,
		"DELETE": DELETE,
		"put":    PUT,
		"PUT":    PUT,
	}
)

dict

View Source
var (
	SrvName = "starmap.api"
)

Configurable raw

Functions

func AInB

func AInB(a []string, b []string) bool

AInB test

func CleanErrResponse

func CleanErrResponse(id string, e error, fn func(id, format string, a ...interface{}) error) error

CleanErrResponse to cancel circle Error

func GetJWT

func GetJWT(r *api.Request) string

GetJWT from header

func ItemInList

func ItemInList(a string, b []string) bool

ItemInList util

func Pair2Str

func Pair2Str(pair *api.Pair) (v string, ok bool)

Pair2Str extract single string value

func RoleLevel

func RoleLevel(roles []string) int

RoleLevel Check

func SetJWT

func SetJWT(r *api.Response, jwt string)

SetJWT to header

Types

type AuthService

type AuthService interface {
	Check(ctx context.Context, in *auth.UserToken, opts ...client.CallOption) (*auth.UserToken, error)
	Roles(ctx context.Context, in *auth.Identity, opts ...client.CallOption) (*auth.Result, error)
	Perms(ctx context.Context, in *auth.Identity, opts ...client.CallOption) (*auth.Result, error)
}

AuthService needed to load

type Flow

type Flow struct {
	// req data (should not change by Middleware)
	Ctx    context.Context
	Req    *api.Request
	Res    *api.Response
	Rest   RESTfulType
	Params map[string]*api.Pair

	// cache
	AuthUserClient AuthService
	Token          *auth.UserToken
	Roles          []string
	Perms          []string
	// contains filtered or unexported fields
}

Flow for RESTful API

func (*Flow) Action

func (s *Flow) Action(t RESTfulType) RESTful

Action for Flow

func (*Flow) Chain

func (s *Flow) Chain(fn Middleware) RESTful

Chain for Flow

func (*Flow) Done

func (s *Flow) Done() RESTful

Done Flow

func (*Flow) Final

func (s *Flow) Final() error

Final Flow

func (*Flow) FreshJWT

func (s *Flow) FreshJWT(jwt string)

FreshJWT API

func (*Flow) Success

func (s *Flow) Success(d interface{}) error

Success API

type Logical

type Logical int

Logical for Role & Perm

const (
	LogicalAND Logical = iota
	LogicalOR
)

Logicals

type Middleware

type Middleware func(context.Context, *Flow) error // if it returns a error, flow will finish

Middleware for RESTful

func JWTCheck

func JWTCheck() Middleware

JWTCheck Wrapper (load UserInfo in s.Token)

func LoadAuthService

func LoadAuthService(loader func(ctx context.Context) (AuthService, bool)) Middleware

LoadAuthService Wrapper

func ParamAutoLoad

func ParamAutoLoad(modify ParamModifyList, entity interface{}) Middleware

ParamAutoLoad Wrapper

func ParamCheck

func ParamCheck(pccs PCCS) Middleware

ParamCheck Wrapper

func PermCheck

func PermCheck(rules []string, logical Logical) Middleware

PermCheck Wrapper

func RoleCheck

func RoleCheck(rules []string, logical Logical) Middleware

RoleCheck Wrapper

func RoleLevelCheck

func RoleLevelCheck(id string) Middleware

RoleLevelCheck Wrapper

type PCC

type PCC struct {
	Must     bool
	Multi    bool
	DefaultV []string

	Link        string
	LinkLogical Logical

	Rename string
	// contains filtered or unexported fields
}

PCC - ParamCheck Config

func PccLabel

func PccLabel(pcc *PCC, logical Logical) *PCC

PccLabel Builder

func PccLink(label string) *PCC

PccLink Builder

func PccRename

func PccRename(pcc *PCC, name string) *PCC

PccRename Builder

type PCCS

type PCCS map[string]*PCC

PCCS - PCC Group

type ParamModify

type ParamModify func([]string) (interface{}, error)

ParamModify for ParamAutoLoad

type ParamModifyList

type ParamModifyList map[string]ParamModify

ParamModifyList for ParamAutoLoad

type RESTful

type RESTful interface {
	// flow
	Action(RESTfulType) RESTful // build child
	Chain(Middleware) RESTful   // use Middleware
	Done() RESTful              // backto father
	Final() error               // error response (nil means success)
}

RESTful API

func REST

func REST(ctx context.Context, req *api.Request, res *api.Response) RESTful

REST Builder

type RESTfulType

type RESTfulType int

RESTfulType for API

const (
	POST   RESTfulType = 1 << iota // INSERT
	DELETE                         // DELETE
	GET                            // INSERT
	PUT                            // UPDATE
)

RESTfulType(s)

Jump to

Keyboard shortcuts

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