Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 48 lines (40 sloc) 1.223 kb
44635af @tobi move error rendering into it's own file
authored
1 package main
2
3 import (
4 "template"
5 "log"
6 "io"
1d88d4f @tobi Better error reporting
authored
7 "regexp"
44635af @tobi move error rendering into it's own file
authored
8 )
9
10 var tmpl *template.Template
1d88d4f @tobi Better error reporting
authored
11 var ex *regexp.Regexp
12
13 func n2br(text string) string {
14 log.Print("Got text: " + text)
15 return ex.ReplaceAllString(text, "<br>")
16 }
17
18 var fmap = template.FuncMap{
19 "n2br": n2br,
20 }
44635af @tobi move error rendering into it's own file
authored
21
22 func init() {
1d88d4f @tobi Better error reporting
authored
23 layout := template.New("error template")
24 layout.Funcs(fmap)
25 tmpl = template.Must(layout.Parse(`
26 <!DOCTYPE html>
27 <html>
28 <head><title>Error: {{.title | html}}</title></head>
29 <body style='font-style:sans-serif;font-size:10pt;background:#eee'>
30 <div style='border:1px solid #999;background:white;margin: 50px auto;padding:1em 3em;width:600px'>
31 <h2>{{.title | html | n2br}}</h2>
32 <pre style='background:#222;color:#eee;padding:8px 5px;border:1px solid #666'>{{.error | html | n2br}}</pre>
33 </div>
34 </body>
35 </html>`))
36
37 ex = regexp.MustCompile("\n")
44635af @tobi move error rendering into it's own file
authored
38 }
39
1d88d4f @tobi Better error reporting
authored
40 func error(client io.WriteCloser, title, error string) {
41 log.Printf("---\nError in %s: \n\n%s\n\n", title, error)
44635af @tobi move error rendering into it's own file
authored
42 client.Write([]byte("HTTP/1.0 500 Internal Error\n"))
1d88d4f @tobi Better error reporting
authored
43 client.Write([]byte("Content-Type: text/html\n\n"))
44 tmpl.Execute(client, map[string]string{"error": error, "title": title})
45 client.Write([]byte("\n"))
44635af @tobi move error rendering into it's own file
authored
46 client.Close()
47 }
Something went wrong with that request. Please try again.