Initial commit

This commit is contained in:
Donny
2019-04-22 20:46:32 +08:00
commit 49ab8aadd1
25441 changed files with 4055000 additions and 0 deletions

View File

@@ -0,0 +1,51 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = ["supervisor_test.go"],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
)
go_library(
name = "go_default_library",
srcs = ["supervisor.go"],
importpath = "go-common/library/net/http/blademaster/middleware/supervisor",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//library/ecode:go_default_library",
"//library/net/http/blademaster:go_default_library",
],
)
go_test(
name = "go_default_xtest",
srcs = ["example_test.go"],
tags = ["automanaged"],
deps = [
"//library/net/http/blademaster:go_default_library",
"//library/net/http/blademaster/middleware/supervisor:go_default_library",
],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,9 @@
### business/blademaster/supervisor
##### Version 1.0.1
1. 使用 ecode 来拒绝请求
##### Version 1.0.0
1. 完成基本功能与测试

View File

@@ -0,0 +1,5 @@
# Author
lintnaghui
# Reviewer
maojian

View File

@@ -0,0 +1,7 @@
# See the OWNERS docs at https://go.k8s.io/owners
approvers:
- lintnaghui
reviewers:
- lintnaghui
- maojian

View File

@@ -0,0 +1,13 @@
#### business/blademaster/supervisor
##### 项目简介
blademaster 的 supervisor middleware主要用于配置路由的开放策略
##### 编译环境
- **请只用 Golang v1.8.x 以上版本编译执行**
##### 依赖包
- No other dependency

View File

@@ -0,0 +1,28 @@
package supervisor_test
import (
"go-common/library/net/http/blademaster"
"go-common/library/net/http/blademaster/middleware/supervisor"
"time"
)
// This example create a supervisor middleware instance and attach to a blademaster engine,
// it will allow '/ping' API can be requested with specified policy.
// This example will block all http method except `GET` on '/ping' API in next hour,
// and allow in further.
func Example() {
now := time.Now()
end := now.Add(time.Hour * 1)
spv := supervisor.New(&supervisor.Config{
On: true,
Begin: now,
End: end,
})
engine := blademaster.Default()
engine.Use(spv)
engine.GET("/ping", func(c *blademaster.Context) {
c.String(200, "%s", "pong")
})
engine.Run(":18080")
}

View File

@@ -0,0 +1,56 @@
package supervisor
import (
"time"
"go-common/library/ecode"
bm "go-common/library/net/http/blademaster"
)
// Config supervisor conf.
type Config struct {
On bool // all post/put/delete method off.
Begin time.Time // begin time
End time.Time // end time
}
// Supervisor supervisor midleware.
type Supervisor struct {
conf *Config
on bool
}
// New new and return supervisor midleware.
func New(c *Config) (s *Supervisor) {
s = &Supervisor{
conf: c,
}
s.Reload(c)
return
}
// Reload reload supervisor conf.
func (s *Supervisor) Reload(c *Config) {
if c == nil {
return
}
s.on = c.On && c.Begin.Before(c.End)
s.conf = c // NOTE datarace but no side effect.
}
func (s *Supervisor) ServeHTTP(c *bm.Context) {
if s.on {
now := time.Now()
method := c.Request.Method
if s.forbid(method, now) {
c.JSON(nil, ecode.ServiceUpdate)
c.Abort()
return
}
}
}
func (s *Supervisor) forbid(method string, now time.Time) bool {
// only allow GET request.
return method != "GET" && now.Before(s.conf.End) && now.After(s.conf.Begin)
}

View File

@@ -0,0 +1,55 @@
package supervisor
import (
"testing"
"time"
)
func create() *Supervisor {
now := time.Now()
end := now.Add(time.Hour * 1)
conf := &Config{
On: true,
Begin: now,
End: end,
}
return New(conf)
}
func TestSupervisor(t *testing.T) {
sv := create()
in := sv.conf.Begin.Add(time.Second * 10)
out := sv.conf.End.Add(time.Second * 10)
if sv.forbid("GET", in) {
t.Error("Request should never be blocked on GET method")
}
if !sv.forbid("POST", in) {
t.Errorf("Request should be blocked on POST method at %+v", in)
}
if sv.forbid("POST", out) {
t.Errorf("Request should not be blocked at %+v", out)
}
}
func TestReload(t *testing.T) {
zero := time.Unix(0, 0)
conf := &Config{
On: false,
Begin: zero,
End: zero,
}
sv := create()
// reload with nil
sv.Reload(nil)
// reload with valid config
sv.Reload(conf)
if sv.conf != conf && sv.on == false {
t.Errorf("Failed to reload config %+v, current config is %+v", conf, sv.conf)
}
}