-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
123 lines (107 loc) · 2.44 KB
/
main.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
// Copyright (c) 2015-2018, Christopher Hall
// see: LICENSE
package main
import (
"fmt"
"html/template"
"log"
"net/http"
"regexp"
"strings"
"time"
"unicode"
)
func main() {
server := &http.Server{
Addr: "127.0.0.1:8016",
Handler: pageHandler(),
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
log.Fatal(server.ListenAndServe())
}
type aHandler struct{}
func pageHandler() http.Handler {
return &aHandler{}
}
const htmlPage = `<!DOCTYPE html>
<html>
<head><meta charset="UTF-8">
<title>{{ .Title }}</title>
<link rel="shortcut icon" href="/icon/{{ .Icon }}.png" />
<script type="text/javascript">
function new_tab(url) {
window.open(url, '_blank');
return false;
}
function do_link() {
var url = document.getElementById('the-url').value;
if ('' === url) {
return false;
}
if ('.' == url[0]) {
new_tab(url.substring(1));
} else if (/^https?:/.test(url)) {
new_tab(url);
} else {
new_tab('http://' + url);
}
return false;
}
</script>
</head>
<body>
<h1>{{ .Title }}</h1>
<form onsubmit="return do_link();">
<input name="url" id="the-url" size="60">
<br />
<br />
<a href="/New%20Tab" onclick="return do_link();" target="_blank">New Tab</a>
</form>
</body>
</html>
`
type data struct {
Title string
Icon string
}
var tp = template.Must(template.New("page").Parse(htmlPage))
const defaultIcon = "tick"
var iconRegexp = regexp.MustCompilePOSIX("^/icon/(.+)[.]png$")
func (f aHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
//fmt.Printf("req: %v\n", r)
//fmt.Printf("path: %q\n", r.URL.Path)
if "/favicon.png" == r.URL.Path || "/favicon.ico" == r.URL.Path {
r.Header.Add("Content-Type", "image/png")
w.Write(icons[defaultIcon])
return
} else if strings.HasPrefix(r.URL.Path, "/icon/") {
r.Header.Add("Content-Type", "image/png")
m := iconRegexp.FindStringSubmatch(r.URL.Path)
if len(m) > 1 {
if icon, ok := icons[m[1]]; ok {
w.Write(icon)
return
}
}
w.Write(icons[defaultIcon])
return
}
str := r.URL.Path
if '/' == str[0] {
str = str[1:] // strip leading '/'
}
runes := []rune(str)
first := string(unicode.ToLower(runes[0]))
//fmt.Printf("first: %q\n", runes[0])
//fmt.Printf("first: %q\n", string(unicode.ToLower(runes[0])))
d := data{
Title: str,
Icon: first,
}
err := tp.Execute(w, d)
if nil != err {
fmt.Printf("error: %s\n", err)
}
}