-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
170 lines (144 loc) · 4.14 KB
/
server.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
package main
import (
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"html/template"
)
func index(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
tmpl := template.Must(template.ParseFiles("site/index.html", "site/partials/header.html", "site/partials/footer.html"))
tmpl.ExecuteTemplate(w, "index", "")
}
}
func enter(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
tmpl := template.Must(template.ParseFiles("site/enter.html", "site/partials/header.html", "site/partials/footer.html"))
tmpl.ExecuteTemplate(w, "enter", "")
} else if r.Method == "POST" {
// Creates new id and folder with that id
id := 1
made := false
for made == false {
if _, err := os.Stat("site/assets/submissions/" + strconv.Itoa(id) + "/"); os.IsNotExist(err) && id <= 100 {
err := os.Mkdir("site/assets/submissions/"+strconv.Itoa(id)+"/", 0755)
if err != nil {
panic(err)
}
made = true
} else if id <= 100 {
id++
} else {
return
}
}
// Parse Form into Variables
r.ParseMultipartForm(32 << 20)
first_name := r.Form["first_name"][0]
last_name := r.Form["last_name"][0]
email := r.Form["email"][0]
phone := r.Form["phone"][0]
file, _, err := r.FormFile("photo")
if err != nil {
panic(err)
return
}
defer file.Close()
// Write info to info.txt file
info, err := os.Create("site/assets/submissions/" + strconv.Itoa(id) + "/info.txt")
if err != nil {
panic(err)
return
}
defer info.Close()
fmt.Fprintf(info, "%s\r\n", first_name)
fmt.Fprintf(info, "%s\r\n", last_name)
fmt.Fprintf(info, "%s\r\n", email)
fmt.Fprintf(info, "%s\r\n", phone)
// Create votes.txt file
votes, err := os.Create("site/assets/submissions/" + strconv.Itoa(id) + "/votes.txt")
if err != nil {
panic(err)
return
}
defer votes.Close()
fmt.Fprintf(votes, "0")
// Save photo file
out, err := os.Create("site/assets/submissions/" + strconv.Itoa(id) + "/picture.png")
if err != nil {
panic(err)
return
}
defer out.Close()
io.Copy(out, file)
// Return to Success page
http.Redirect(w, r, "/success", http.StatusFound)
fmt.Println("Image submitted")
}
}
func vote(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
files, _ := ioutil.ReadDir("site/assets/submissions")
num := len(files)
nums := make([]int, 0)
for i := 1; i <= num; i++ {
nums = append(nums, i)
}
tmpl := template.Must(template.ParseFiles("site/vote.html", "site/partials/header.html", "site/partials/footer.html"))
tmpl.ExecuteTemplate(w, "vote", nums)
} else if r.Method == "POST" {
var id = r.URL.Query().Get("id")
votes, err := os.Open("site/assets/submissions/" + id + "/votes.txt");
if err != nil {
panic(err)
return
}
current_votes, _ := ioutil.ReadAll(votes)
string_votes := string(current_votes)
int_votes, _ := strconv.Atoi(string_votes)
int_votes += 1
new_votes := strconv.Itoa(int_votes)
votes.Close()
err = os.Remove("site/assets/submissions/" + id + "/votes.txt")
if err != nil {
log.Fatal(err)
}
votes, err = os.Create("site/assets/submissions/" + id + "/votes.txt")
if err != nil {
panic(err)
return
}
defer votes.Close()
fmt.Fprintf(votes, new_votes)
}
}
func contact(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
tmpl := template.Must(template.ParseFiles("site/contact.html", "site/partials/header.html", "site/partials/footer.html"))
tmpl.ExecuteTemplate(w, "contact", "")
}
}
func success(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
tmpl := template.Must(template.ParseFiles("site/success.html", "site/partials/header.html", "site/partials/footer.html"))
tmpl.ExecuteTemplate(w, "success", "")
}
}
func main() {
http.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir("site/assets/"))))
http.HandleFunc("/enter", enter)
http.HandleFunc("/vote", vote)
http.HandleFunc("/contact", contact)
http.HandleFunc("/success", success)
http.HandleFunc("/", index)
err := http.ListenAndServe(":80", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
} else {
fmt.Println("Photo Contest Server Running...")
}
}