This repository has been archived by the owner on Mar 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
xweb.go
198 lines (165 loc) · 11.5 KB
/
xweb.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
// Package web is a lightweight web framework for Go. It's ideal for
// writing simple, performant backend web services.
package xweb
import (
"crypto/tls"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"github.com/go-xweb/log"
)
const (
Version = "0.2.1"
)
func redirect(w http.ResponseWriter, url string, status ...int) error {
s := 302
if len(status) > 0 {
s = status[0]
}
w.Header().Set("Location", url)
w.WriteHeader(s)
_, err := w.Write([]byte("Redirecting to: " + url))
return err
}
func Download(w http.ResponseWriter, fpath string) error {
f, err := os.Open(fpath)
if err != nil {
return err
}
defer f.Close()
fName := filepath.Base(fpath)
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%v\"", fName))
_, err = io.Copy(w, f)
return err
}
const (
defaultErrorTmpl = `<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8" />
<title>%d - %s</title>
<style type="text/css">
body{border:0;margin:0;padding:0;background:#eee url('data:image/gif;base64,R0lGODlhLQLeAIABAOrq6u7u7iH5BAEAAAEALAAAAAAtAt4AAAL/jI+py+0Po5y02ouz3rz7D4biSJbmi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xLquO99zEOk+NL7PvGTf7Pwh9/32Fv+4sg3/eFXzfzmU/74KU+49Inf3yFT/foVM7XruQn+8Iuf1UiP+vjPj/70k/33cU+7+t8P//iXSO6ARr38749/wvSb0uu0z7///x9N+yd1/QeABWiAHCGAYGd/B8iADYhv7md9EOiAE0iB4VZ+zVaBGaiB3md3EXh2GwiCIch/DidzkSaCJ4iCYHeBGJiCLeiCwbOC3/eCM0iDkhaDBFiDOaiD+DJ19ZeAOwiEQTgcPdhwCyiER5iDEmh2P4iETUiD3AeFUSiFU0iFVWiFV4iFWaiFW8iFXeiFXwiGYSiGY0iGZWiGZ4iGaaiGa8iGbeiGbwiHcSiHc0iHdWiHd4iHCVAAADs') right bottom;}
.error{width:600px;border:1px solid #ccc;margin:100px auto;border-radius:5px;box-shadow:0 0 10px #ccc;background:#fff url('data:image/gif;base64,R0lGODlhlwA8APeyAP/gy/+0gv/7+f/EnP+1g+Tx///k0srk///+/f+wfP/BmPj8/5vN///t4f/699fr/73e//+we6jU//H4//+xfev1/6/X//+zgP/+/qLR/8rHzf/Gn/+yf//cxP+zgf+4if+5iv/p2f/59f/fyrba///9+//8+v/UuP/Dm//9/N7u///NrP/Ttf+1hP/fyf/17//48//k0//izv/Wu//7+P/l1f/s3//t4MTh///8+9Do///17f/Hov+yfv+4iP+9kf/QsP/w5//Lqf/Jpf/17v/XvP/Orf/Alv/y6v/28P/j0f/49P/o2P/69v/hzP/awf/Bl//n1//StP/u4//i0P/48v/hzf/eyP/z6//awv/Orv+0g//ex//Gof+9kP/q3P/u4v/Hof/x6P+3hv/Rsv/r3v/38f/cxf/Yv//y6f+2heS5of/27//Io//Mqf/ZwP/dxv++lP+8j//j0P/bw//gzP/Pr//38v/Vuf/Fnv/Ss//m1f+7jv/GoP/Fnf+7jf/m1v/LqP/Ut//Jpv/q2/+3h//w5v/dx//Mqv/v5P/07P/p2v/Mq/Gyi//JpP/t4v/Kp/LFqf/s4Mq8vP/Wuv/iz/+8kKjT/v/r3f/Rs/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OpOJDmkC6sKGQlnsIVdhE+gvo3QBGIXQgzgLwSRZNAOK0CtxZiSjVdzEEJaFDQ4CEYE5L8VAfkYpASjYgExnOgSsYZEDRRCCK6OEhol8tCmKm9cSQeYQE4btTimw29aOkLdshi0BwqRdLEIwEYLCQncHwR1USKoQMEC0ZQPp3dYCOdFBRrVzlhDaQ9J21ekZZAFJaogDWrA1NVjRnls7xEV5edbAPYFNTUoIW5/CVYQgAApbi0MKBgKHrUXgC3s1qxyleoQ6QgQHlFLIo6wSwYQAYWulIMgfL+UBX7qUb6ssJBy14CmIuLOzCCFBmxhCh63/XW4godiaF7BpkKgFjgJe861jL8CHQOAwIu5UWkMmdRXYGoQIAIiudKcb3REkcSA5kAF1tzsCMSZkB9sN73RlsFhZgFe806UCIMrwAlI9xJ3wZMhQm0K/c9m3IlplilsbMth3MvK+AG5IBVoDFVAyxJ1PCWqAF3yQCRRAB7K1CglUUID/zqsAKsDBfBP3gAoz+MPFIQ0sYGtVETPFFOhNsYpXzOIWu1jFI4iCQPLrGAObOCqTkKqOd8zjsMVBIP2dS309eWOnjKLHSE4ykqEgkBI7prDMLXJTGqHkKluZb0cQSAG2zOUue9nLBnHwl8f8ZVGs4cwBSLOa18zmNrv5HM1wjrOc50znNufhw3jOs573zOc++/nPgGZwQAAAOw') no-repeat right bottom;}
.error-body,.error-foot{margin:0 10px 10px 10px;}
.error-body{margin-top:10px;min-height:180px}
.error-head h1{padding:10px 10px 0 10px;margin:0 2px;vertical-align:middle;border-bottom:1px solid #eee;}
.framework{color:#eee;font-size:11px}
</style>
<body>
<div class="error">
<div class="error-head">
<h1>%d - %s</h1>
</div>
<div class="error-body">%s</div>
<div class="error-foot">
<input type="button" title="Back" value="返回" onclick="history.go(-1)"/>
<em class="framework">(xweb v%s)</em>
</div>
</div>
</body>
</html>`
)
var errorTmpl string = ""
func Error(w http.ResponseWriter, status int, content string) error {
return mainServer.error(w, status, content)
}
// Process invokes the main server's routing system.
func Process(c http.ResponseWriter, req *http.Request) {
mainServer.Process(c, req)
}
// Run starts the web application and serves HTTP requests for the main server.
func Run(addr string) {
mainServer.Run(addr)
}
func SimpleTLSConfig(certFile, keyFile string) (*tls.Config, error) {
config := &tls.Config{}
if config.NextProtos == nil {
config.NextProtos = []string{"http/1.1"}
}
var err error
config.Certificates = make([]tls.Certificate, 1)
config.Certificates[0], err = tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
return nil, err
}
return config, nil
}
// RunTLS starts the web application and serves HTTPS requests for the main server.
func RunTLS(addr string, config *tls.Config) {
mainServer.RunTLS(addr, config)
}
// RunScgi starts the web application and serves SCGI requests for the main server.
func RunScgi(addr string) {
mainServer.RunScgi(addr)
}
// RunFcgi starts the web application and serves FastCGI requests for the main server.
func RunFcgi(addr string) {
mainServer.RunFcgi(addr)
}
// Close stops the main server.
func Close() {
mainServer.Close()
}
func AutoAction(c ...interface{}) {
mainServer.AutoAction(c...)
}
func AddAction(c ...interface{}) {
mainServer.AddAction(c...)
}
func AddTmplVar(name string, varOrFun interface{}) {
mainServer.AddTmplVar(name, varOrFun)
}
func AddTmplVars(t *T) {
mainServer.AddTmplVars(t)
}
func AddRouter(url string, c interface{}) {
mainServer.AddRouter(url, c)
}
func AddFilter(filter Filter) {
mainServer.AddFilter(filter)
}
func AddApp(a *App) {
mainServer.AddApp(a)
}
func AddConfig(name string, value interface{}) {
mainServer.AddConfig(name, value)
}
func AddHook(name string, fns ...interface{}) {
XHook.Bind(name, fns...)
}
func SetTemplateDir(dir string) {
mainServer.SetTemplateDir(dir)
}
func SetStaticDir(dir string) {
mainServer.SetStaticDir(dir)
}
// SetLogger sets the logger for the main server.
func SetLogger(logger *log.Logger) {
mainServer.SetLogger(logger)
}
func MainServer() *Server {
return mainServer
}
func RootApp() *App {
return mainServer.RootApp
}
func Serv(name string) *Server {
server, ok := Servers[name]
if ok {
return server
}
return nil
}
// Config is the configuration of the main server.
var (
Config *ServerConfig = &ServerConfig{
RecoverPanic: true,
EnableGzip: true,
//Profiler: true,
StaticExtensionsToGzip: []string{".css", ".js"},
}
Servers map[string]*Server = make(map[string]*Server) //[SWH|+]
mainServer *Server = NewServer("main")
)