-
Notifications
You must be signed in to change notification settings - Fork 7
/
baseMiddleware.go
91 lines (82 loc) · 1.75 KB
/
baseMiddleware.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
/**
* @Author: DollarKiller
* @Description: 基础中间件
* @Github: https://github.com/dollarkillerx
* @Date: Create in 10:55 2019-09-30
*/
package erguotou
import (
"github.com/dollarkillerx/erguotou/clog"
"github.com/dollarkillerx/erguotou/local"
"io/ioutil"
"log"
"sync"
)
var localOnce sync.Once
func init() {
localOnce = sync.Once{}
}
func Logger(ctx *Context) {
path := ctx.Ctx.Path()
var head string
switch {
case ctx.Ctx.IsGet():
head = "Get: "
case ctx.Ctx.IsPost():
head = "Post: "
case ctx.Ctx.IsDelete():
head = "Delete: "
case ctx.Ctx.IsPut():
head = "Put: "
}
log.Println(head + string(path))
ctx.Next()
}
func Local(language string) func(ctx *Context) {
return func(ctx *Context) {
lang := ctx.GetCookie("language")
if lang == "" {
ctx.SetCookie("language", language)
} else {
if lang != language {
language = lang
}
}
if ctx.engine.Option.Debug {
init := local.LocalInit()
e := init.Init("local/local.json")
if e != nil {
e := ioutil.WriteFile("local/local.json", []byte(local.Source), 00755)
if e != nil {
log.Fatal("Localization is initialized")
}
}
i, ok := init.SourceMap[language]
if ok {
ctx.Data("local", i)
} else {
log.Println(init.SourceMap)
clog.PrintWa("No language pack")
}
} else {
localOnce.Do(func() {
init := local.LocalInit()
e := init.Init("local/local.json")
if e != nil {
e := ioutil.WriteFile("local/local.json", []byte(local.Source), 00755)
if e != nil {
log.Fatal("Localization is initialized")
}
}
i, ok := init.SourceMap[language]
if ok {
ctx.Data("local", i)
} else {
log.Println(init.SourceMap)
clog.PrintWa("No language pack")
}
})
}
ctx.Next()
}
}