/
gower.go
337 lines (284 loc) · 8.45 KB
/
gower.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// The MIT License (MIT)
// Copyright (c) 2014 Tony Walker
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package gower
import (
"encoding/json"
"flag"
"fmt"
"html/template"
"log"
"net/http"
"os"
"path/filepath"
"regexp"
"runtime"
"strconv"
"time"
"github.com/flosch/pongo2"
)
const (
RED = "\033[1;31m%s\033[0m"
GREEN = "\033[1;32m%s\033[0m"
)
// ------
// Types
// ------
// Server Configuration
type Config struct {
Port int
Host string
StaticDir string
TemplateDir string
Csrf bool
Routes []*Route
ColoredLog bool
Debug bool
}
// Context contains the `Request`, `Response` and `Matches` vars
type Context struct {
Res http.ResponseWriter
Req *http.Request
Matches []string
}
// Route contains the compiled pattern for the url, the method and the handler
type Route struct {
re *regexp.Regexp // url pattern to match
method string // request method
handler func(*Context) // func to run
}
var templateCache = make(map[string]pongo2.Template)
var ServerConfig = &Config{}
var ServerStat = NewStat()
// ----------------
// Context Methods
// ----------------
// Write plain text response
func (c *Context) Write(data ...interface{}) {
c.Res.Header().Set("Content-Type", "text/plain")
fmt.Fprint(c.Res, data...)
}
//
func renderTemplate(filepath string, data map[string]interface{}) []byte {
var out string
var err error
var template pongo2.Template
// Read the template from the disk every time
if ServerConfig.Debug {
newTemplate, err := pongo2.FromFile(filepath)
if err != nil {
panic(err)
}
template = *newTemplate
} else {
// Read the template and cache it
cached, ok := templateCache[filepath]
if ok == false {
newTemplate, err := pongo2.FromFile(filepath)
if err != nil {
panic(err)
}
templateCache[filepath] = *newTemplate
cached = *newTemplate
}
template = cached
}
out, err = template.Execute(data)
if err != nil {
panic(err)
}
return []byte(out)
}
// Render template and write it to Response
func (c *Context) WriteTemplate(filename string, data map[string]interface{}) {
c.Res.Header().Set("Content-Type", "text/html; charset=utf-8")
filepath := ServerConfig.TemplateDir + "/" + filename
str := renderTemplate(filepath, data)
c.Res.Write(str)
}
// Write json response
func (c *Context) WriteJson(data interface{}) {
c.Res.Header().Set("Content-Type", "application/json; charset=utf-8")
out, err := json.Marshal(data)
if err != nil {
panic(err)
}
c.Res.Write(out)
}
// --------
// Methods
// --------
// Create a Config instance
func NewConfig() *Config {
return &Config{}
}
// Create a new Route
func NewRoute(pattern string, method string, handler func(*Context)) *Route {
re, err := regexp.Compile("^" + pattern + "$")
if err != nil {
log.Fatal(err)
}
return &Route{re, method, handler}
}
// Load templates from path
func NewTemplates(path string) *template.Template {
pattern := filepath.Join(path, "*.html")
return template.Must(template.ParseGlob(pattern))
}
// Handler to server static content
func ServeStatic() http.Handler {
cwd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
staticDir := filepath.Join(cwd, ServerConfig.StaticDir)
return http.StripPrefix("/static/", http.FileServer(http.Dir(staticDir)))
}
// Register new route
func RegisterRoute(path string, method string, fun func(c *Context)) {
route := NewRoute(path, method, fun)
ServerConfig.Routes = append(ServerConfig.Routes, route)
}
// -------------
// HTTP Methods
// -------------
// Create and register a new GET route
func Get(path string, fun func(c *Context)) {
RegisterRoute(path, "GET", fun)
}
// Create and register a new POST route
func Post(path string, fun func(c *Context)) {
RegisterRoute(path, "POST", fun)
}
// Create and register a new DELETE route
func Delete(path string, fun func(c *Context)) {
RegisterRoute(path, "DELETE", fun)
}
// Create and register a new PUT route
func Put(path string, fun func(c *Context)) {
RegisterRoute(path, "PUT", fun)
}
// ----------
// OTHER
// ----------
// Safely process a request and recover if necessary
func Process(w http.ResponseWriter, r *http.Request) {
reqStarted := time.Now()
// Recover if `process` errors
defer func() {
if err := recover(); err != nil {
// Append the error to the method for logging purposes
log.Println(err)
method := fmt.Sprintf("%s (%s)", r.Method, err)
logRequest(r.URL.Path, method, r.RemoteAddr, false, time.Since(reqStarted))
http.Error(w, "Server Error", 500)
ServerStat.Increment(500, time.Since(reqStarted))
}
}()
process(w, r)
}
// Process a request
func process(w http.ResponseWriter, r *http.Request) {
reqStarted := time.Now()
var foundPattern bool = false
var foundMethod bool = false
var routeToExecute *Route
var foundMatches []string
// Find the Route to serve this request
for _, route := range ServerConfig.Routes {
if matches := route.re.FindStringSubmatch(r.URL.Path); matches != nil {
foundPattern = true
if foundPattern {
foundMethod = (route.method == r.Method)
}
if foundPattern && foundMethod {
routeToExecute = route
foundMatches = matches
break
}
}
}
statusCode := 200
// Not found
if !foundPattern {
http.Error(w, "Not Found", 404)
statusCode = 404
} else if !foundMethod { // Invalid Method
http.Error(w, "Method not allowed", 405)
statusCode = 405
}
// If all is good
if foundMethod && foundPattern {
// Execute the routes handler
context := &Context{Res: w, Req: r, Matches: foundMatches}
routeToExecute.handler(context)
}
duration := time.Since(reqStarted)
if ServerConfig.Debug {
logRequest(r.URL.Path, r.Method, r.RemoteAddr, statusCode == 200, duration)
}
ServerStat.Increment(statusCode, duration)
}
// Log an incoming request (with colors if requested)
func logRequest(path string, method string, ip string, success bool, duration time.Duration) {
if ServerConfig.ColoredLog {
if success {
method = fmt.Sprintf(GREEN, string(method))
} else {
method = fmt.Sprintf(RED, string(method))
}
}
log.Println(method, path, ip, duration)
}
// Display system information
func showInfo() {
fmt.Printf("\nRuntime\n\n")
fmt.Printf("* %-13v: %v\n", "PID", os.Getpid())
fmt.Printf("* %-13v: %v\n", "Host", ServerConfig.Host)
fmt.Printf("* %-13v: %v\n", "CPUs", runtime.NumCPU())
fmt.Printf("* %-13v: %v\n", "Arch", runtime.GOARCH)
fmt.Printf("* %-13v: %v\n", "OS", runtime.GOOS)
fmt.Printf("* %-13v: %v\n", "Go version", runtime.Version())
fmt.Printf("* %-13v: %v\n", "Goroutines", runtime.NumGoroutine())
fmt.Print("\nServing\n\n")
}
// Start the server
func Start() {
showInfo()
// Listen and serve
http.Handle("/static/", ServeStatic())
http.HandleFunc("/", Process)
log.Fatal(http.ListenAndServe(ServerConfig.Host, nil))
}
// Default command line options
var (
port = flag.Int("port", 8000, "Port number for server")
staticDir = flag.String("static-dir", "www/static", "Static directory")
templateDir = flag.String("template-dir", "www/templates", "Templates directory")
enableCsrf = flag.Bool("enable-csrf", false, "Enable cross site request forgery")
debug = flag.Bool("debug", false, "Enable debugging")
)
func init() {
flag.Parse() // read command line flags
ServerConfig.Port = *port
ServerConfig.Host = ":" + strconv.Itoa(ServerConfig.Port)
ServerConfig.StaticDir = *staticDir
ServerConfig.TemplateDir = *templateDir
ServerConfig.Routes = []*Route{}
ServerConfig.Csrf = *enableCsrf
ServerConfig.ColoredLog = true
ServerConfig.Debug = *debug
}