-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path06.go
56 lines (45 loc) · 1.11 KB
/
06.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
package main
import (
"fmt"
"net/http"
"os"
"strings"
"text/template"
)
const LAUNCH_FAILED = 1
const FILE_READ = 2
const BAD_TEMPLATE = 3
const ADDRESS = ":3000"
type Commands map[string]func(http.ResponseWriter, *http.Request)
type PageConfiguration struct{ Commands }
func main() {
p := PageConfiguration{
Commands{
"A": AJAX_handler("A"), "B": AJAX_handler("B"), "C": AJAX_handler("C")}}
html, e := template.ParseFiles(BaseName() + ".html")
Abort(FILE_READ, e)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
Abort(BAD_TEMPLATE, html.Execute(w, p))
})
for c, f := range p.Commands {
http.HandleFunc("/"+c, f)
}
Abort(LAUNCH_FAILED, http.ListenAndServe(ADDRESS, nil))
}
func Abort(n int, e error) {
if e != nil {
fmt.Println(e)
os.Exit(n)
}
}
func BaseName() string {
s := strings.Split(os.Args[0], "/")
return s[len(s)-1]
}
func AJAX_handler(c string) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
fmt.Fprint(w, c)
}
}