/
ergo.go
145 lines (127 loc) · 3.53 KB
/
ergo.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
package main
import (
"bytes";
"flag";
"http";
"io";
"log";
"strings";
"template";
pathutil "path";
"exec";
"os";
)
var addr = flag.String("addr", ":1718", "http service address") // Q=17, R=18
var tmplroot *string
var fmap = template.FormatterMap{
"html": template.HTMLFormatter,
"url+html": UrlHtmlFormatter,
}
func readTemplate(name string) *template.Template {
path := pathutil.Join(*tmplroot, name);
data, err := io.ReadFile(path);
if err != nil {
log.Exitf("ReadFile %s: %v", path, err)
}
t, err := template.Parse(string(data), fmap);
if err != nil {
log.Exitf("%s: %v", name, err)
}
return t;
}
var (
appHTML,
addHTML *template.Template;
)
func readTemplates() {
// have to delay until after flags processing,
// so that main has chdir'ed to goroot.
appHTML = readTemplate("application.html");
addHTML = readTemplate("add.html");
}
func servePage(c *http.Conn, title, query string, content string) {
type Data struct {
Title string;
//Timestamp uint64; // int64 to be compatible with os.Dir.Mtime_ns
Query string;
Content string;
}
//_, ts := fsTree.get();
d := Data{
Title: title,
//Timestamp: uint64(ts) * 1e9, // timestamp in ns
Query: query,
Content: content,
};
if err := appHTML.Execute(&d, c); err != nil {
log.Stderrf("appHTML.Execute: %s", err)
}
}
func main() {
// Determine execuable dir
execpath, err := exec.LookPath(os.Args[0]);
if err != nil {
log.Exitf("Unable to determine executable path")
}
// Absolutise execpath: Seems the best way in absence of Realpath
if pwd, err := os.Getwd(); err == nil {
execpath = pathutil.Clean(pathutil.Join(pwd, execpath))
} else {
log.Exitf("Getwd: %s", err)
}
execdir, _ := pathutil.Split(execpath);
tmplroot = flag.String("root", pathutil.Join(execdir, "templates"), "root directory for templates");
flag.Parse();
log.Stdoutf("Using template dir: %s", *tmplroot);
readTemplates();
http.Handle("/", http.HandlerFunc(func(c *http.Conn, req *http.Request) {}));
http.Handle("/add", http.HandlerFunc(func(c *http.Conn, req *http.Request) {
// Process the add template
var buf bytes.Buffer;
/* if err := parseerrorHTML.Execute(errors, &buf); err != nil {*/
err := addHTML.Execute("x", &buf);
if err != nil {
log.Stderrf("addHTML.Execute: %s", err)
}
//templ.Execute(req.FormValue("s"), c);
servePage(c, "Add", "", string(buf.Bytes()));
}));
http.Handle("/css/", http.FileServer("public/css", "/css/"));
http.Handle("/js/", http.FileServer("public/js", "/js/"));
err = http.ListenAndServe(*addr, nil);
if err != nil {
log.Exit("ListenAndServe:", err)
}
}
func QR(c *http.Conn, req *http.Request, templ *template.Template) {
templ.Execute(req.FormValue("s"), c)
}
func UrlHtmlFormatter(w io.Writer, v interface{}, fmt string) {
template.HTMLEscape(w, strings.Bytes(http.URLEscape(v.(string))))
}
const templateStr = `
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=480" />
<title>QR Link Generator</title>
<link rel="stylesheet" href="/css/screen.css" type="text/css" media="screen, projection" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script src="/js/script.js"></script>
</head>
<body>
{.section @}
<img src="http://chart.apis.google.com/chart?chs=300x300&cht=qr&choe=UTF-8&chl={@|url+html}"
/>
<br>
{@|html}
<br>
<br>
{.end}
<form action="/" name=f method="GET"><input maxLength=1024 size=70
name=s value="" title="Text to QR Encode"><input type=submit
value="Show QR" name=qr>
</form>
</body>
</html>
`