-
Notifications
You must be signed in to change notification settings - Fork 8
/
app.go
76 lines (69 loc) · 1.98 KB
/
app.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
/**
* @ Author: Daniel Tan
* @ Date: 2020-04-07 09:41:11
* @ LastEditTime: 2020-07-29 19:03:26
* @ LastEditors: Daniel Tan
* @ Description:
* @ FilePath: /trinitygo/application/app.go
* @
*/
package application
import (
"context"
"github.com/PolarPanda611/trinitygo/keyword"
truntime "github.com/PolarPanda611/trinitygo/runtime"
"github.com/casbin/casbin/v2"
"github.com/gin-gonic/gin"
"github.com/PolarPanda611/trinitygo/conf"
"github.com/jinzhu/gorm"
"github.com/kataras/golog"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)
// Application global app interface
type Application interface {
IsLogSelfCheck() bool
Logger() *golog.Logger
RuntimeKeys() []truntime.RuntimeKey
Conf() conf.Conf
Keyword() keyword.Keyword
ContextPool() *ContextPool
DB() *gorm.DB
Enforcer() *casbin.Enforcer
InstallDB(f func() *gorm.DB)
ControllerPool() *ControllerPool
InstancePool() *InstancePool
UseInterceptor(interceptor ...grpc.UnaryServerInterceptor) Application
UseMiddleware(middleware ...gin.HandlerFunc) Application
RegRuntimeKey(runtime ...truntime.RuntimeKey) Application
InitGRPC()
InitHTTP()
InitRouter()
GetGRPCServer() *grpc.Server
ServeGRPC()
ServeHTTP()
ResponseFactory() func(status int, res interface{}, runtime map[string]string) interface{}
}
// DecodeGRPCRuntimeKey decode runtime key from ctx
func DecodeGRPCRuntimeKey(ctx context.Context, runtimeKey []truntime.RuntimeKey) map[string]string {
runtimeKeyMap := make(map[string]string)
if ctx != nil {
md, ok := metadata.FromIncomingContext(ctx)
if ok {
for _, v := range runtimeKey {
runtimeKeyMap[v.GetKeyName()] = md[v.GetKeyName()][0]
}
}
}
return runtimeKeyMap
}
// DecodeHTTPRuntimeKey decode http runtime
func DecodeHTTPRuntimeKey(c *gin.Context, runtimeKey []truntime.RuntimeKey) map[string]string {
runtimeKeyMap := make(map[string]string)
if c != nil {
for _, v := range runtimeKey {
runtimeKeyMap[v.GetKeyName()] = c.GetString(v.GetKeyName())
}
}
return runtimeKeyMap
}