-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.go
88 lines (75 loc) · 2.08 KB
/
web.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
package main
import (
"encoding/json"
"fmt"
"html/template"
"io/ioutil"
"net/http"
"strconv"
)
func webIndex(w http.ResponseWriter, r *http.Request) {
fmt.Println("method:", r.Method) //get request method
if r.Method == "GET" {
http.Redirect(w, r, "/", http.StatusSeeOther)
} else {
indexRestaurants()
http.Redirect(w, r, "/", http.StatusSeeOther)
}
}
func generateWebsite(w http.ResponseWriter, r *http.Request) {
html, _ := ioutil.ReadFile("./web/template.html")
cont, _ := ioutil.ReadFile("./web/content.html")
names := redisGetKeys("*")
var HtmlRest = make(map[string]interface{})
var HtmlSubs = make(map[string]interface{})
//var HtmlTimes = make(map[string]interface{})
for num, rest := range names {
HtmlRest[names[num]] = rest
if redisListLength(names[num]) >= 2 {
HtmlSubs[redisGetList(rest, 0, 1)[0]] = redisGetList(rest, 1, redisListLength(names[num]))
}
//HtmlTimes[]
}
dropdownTemplate, err := template.New("dropdownexample").Parse(string(html))
if err != nil {
panic(err)
}
dropdownTemplate.Execute(w, HtmlRest)
content, err := template.New("content").Parse(string(cont))
if err != nil {
panic(err)
}
content.Execute(w, HtmlSubs)
}
func webSubscribe(w http.ResponseWriter, r *http.Request) {
fmt.Println("method:", r.Method) //get request method
if r.Method == "GET" {
http.Redirect(w, r, "/", http.StatusSeeOther)
} else {
r.ParseForm()
group := Subscribe{
Time: r.Form["time"][0],
Users: []string{r.Form["username"][0]},
}
json.Marshal(group)
b, err := json.Marshal(group)
if err != nil {
fmt.Println("error:", err)
}
subscribe(r.Form["restaurant"][0], []byte(b))
http.Redirect(w, r, "/", http.StatusSeeOther)
}
}
func webAppend(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
http.Redirect(w, r, "/", http.StatusSeeOther)
} else {
r.ParseForm()
data := Restaurant{}
user := r.Form["username"][0]
index, _ := strconv.Atoi(r.Form["index"][0])
json.Unmarshal([]byte(r.Form["restaurant"][0]), &data)
redisAppend(data.Name, index+1, user)
http.Redirect(w, r, "/", http.StatusSeeOther)
}
}