micro

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2020 License: Apache-2.0 Imports: 6 Imported by: 1

README

micro

simple base RESTFUL framework

Quick Start

package main

import (
	"encoding/json"
	"fmt"
	"net/http"
	"test/handlers"

	"github.com/byronzr/micro"
	"github.com/byronzr/micro/helper"
)

// 定义一个请求方式
type GET struct{}

func main() {
  // 注册 handler
  // 注册过程中可进行分组 
	service := micro.Register(GET{}, "byron", handlers.POST{}) // must be first
  
  // 注册全局中间件 before / after
	bf := func(m *helper.MicroRequest) bool {
		fmt.Println("i'm running before <GLB>")
		return true
	}
	af := func(m *helper.MicroRequest) bool {
		fmt.Println("i'm running after <GLB>")
		return true
	}
	service.Before(bf)
	service.After(af)

  // 启动服务(端口,超时)
	service.Start(8000, 10)
}

// (无效的) handler V1
func (GET) Check(r *http.Request) (response []byte, err error) {
	msg := "GET.Check"
	return json.Marshal(msg)
}

// 局部中间件
func (GET) Before(m *helper.MicroRequest) bool {
	// 设置与传递值
	mid := m.M.Before()
	mid.Set("value from partial get.")
	// false, 中断执行
	return true
}

func (GET) After(m *helper.MicroRequest) bool {
	// 获取中间件的传递值
	mid := m.M.Before()
	v, ok := mid.Value()
	if ok {
		fmt.Printf("after: %s\n", v)
	}
	return true
}

// 常规 handler
func (GET) FullCheck(m *helper.MicroRequest) int {
	content := "fulll check."
	l, err := m.W.Write([]byte(content))
	if err != nil {
		panic(err)
	}
	// 返回写入长度
	return l
}

runtime
➜  example git:(V2) ✗ go run main.go
┌────────┬─────────────────────────────┐
│ Type   │ Register                    │
├────────┼─────────────────────────────┤
│ MIDDLE │ GET before                  │
│ MIDDLE │ GET after                   │
│ MIDDLE │ GLB before                  │
│ MIDDLE │ GLB after                   │
│ ACTION │ GET /full/check             │
│ ACTION │ POST /byron/report/thisweek │
└────────┴─────────────────────────────┘
INF 2020/09/03 12:04:18 :::::: service [ 8000 ] start ::::::
i'm running before <GLB>
INF 2020/09/03 12:04:23 GET /full/check write:12b t:45.292µs
i'm running after <GLB>
after: value from partial get.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type SERVICE added in v0.1.6

type SERVICE struct {
	Mux *http.ServeMux
}

func Register added in v0.2.1

func Register(hands ...interface{}) *SERVICE

register handlers

func (*SERVICE) After added in v1.0.0

func (s *SERVICE) After(f func(*helper.MicroRequest) bool) *SERVICE

global after call

func (*SERVICE) Before added in v1.0.0

func (s *SERVICE) Before(f func(*helper.MicroRequest) bool) *SERVICE

global before call

func (*SERVICE) Start added in v0.1.6

func (s *SERVICE) Start(port, timeout int)

service start

type TableInfo added in v1.0.0

type TableInfo struct {
	Type     string
	Register string
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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