-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlazyweb.go
206 lines (193 loc) · 5.12 KB
/
lazyweb.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package main
import (
"flag"
"fmt"
"html/template"
"log"
"net/http"
"os"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
)
var Menus []string
func init() {
Menus = []string{"CoffeeCat", "CatNDog", "ShortFilms", "About"}
}
func www_root(w http.ResponseWriter, r *http.Request) {
www(w, r, "template/index.html", "Index")
}
func www_shortfilms(w http.ResponseWriter, r *http.Request) {
www(w, r, "template/shortfilms.html", "ShortFilms")
}
func www_about(w http.ResponseWriter, r *http.Request) {
www(w, r, "template/about.html", "About")
}
// www_coffeecat shows coffeecat toon pages.
func www_coffeecat(w http.ResponseWriter, r *http.Request) {
page := r.URL.Path[len("/coffeecat/"):]
if page == "" {
www_toon_root(w, r, "CoffeeCat")
return
}
// page should convertable to int
i, err := strconv.Atoi(page)
if err != nil {
log.Print(err)
http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
return
}
www_toon_page(w, r, "CoffeeCat", i)
}
// www_catndog shows catndog toon pages.
func www_catndog(w http.ResponseWriter, r *http.Request) {
page := r.URL.Path[len("/catndog/"):]
if page == "" {
www_toon_root(w, r, "CatNDog")
return
}
// page should convertable to int
i, err := strconv.Atoi(page)
if err != nil {
log.Print(err)
http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
return
}
www_toon_page(w, r, "CatNDog", i)
}
// www_toon_root redirects to the last page of the toon.
func www_toon_root(w http.ResponseWriter, r *http.Request, title string) {
ltitle := strings.ToLower(title)
f, err := os.Open("toon/" + ltitle)
if err != nil {
log.Print(err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
images, err := f.Readdirnames(-1)
if err != nil {
log.Print(err)
return
}
sort.Sort(sort.Reverse(sort.StringSlice(images)))
last := -1
re := regexp.MustCompile("([0-9]+)[.]png$")
for i := 0; i < len(images); i++ {
m := re.FindStringSubmatch(images[i])
if len(m) == 2 {
last, _ = strconv.Atoi(m[1])
break
}
}
if last == -1 {
log.Print("no images matches to regexp.")
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
http.Redirect(w, r, fmt.Sprintf("/"+ltitle+"/%d", last), http.StatusFound)
}
// www_toon_page shows the page of the toon.
func www_toon_page(w http.ResponseWriter, r *http.Request, title string, i int) {
ltitle := strings.ToLower(title)
w.Header().Set("Content-Type", "text/html")
img := fmt.Sprintf("toon/"+ltitle+"/%04d.png", i)
_, err := os.Stat(img)
if err != nil {
if os.IsNotExist(err) {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
} else {
log.Print(err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
}
prev := fmt.Sprintf(ltitle+"/%d", i-1)
_, err = os.Stat(fmt.Sprintf("toon/"+ltitle+"/%04d.png", i-1))
if err != nil {
if os.IsNotExist(err) {
prev = ""
} else {
log.Print(err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
}
next := fmt.Sprintf(ltitle+"/%d", i+1)
_, err = os.Stat(fmt.Sprintf("toon/"+ltitle+"/%04d.png", i+1))
if err != nil {
if os.IsNotExist(err) {
next = ""
} else {
log.Print(err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
}
fmap := template.FuncMap{
"lower": strings.ToLower,
}
t, err := template.New("toon_page.html").Funcs(fmap).ParseFiles("template/toon_page.html", "template/head.html", "template/menu.html", "template/footer.html")
if err != nil {
log.Fatal(err)
}
info := struct {
Menus []string
MenuOn string
Image string
Prev string
Next string
}{
Menus: Menus,
MenuOn: title,
Image: img,
Prev: prev,
Next: next,
}
err = t.Execute(w, info)
if err != nil {
log.Fatal(err)
}
}
func www(w http.ResponseWriter, r *http.Request, page, menuon string) {
w.Header().Set("Content-Type", "text/html")
fmap := template.FuncMap{
"lower": strings.ToLower,
}
t, err := template.New(filepath.Base(page)).Funcs(fmap).ParseFiles(page, "template/head.html", "template/menu.html", "template/footer.html")
if err != nil {
log.Fatal(err)
return
}
info := struct {
Menus []string
MenuOn string
}{
Menus: Menus,
MenuOn: menuon,
}
err = t.Execute(w, info)
if err != nil {
log.Fatal(err)
}
}
func main() {
portPtr := flag.String("http", "", "service port ex):80")
flag.Parse()
if *portPtr == "" {
fmt.Println("lazyweb service")
flag.PrintDefaults()
os.Exit(1)
}
http.Handle("/template/", http.StripPrefix("/template/", http.FileServer(http.Dir("template"))))
http.Handle("/images/", http.StripPrefix("/images/", http.FileServer(http.Dir("images"))))
http.Handle("/toon/", http.StripPrefix("/toon/", http.FileServer(http.Dir("toon"))))
http.HandleFunc("/", www_root)
http.HandleFunc("/shortfilms", www_shortfilms)
http.HandleFunc("/coffeecat/", www_coffeecat)
http.HandleFunc("/catndog/", www_catndog)
http.HandleFunc("/about", www_about)
log.Fatal(http.ListenAndServe(*portPtr, nil))
}