-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.go
187 lines (175 loc) · 4.72 KB
/
base.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
package www
import (
"github.com/ltto/T/www/conf"
"io"
"net/http"
"os"
"path"
"path/filepath"
"strings"
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/cookie"
"github.com/gin-gonic/gin"
"github.com/ltto/T/www/rest"
)
var (
Engine *gin.Engine
)
func init() {
Engine = gin.New()
store := cookie.NewStore([]byte("secret"))
Engine.Use(sessions.Sessions(SessionID, store))
}
func Run() error {
Engine.Static(conf.Conf.GetStaticPath(), conf.Conf.GetStaticLocal())
return Engine.Run(conf.Conf.Server.HostPort)
}
type RouterInfo struct {
Mapping string
HttpMethod string
Routes gin.IRoutes
Do interface{}
f *Func
}
func GetMapping(routes gin.IRoutes, Mapping string, Do interface{}) *RouterInfo {
r := &RouterInfo{Mapping: Mapping, HttpMethod: http.MethodGet, Do: Do, Routes: routes}
Router(r)
return r
}
func PostMapping(routes gin.IRoutes, Mapping string, Do interface{}) *RouterInfo {
r := &RouterInfo{Mapping: Mapping, HttpMethod: http.MethodPost, Do: Do, Routes: routes}
Router(r)
return r
}
func DeleteMapping(routes gin.IRoutes, Mapping string, Do interface{}) *RouterInfo {
r := &RouterInfo{Mapping: Mapping, HttpMethod: http.MethodDelete, Do: Do, Routes: routes}
Router(r)
return r
}
func PutMapping(routes gin.IRoutes, Mapping string, Do interface{}) *RouterInfo {
r := &RouterInfo{Mapping: Mapping, HttpMethod: http.MethodPut, Do: Do, Routes: routes}
Router(r)
return r
}
func Get(Mapping string, Do interface{}) *RouterInfo {
return GetMapping(nil, Mapping, Do)
}
func Post(Mapping string, Do interface{}) *RouterInfo {
return PostMapping(nil, Mapping, Do)
}
func Delete(Mapping string, Do interface{}) *RouterInfo {
return DeleteMapping(nil, Mapping, Do)
}
func Put(routes gin.IRoutes, Mapping string, Do interface{}) *RouterInfo {
return PutMapping(routes, Mapping, Do)
}
func Router(info *RouterInfo) {
pathMap := path.Join(conf.Conf.Server.Base, info.Mapping)
if info.Routes == nil {
info.Routes = Engine
}
switch info.HttpMethod {
case http.MethodGet:
info.Routes.GET(pathMap, info.ginFunc())
case http.MethodPost:
info.Routes.POST(pathMap, info.ginFunc())
case http.MethodPut:
info.Routes.PUT(pathMap, info.ginFunc())
case http.MethodDelete:
info.Routes.DELETE(pathMap, info.ginFunc())
}
}
func (info *RouterInfo) ginFunc() func(c *gin.Context) {
info.f = NewFunc(info.Do)
return func(c *gin.Context) {
var (
session = sessions.Default(c)
ctx = &Context{Context: c, Session: session}
do interface{}
err error
)
session.Options(sessions.Options{MaxAge: 60 * 60})
c.Writer.Header().Add("Cache-Control", "no-cache, no-store, must-revalidate")
c.Writer.Header().Add("Access-Control-Allow-Headers", "*")
c.Writer.Header().Add("Pragma", "no-cache")
c.Writer.Header().Add("Expires", "0")
c.Set("routerInfo", info)
if do, err = info.f.Call(ctx); err != nil {
DefaultErrHandler(ctx, err)
}
if err := session.Save(); err != nil {
DefaultErrHandler(ctx, err)
return
}
switch t := do.(type) {
case *os.File:
defer t.Close()
c.Writer.Header().Add("Content-Disposition", "attachment; filename="+filepath.Base(t.Name()))
ext := filepath.Ext(t.Name())
c.Writer.Header().Add("Content-Type", ContentType(ext))
if _, err := io.Copy(c.Writer, t); err != nil {
DefaultErrHandler(ctx, err)
return
}
case string:
stringHdl(ctx, t)
case *string:
s := *t
stringHdl(ctx, s)
case io.ReadCloser:
defer t.Close()
if _, err := io.Copy(c.Writer, t); err != nil {
DefaultErrHandler(ctx, err)
return
}
case io.Reader:
if _, err := io.Copy(c.Writer, t); err != nil {
DefaultErrHandler(ctx, err)
return
}
case error:
DefaultErrHandler(ctx, t)
return
case nil:
default:
c.JSON(http.StatusOK, do)
}
}
}
func stringHdl(c *Context, s string) {
var org = s
if ss, ok := rest.Redirect(s); ok {
c.Redirect(http.StatusMovedPermanently, ss)
return
}
var ok bool
if s, ok = rest.File(org); ok {
contentType := ContentType(filepath.Ext(s))
c.Writer.Header().Add("Content-Type", contentType)
if strings.Contains(contentType, "image") {
c.Writer.Header().Add("Content-Disposition", "filename="+filepath.Base(s))
} else {
c.Writer.Header().Add("Content-Disposition", "attachment;filename="+filepath.Base(s))
}
http.ServeFile(c.Writer, c.Request, s)
return
}
if s, ok = rest.Html(org); ok {
get := c.CParams()
c.HTML(http.StatusOK, s, get)
return
}
if strings.ToLower(path.Ext(org)) == ".html" {
get := c.CParams()
c.HTML(http.StatusOK, org, get)
return
}
{
paramsMap := c.CParams()
params := make([]interface{}, 0, len(paramsMap))
for k := range paramsMap {
params = append(params, paramsMap[k])
}
c.String(http.StatusOK, org, params...)
}
}