micro

package module
v3.0.7+incompatible Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2021 License: Apache-2.0 Imports: 12 Imported by: 2 modules, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

micro

simple base RESTFUL framework

Quick Start

package main

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

	"github.com/byronzr/micro"
)

// request method set
type GET struct{}

func main() {
	service := micro.Register(GET{}, "byron", handlers.POST{}) // must be first
	bf := func(m *micro.MicroRequest) bool {
		fmt.Println("i'm running before <GLB>")
		return true
	}
	af := func(m *micro.MicroRequest) bool {
		fmt.Println("i'm running after <GLB>")
		return true
	}
    
    // 添加一个默认路由
	// 返回 -1 则是 404
	service.Default(func(m *micro.MicroRequest) int {
		if len(m.R.URL.Query().Get("test")) == 0 {
			return -1
		}
		return GET{}.FullCheck(m)
	})

	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 *micro.MicroRequest) bool {
	// 设置与传递值
	mid := m.M.Before()
	mid.Set("value from partial get.")
	// false, 中断执行
	return true
}

func (GET) After(m *micro.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 *micro.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
➜  example git:(master) ✗ go run main.go
┌────────┬─────────────────────────────┐
│ Type   │ Register                    │
├────────┼─────────────────────────────┤
│ MIDDLE │ GLOBAL after                │
│ MIDDLE │ GET before                  │
│ MIDDLE │ GET after                   │
│ MIDDLE │ GLOBAL before               │
│ ACTION │ GET /full/check             │
│ ACTION │ POST /byron/report/thisweek │
└────────┴─────────────────────────────┘
INF 2020/09/07 10:24:03 :::::: service [ 8000 ] start ::::::

changelog

2020-01-09 add Fail / Success / Standard Response


type Response struct {
	Code   int         `json:"code"`
	Msg    string      `json:"msg"`
	Result interface{} `json:"result"`
}

func (POST) ResponseJsonFail(m *micro.MicroRequest) int {
	micro.Err("check this longfile flag.")
	return m.Fail("fail", -1)
}

//{
//  "code": -1,
//  "msg": "fail",
//  "result": null
//}

func (POST) ResponseJsonSuccess(m *micro.MicroRequest) int {
	result := "this is result."
	return m.Success("success", result)
}

//{
//  "code": 0,
//  "msg": "success",
//  "result": "this is result."
//}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// V2
	ActionFuncMap = make(map[string]func(*MicroRequest) int, 0)
	MiddleFuncMap = make(map[string]func(*MicroRequest) bool, 0)
)

request method set

Functions

func DD

func DD(o ...interface{})

func Err

func Err(msg ...interface{})

func Inf

func Inf(msg ...interface{})

func PPP

func PPP(msg ...interface{})

func PackSize

func PackSize(v int, uint string) string

auto covert content length B/KB/MB/GB/TB

func RegisterHandler

func RegisterHandler(h interface{})

func SS

func SS(msg ...interface{})

func Wrn

func Wrn(msg ...interface{})

Types

type AfterCall

type AfterCall interface {
	After(*MicroRequest) bool
}

middle function call

type BeforeCall

type BeforeCall interface {
	Before(*MicroRequest) bool
}

middle function call on before

type MicroRequest

type MicroRequest struct {
	R *http.Request
	W http.ResponseWriter
	M *Middle
}

func (*MicroRequest) Fail

func (m *MicroRequest) Fail(msg string, code int) int

func (*MicroRequest) Success

func (m *MicroRequest) Success(msg string, result interface{}) int

type Middle

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

func (*Middle) After

func (m *Middle) After() *Middle

func (*Middle) Before

func (m *Middle) Before() *Middle

func (*Middle) Global

func (m *Middle) Global() *Middle

func (*Middle) Set

func (m *Middle) Set(v interface{}) (update bool, err error)

func (*Middle) Value

func (m *Middle) Value() (v interface{}, has bool)

type ROUTER

type ROUTER struct{}

func (ROUTER) CrossHeader

func (ROUTER) CrossHeader(w http.ResponseWriter)

func (ROUTER) ServeHTTP

func (ROUTER) ServeHTTP(w http.ResponseWriter, r *http.Request)

type Response

type Response struct {
	Code   int         `json:"code"`
	Msg    string      `json:"msg"`
	Result interface{} `json:"result"`
}

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(*MicroRequest) bool) *SERVICE

global after call

func (*SERVICE) Before added in v1.0.0

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

global before call

func (*SERVICE) Default

func (s *SERVICE) Default(f func(*MicroRequest) int) *SERVICE

default router "/"

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
⌂ 换网址