adventurer

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2020 License: MIT Imports: 18 Imported by: 0

README

Adventurer

Go语言的路由器框架,支持URL正则匹配,API预处理等功能

Features

  • URL支持正则表达式
  • API预处理
  • URL支持指定请求Method
  • 支持跨域
  • 支持自定义错误返回

Quick Start

Story

使用yaml存储配置信息。

例:stories.yaml

- url: "/test/hello" # 请求的URL
  method:  # 支持的Method方法,可以多个
    - "GET"
  handler: "DemoHandler"  # 处理struct 对象的handler方法名称,必须大写开头
  trials: # 预处理定义,map[string][]string类型,和Hook配对使用
    token:
      - "true"
    header:
      - "true"
    permission:
      - "ADMIN"

加载yaml信息

stories, err := adventurer.LoadStories("./stories/stories.yaml")
if nil != err {
    log.Fatalln(err.Error())
}
Hook

如果需要对URL进行预处理,需要实现Hook接口。

type HeaderHook struct {
}

func NewHeaderHook() adventurer.Hook {
	return &HeaderHook{}
}

func (h *HeaderHook) Fire(prerequisite []string, equipment adventurer.Equipment) (bool, error) {
	if nil == prerequisite || len(prerequisite) < 1 {
		return false, nil
	}
	if "true" == prerequisite[0] {
		if "" == equipment.Header.Get("device") {
			return false, nil
		}
	}
	return true, nil
}
跨域
adventurer.SetCros(true)
Demo
func main() {
	log.SetFlags(log.LstdFlags | log.Lshortfile)
	stories, err := adventurer.LoadStories("./stories/stories.yaml")
	if nil != err {
		log.Fatalln(err.Error())
	}
	hook := make(adventurer.StoryHook, 0)
	hook["header"] = NewHeaderHook()
	a, err := adventurer.NewAdventurer(Demo{}, stories,
		adventurer.NewProfile("/demo/about", "0.0.1", "1.11.2", "", "test"),
		&hook)
	if nil != err {
		log.Fatalln(err.Error())
	}
	http.HandleFunc("/", a.Explore)
	log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", 2111), nil))
}

// Demo demo
type Demo struct {
}

// DemoHandler demo handler
func (d *Demo) DemoHandler(w http.ResponseWriter, r *http.Request) {
	w.Write([]byte("Hello"))
}

type HeaderHook struct {
}

func NewHeaderHook() adventurer.Hook {
	return &HeaderHook{}
}

func (h *HeaderHook) Fire(prerequisite []string, equipment adventurer.Equipment) (bool, error) {
	if nil == prerequisite || len(prerequisite) < 1 {
		return false, nil
	}
	if "true" == prerequisite[0] {
		if "" == equipment.Header.Get("device") {
			return false, nil
		}
	}
	return true, nil
}

Documentation

Overview

Package adventurer @author K·J Create at 2019-01-09 11:00

Package adventurer @author K·J Create at 2019-01-09 11:03

Package adventurer @author K·J Create at 2019-01-09 11:04

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LoadStories

func LoadStories(path string) (*[]Story, error)

LoadStories load stories

Types

type Adventurer

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

Adventurer 路由对象

func NewAdventurer

func NewAdventurer(owner interface{}, stories *[]Story,
	profile *Profile, hook *StoryHook, resp *Resp,
	enableTrialsErr bool, logger *logrus.Logger) (*Adventurer, error)

NewAdventurer 生成路由对象

func (*Adventurer) AddStory

func (a *Adventurer) AddStory(s Story) error

AddStory add story

func (*Adventurer) Explore

func (a *Adventurer) Explore(w http.ResponseWriter, r *http.Request)

Explore url router

func (*Adventurer) InitStory

func (a *Adventurer) InitStory(stories []Story) error

InitStory 初始化story

func (*Adventurer) SetCros

func (a *Adventurer) SetCros(cros bool)

SetCros enable or disable cros

type Equipment

type Equipment struct {
	Query  url.Values
	Body   []byte
	Header http.Header
	Method string
}

Equipment 请求的信息

type Hook

type Hook interface {
	Fire(prerequisite []string, equipment Equipment) (bool, error)
	ErrResp(w *http.ResponseWriter)
}

Hook 校验

type Profile

type Profile struct {
	Name      string // Application name
	Version   string // Application version
	BuildTime string // Compilation date
	GoVersion string // Golang version
	Mode      string // Deployment mode
	URL       string // URL
	Desc      string // Description.
}

Profile 项目信息

func NewProfile

func NewProfile(url, version, goVersion, buildTime, mode string) *Profile

NewProfile profile

func (*Profile) Description

func (p *Profile) Description() string

Description profile desc

func (*Profile) Handler

func (p *Profile) Handler(w http.ResponseWriter, r *http.Request)

Handler url handler

type Resp added in v1.0.2

type Resp struct {
	Code int
	Msg  []byte
}

type Story

type Story struct {
	URL     string              `yaml:"url"`     // 请求的URL,支持正则表达式
	Method  []string            `yaml:"method"`  // 支持的请求方法
	Handler string              `yaml:"handler"` // 处理请求的方法名称,必须是对外公开的,即以大写开头
	Trials  map[string][]string `yaml:"trials"`  // 请求的预处理
}

Story url路径映射

type StoryHook

type StoryHook map[string]Hook // 基本信息,key:校验的名字,value:校验实现

Directories

Path Synopsis
Package examples @author K·J Create at 2019-01-11 13:56
Package examples @author K·J Create at 2019-01-11 13:56

Jump to

Keyboard shortcuts

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