-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
60 lines (47 loc) · 1.17 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
package main
import (
view "github.com/KeizoBookman/library/view"
"net/http"
"net/http/cgi"
"strings"
)
func main() {
err := cgi.Serve(http.HandlerFunc(handler))
if err != nil {
view.ViewFail("request")
}
}
func handler(w http.ResponseWriter, r *http.Request) {
r.Header.Set("Content-Type:", "text/html; charset=UTF-8")
handle, err := routing(r.URL.Path)
if err != nil {
view.ViewFail(err.Error())
}
handle(w, r)
}
type failRouting string
func (f failRouting) Error() string {
return string(f)
}
func routing(path string) (func(w http.ResponseWriter, r *http.Request), error) {
top := "index.cgi"
n := strings.Index(path, top)
if n == -1 {
return nil, failRouting("routing can not find root " + top)
}
size := n + len(top)
switch {
case strings.HasPrefix(path[size:], "/public"):
return nil, nil
case strings.HasPrefix(path[size:], "/new"):
return view.NewSeat, nil
case strings.HasPrefix(path[size:], "/list"):
return view.List, nil
case strings.HasPrefix(path[size:], "/search"):
return view.Search, nil
case strings.HasPrefix(path[size:], "/"), strings.HasPrefix(path[size:], ""):
return view.Index, nil
default:
return http.NotFound, nil
}
}