-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
108 lines (98 loc) · 2.87 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"github.com/billyyoyo/microj/app"
"github.com/billyyoyo/microj/config"
"github.com/billyyoyo/microj/examples/gatewaysrv/codes"
"github.com/billyyoyo/microj/examples/rpcsrv/proto"
"github.com/billyyoyo/microj/logger"
_ "github.com/billyyoyo/microj/plugins/broker/nats"
_ "github.com/billyyoyo/microj/plugins/config/etcd"
_ "github.com/billyyoyo/microj/plugins/registry/etcd"
"github.com/billyyoyo/microj/server/gateway"
"github.com/billyyoyo/microj/util"
"github.com/valyala/fasthttp"
"net/http"
"strings"
)
var (
conf *GatewayConfig
)
type GatewayConfig struct {
Routes []gateway.Route `yaml:"route" mapstructure:"route"`
WhiteList []string `yaml:"white-list" mapstructure:"white-list"`
}
func (g *GatewayConfig) CanRefresh() bool {
return true
}
func (g *GatewayConfig) KeyName() string {
return "gateway"
}
func main() {
app.NewApplication().
Init(func() {
var c GatewayConfig
err := config.ScanWithRefresh(&c)
if err != nil {
logger.Fatal("gateway config load failed", err)
}
conf = &c
}).
WithServer(gateway.NewGateway().
AddInterceptor(-11, fasthttp.CompressHandler).
AddInterceptor(-10, gateway.RecoverHandler).
AddInterceptor(-9, gateway.LogHandler).
AddInterceptor(1, MatchHandler).
AddInterceptor(3, AuthHandler).
AddInterceptor(4, PermissionHandler).
AddRpcEndpoint(proto.NewExampleGw()).
AddRpcEndpoint(proto.NewTestGw()).
AddRpcEndpoint(proto.NewHelloGw())).
Run()
}
func MatchHandler(next fasthttp.RequestHandler) fasthttp.RequestHandler {
return func(ctx *fasthttp.RequestCtx) {
path := util.Bytes2str(ctx.Path())
for _, r := range conf.Routes {
if strings.HasPrefix(path, r.Path) {
ctx.Request.Header.Set(gateway.MICRO_SERVICE_NAME, r.Id)
ctx.Request.Header.Set(gateway.MICRO_SERVICE_PATH, r.Path)
ctx.Request.Header.Set(gateway.MICRO_SERVICE_SCHEMA, r.Schema)
next(ctx)
return
}
}
ctx.Error("non router exist", http.StatusNotFound)
}
}
func AuthHandler(next fasthttp.RequestHandler) fasthttp.RequestHandler {
return func(ctx *fasthttp.RequestCtx) {
whitelist := []string{"/server-api/login"}
originPath := util.Bytes2str(ctx.Request.URI().Path())
for _, p := range whitelist {
if p == originPath {
next(ctx)
return
}
}
token := util.Bytes2str(ctx.Request.Header.Peek("Authorization"))
if token == "" {
gateway.RetFailed(ctx, codes.ERR_UNAUTHORIZATION, "unauthenticate")
return
}
next(ctx)
}
}
func PermissionHandler(next fasthttp.RequestHandler) fasthttp.RequestHandler {
return func(ctx *fasthttp.RequestCtx) {
permissionlist := []string{"/edit"}
path := util.Bytes2str(ctx.Path())
token := util.Bytes2str(ctx.Request.Header.Peek("Authorization"))
for _, p := range permissionlist {
if p == path && token == "123" {
gateway.RetFailed(ctx, codes.ERR_NO_PERMISSION, "no permission")
return
}
}
next(ctx)
}
}