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
|
package main
import (
"html/template"
"log"
"net/http"
"strings"
)
type TemplateHandler struct {
handler http.Handler
}
func (t *TemplateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Server", "uhttpd")
// We use our custom handler to prettify directories. Files are served via default.
if !strings.HasSuffix(r.URL.Path, "/") {
t.handler.ServeHTTP(w, r)
return
}
rec := NewRecorder()
defer rec.Body.Reset()
// passing the recorder instead of the real ResponseWriter
t.handler.ServeHTTP(rec, r)
// let the standard lib handle all the caching
if rec.Code > 300 && rec.Code < 400 {
log.Println("Code: ", rec.Code)
t.handler.ServeHTTP(w, r)
return
}
// we copy the original headers first
for k, v := range rec.Header() {
w.Header()[k] = v
}
// not found
if rec.Code == 404 {
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(404)
tmpl := template.New("404")
tmpl, err := tmpl.Parse(get404())
if err != nil {
log.Println(err.Error())
w.WriteHeader(500)
w.Write([]byte(err.Error()))
return
}
err = tmpl.Execute(w, struct{ URL string }{URL: r.URL.Path})
if err != nil {
log.Println(err.Error())
w.WriteHeader(500)
w.Write([]byte(err.Error()))
return
}
return
}
// we serve a file instead of a html page
if !strings.Contains(w.Header().Get("Content-Type"), "text/html") {
w.Write(rec.Body.Bytes())
return
}
data := rec.Body.String()
// we serve the directoy page
if strings.HasPrefix(data, "<pre>") {
execTemplate(w, r, data)
} else {
w.Write(rec.Body.Bytes())
}
}
func execTemplate(w http.ResponseWriter, r *http.Request, data string) {
data = strings.Replace(data, "<pre>", "", 1)
data = strings.Replace(data, "</pre>", "", 1)
data = strings.Replace(data, "</a>", "</a><br>", -1)
tmpl := template.New("page")
tmpl, err := tmpl.Parse(getTemplate())
if err != nil {
log.Println(err.Error())
w.WriteHeader(500)
w.Write([]byte(err.Error()))
return
}
err = tmpl.Execute(w, struct {
Data template.HTML
URL string
}{Data: template.HTML(data), URL: r.URL.Path})
if err != nil {
log.Println(err)
}
}
func NewTemplateHandler(handler http.Handler, allowedHost string) *TemplateHandler {
return &TemplateHandler{handler: handler}
}
func getTemplate() string {
return `<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>{{.URL}}</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>/*! normalize.css v3.0.2 | MIT License | git.io/normalize */html{font-family:sans-serif;-ms-text-size-adjust:100%;-webkit-text-size-adjust:100%}body{margin:0}a{background-color:transparent}a:active,a:hover{outline:0}button::-moz-focus-inner,input::-moz-focus-inner{border:0;padding:0}.container{position:relative;width:100%;max-width:960px;margin:0 auto;padding:0 20px;box-sizing:border-box}.column{width:100%;float:left;box-sizing:border-box}@media (min-width:400px){.container{width:85%;padding:0}}@media (min-width:550px){.container{width:80%}.column{margin-left:4%}.column:first-child{margin-left:0}}html{font-size:62.5%}body{font-size:1.5em;line-height:1.6;font-weight:400;font-family:Raleway,HelveticaNeue,"Helvetica Neue",Helvetica,Arial,sans-serif;color:#222}h4{margin-top:0;margin-bottom:2rem;font-weight:300;font-size:2.4rem;line-height:1.35;letter-spacing:-.08rem}@media (min-width:550px){h4{font-size:3rem}}a{color:#1EAEDB}a:hover{color:#0FA0CE}blockquote{margin-bottom:2.5rem}.container:after,.row:after{content:"";display:table;clear:both}.margin-top{margin-top:20px}.dir,.dir:hover{color:#000}.dir:hover{text-decoration:none}.dir:before{content:"> "}div.icon{height:32px;width:32px;position:relative;margin:15px;overflow:hidden;display:inline-block}div.icon div.home{height:0;width:0;border-width:16px;border-style:solid;border-color:transparent transparent #333;position:absolute;bottom:16px;left:0}div.icon div.home:after{content:'';width:5px;height:16px;background-color:transparent;position:absolute;top:16px;right:-11px;border-left:8px solid #333;border-right:8px solid #333}div.icon div.home:before{content:'';width:9px;height:6px;background-color:#333;position:absolute;top:16px;right:-5px}div.icon div.chimney{width:4px;height:10px;background:#333;position:absolute;right:6px;top:3px}</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="column margin-top">
<h4><a href="/.." title="Go back to root"><div class="icon"><div class="home"></div><div class="chimney"></div></div></a><br>
{{.URL}}</h4>
<blockquote>
{{if ne .URL "/"}}
<a class="dir" href="..">.. (up a dir)</a><br><br>
{{end}}
{{.Data}}
</blockquote>
</div>
</div>
</div>
<script>
String.prototype.endsWith = function(suffix) {
return this.indexOf(suffix, this.length - suffix.length) !== -1;
}
allAnchors = document.querySelectorAll("a");
for(i=0;i<allAnchors.length;i++){
if (allAnchors[i].getAttribute("href").endsWith("/")){
allAnchors[i].className = allAnchors[i].className + " dir";
}
allAnchors[i].className = allAnchors[i].className + " item";
}
</script>
</body>
</html>`
}
func get404() string {
return `<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Not Found - {{.URL}}</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>/*! normalize.css v3.0.2 | MIT License | git.io/normalize */html{font-family:sans-serif;-ms-text-size-adjust:100%;-webkit-text-size-adjust:100%}body{margin:0}a{background-color:transparent}a:active,a:hover{outline:0}button::-moz-focus-inner,input::-moz-focus-inner{border:0;padding:0}.container{position:relative;width:100%;max-width:960px;margin:0 auto;padding:0 20px;box-sizing:border-box}@media (min-width:400px){.container{width:85%;padding:0}}@media (min-width:550px){.container{width:80%}}html{font-size:62.5%}body{font-size:1.5em;line-height:1.6;font-weight:400;font-family:Raleway,HelveticaNeue,"Helvetica Neue",Helvetica,Arial,sans-serif;color:#222}h3{margin-top:0;margin-bottom:2rem;font-weight:300;font-size:3rem;line-height:1.3;letter-spacing:-.1rem}@media (min-width:550px){h3{font-size:3.6rem}}p{margin-top:0}a{color:#1EAEDB}a:hover{color:#0FA0CE}.button{display:inline-block;height:38px;padding:0 30px;color:#555;text-align:center;font-size:11px;font-weight:600;line-height:38px;letter-spacing:.1rem;text-transform:uppercase;text-decoration:none;white-space:nowrap;background-color:transparent;border-radius:4px;border:1px solid #bbb;cursor:pointer;box-sizing:border-box}.button:focus,.button:hover{color:#333;border-color:#888;outline:0}.button.button-primary{color:#FFF;background-color:#33C3F0;border-color:#33C3F0}.button.button-primary:focus,.button.button-primary:hover{color:#FFF;background-color:#1EAEDB;border-color:#1EAEDB}.button{margin-bottom:1rem}p{margin-bottom:2.5rem}.container:after{content:"";display:table;clear:both}.section{padding:8rem 0 7rem;text-align:center}.section-description,.section-heading{margin-bottom:1.2rem}</style>
</head>
<body>
<div class="section">
<div class="container">
<h3 class="section-heading">Not Found - {{.URL}}</h3>
<p class="section-description">
Please check for typos in your url. </p>
<p class="section-description">
<a href="/" class="button button-primary">Go back to root</a></p>
</div>
</div>
</body>
</html>`
}
|