-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmod.go
199 lines (183 loc) · 4.84 KB
/
mod.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package kuu
import (
"fmt"
"github.com/jinzhu/inflection"
"gopkg.in/guregu/null.v3"
"path"
"strings"
"sync"
"github.com/jinzhu/gorm"
)
var (
tableNames = make(map[string]string)
tableNameMetaMap = make(map[string]*Metadata)
modMap = make(map[string]*Mod)
)
var (
routesMap = make(map[string]RouteInfo)
routesMapMu sync.RWMutex
)
func init() {
gorm.DefaultTableNameHandler = func(db *gorm.DB, defaultTableName string) string {
if v, ok := tableNames[defaultTableName]; ok && v != "" {
return v
}
return defaultTableName
}
}
type tabler interface {
TableName() string
}
type dbTabler interface {
TableName(*gorm.DB) string
}
// HandlerFunc defines the handler used by ok middleware as return value.
type HandlerFunc func(*Context) *STDReply
// HandlersChain defines a HandlerFunc array.
type HandlersChain []HandlerFunc
// Mod
type Mod struct {
Code string
Prefix string
Middleware HandlersChain
Routes RoutesInfo
Models []interface{}
IntlMessages map[string]string
OnImport func() error
OnInit func() error
TablePrefix null.String
IgnoreCrudRoutes bool
IgnoreModRoutes bool
IgnoreDataDict bool
}
// Import
func (app *Engine) Import(mods ...*Mod) {
migrate := C().GetBool("gorm:migrate")
for _, mod := range mods {
for _, middleware := range mod.Middleware {
if middleware != nil {
app.Use(middleware)
}
}
}
for _, mod := range mods {
if mod.Code == "" {
PANIC("模块编码不能为空")
}
if len(mod.IntlMessages) > 0 {
AddDefaultIntlMessage(mod.IntlMessages)
}
mod.Code = strings.ToLower(mod.Code)
routePrefix := path.Join(C().GetString("prefix"), mod.Prefix)
for _, route := range mod.Routes {
if mod.IgnoreModRoutes {
continue
}
if route.Path == "" || route.HandlerFunc == nil {
PANIC("Route path and handler can't be nil")
}
if route.Name == "" {
PANIC("Need route name for system audit: %s %s", route.Method, route.Path)
}
if route.Method == "" {
route.Method = "GET"
}
route.Method = strings.ToUpper(route.Method)
var routePath string
if route.IgnorePrefix {
routePath = path.Join(route.Path)
} else {
routePath = path.Join(routePrefix, route.Path)
}
if route.Method == "*" {
app.Any(routePath, route.HandlerFunc)
for _, method := range []string{"GET", "POST", "PUT", "PATCH", "HEAD", "OPTIONS", "DELETE", "CONNECT", "TRACE"} {
key := fmt.Sprintf("%s %s", method, routePath)
routesMapMu.Lock()
routesMap[key] = route
routesMapMu.Unlock()
}
} else {
app.Handle(route.Method, routePath, route.HandlerFunc)
key := fmt.Sprintf("%s %s", route.Method, routePath)
routesMapMu.Lock()
routesMap[key] = route
routesMapMu.Unlock()
}
if len(route.IntlMessages) > 0 {
AddDefaultIntlMessage(route.IntlMessages)
}
}
for _, model := range mod.Models {
var desc *RestDesc
if !mod.IgnoreCrudRoutes {
desc = RESTful(app, routePrefix, model)
}
if meta := parseMetadata(model); meta != nil {
if !mod.IgnoreCrudRoutes {
meta.RestDesc = desc
}
meta.ModCode = mod.Code
gormTableName := gorm.ToTableName(meta.Name)
pluralTableName := inflection.Plural(gormTableName)
if v, ok := model.(tabler); ok {
tableName := v.TableName()
tableNames[tableName] = tableName
} else if v, ok := model.(dbTabler); ok {
tableName := v.TableName(DB())
tableNames[tableName] = tableName
} else if C().GetBool("legacy_table_name") {
tableName := fmt.Sprintf("%s_%s", mod.Code, meta.Name)
tableNames[gormTableName] = tableName
tableNames[pluralTableName] = tableName
} else {
var kuuTableName string
if v := mod.TablePrefix; v.Valid {
if v.String != "" {
kuuTableName = fmt.Sprintf("%s_%s", v.String, pluralTableName)
} else {
kuuTableName = pluralTableName
}
} else {
kuuTableName = fmt.Sprintf("%s_%s", mod.Code, pluralTableName)
}
tableNames[gormTableName] = kuuTableName
tableNames[pluralTableName] = kuuTableName
tableNames[kuuTableName] = kuuTableName
}
metaTableName := DB().NewScope(model).TableName()
if metaTableName != "" {
tableNameMetaMap[metaTableName] = meta
if !mod.IgnoreDataDict {
DefaultCache.HSet(
BuildKey("datadict"),
fmt.Sprintf("%s:%s", meta.ModCode, meta.Name),
JSONStringify(meta, true),
)
}
}
}
if migrate {
DB().AutoMigrate(model)
if v, ok := model.(DBTypeRepairer); ok {
v.RepairDBTypes()
}
}
}
if mod.OnImport != nil {
if err := mod.OnImport(); err != nil {
// 模块导入失败直接退出
panic(err)
}
}
modMap[mod.Code] = mod
}
}
// GetModPrefix
func GetModPrefix(modCode string) string {
mod, ok := modMap[modCode]
if ok {
return mod.Prefix
}
return ""
}