-
Notifications
You must be signed in to change notification settings - Fork 0
/
wahanthok.go
84 lines (75 loc) · 1.99 KB
/
wahanthok.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
package wahanthok
import (
"log"
"errors"
"strings"
"net/http"
"html/template"
)
var (
db Was
p *SearchPage
templates *template.Template
)
func renderTemplate(w http.ResponseWriter, tmpl string) {
if err := templates.ExecuteTemplate(w, tmpl+".html", p); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func index(w http.ResponseWriter, r *http.Request) {
renderTemplate(w, "search")
// Reset details for the next request
p = &SearchPage{}
p.BackgroundImage = "images/default.jpg"
}
func search(w http.ResponseWriter, r *http.Request) {
word := r.FormValue("word")
if word == "" {
http.Redirect(w, r, "/", http.StatusFound)
return
}
log.Println("Search for word:", word)
if v, ok := db[strings.ToLower(word)]; ok { // The key is all in lower case
p.W = v
p.W.Word = word // Display the word in the same cases as user entered
p.Msg = ""
log.Println("Found meaning:", v)
} else {
p.W = Wa{word, nil}
p.Msg = "Atoppa wahei ama thibiyo! Please search another word."
log.Println("Not found")
}
// Redirect to /
http.Redirect(w, r, "/", http.StatusFound)
}
func init() {
var err error
var d *Was
// Load the templates
templates = template.Must(template.ParseFiles("html/search.html"))
if templates == nil {
log.Println("Unable to parse templates")
panic(errors.New("Exiting application"))
}
// Load the db
if d, err = LoadDB(); err != nil {
log.Println("Unable to load db", err)
panic(errors.New("Exiting application"))
}
db = *d
// Set up default page data
p = &SearchPage{}
p.BackgroundImage = "images/default.jpg"
// Routing
http.HandleFunc("/", index)
http.HandleFunc("/search", search)
// Static files server
http.HandleFunc("/images/", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, r.URL.Path[1:])
})
http.HandleFunc("/css/", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, r.URL.Path[1:])
})
//log.Println("Running wahei at port 9000")
//http.ListenAndServe(":9000", nil)
}