-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
123 lines (99 loc) · 2.6 KB
/
server.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
package main
import (
"bytes"
"log"
"net/http"
"os"
"path"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/leonelquinteros/thtml/templates"
)
// Handler
type thtmlHandler struct{}
func (h thtmlHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Catch panics
defer func() {
if err := recover(); err != nil {
// Get stack trace
stack := make([]byte, 1<<16)
runtime.Stack(stack, false)
// Log panic
log.Printf("PANIC! :: %v", err)
log.Print(string(stack))
}
// Log
log.Printf("%s %s", r.Method, r.URL.String())
}()
// Construct path
p := h.cleanPath(_publicPath + r.URL.EscapedPath())
// Check if file exists and if it's a file
if info, err := os.Stat(p); err == nil && !info.IsDir() {
// Load comma separated extensions list
exts := strings.Split(_exts, ",")
// Load templates
tpl, err := templates.Load(_templatesPath, exts...)
if err != nil {
log.Fatalf("Error loading templates from '%s': %s", _templatesPath, err)
}
// Configure
tpl.Minify(_minify)
// Render to buffer
buff := new(bytes.Buffer)
err = tpl.Render(buff, p, nil)
if err != nil {
buff.Write([]byte(err.Error()))
}
content := buff.Bytes()
// Detect content type
ext := filepath.Ext(p)
switch ext {
case ".html":
w.Header().Set("Content-Type", "text/html; charset=utf-8")
case ".js":
w.Header().Set("Content-Type", "application/javascript; charset=utf-8")
case ".css":
w.Header().Set("Content-Type", "text/css; charset=utf-8")
case ".svg":
w.Header().Set("Content-Type", "image/svg+xml; charset=utf-8")
default:
w.Header().Set("Content-Type", http.DetectContentType(content))
}
// Flush
w.Write(content)
} else {
w.WriteHeader(404)
}
}
// cleanPath normalizes requeste filenames
func (h thtmlHandler) cleanPath(p string) string {
p = path.Clean(p)
// Catch routes without ".html" and dir names without /index.html
if info, err := os.Stat(p); err != nil || info.IsDir() {
if info, err := os.Stat(p + ".html"); err == nil && !info.IsDir() {
p += ".html"
} else if info, err := os.Stat(p + "index.html"); err == nil && !info.IsDir() {
p += "index.html"
} else if info, err := os.Stat(p + "/index.html"); err == nil && !info.IsDir() {
p += "/index.html"
}
}
return p
}
func runServer() {
// Routes
http.Handle("/", thtmlHandler{})
// Server
s := &http.Server{
Addr: _httpListen,
Handler: nil,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
log.Printf("Starting dev web server on '%s'", _httpListen)
// Lock
log.Print(s.ListenAndServe())
}