-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
181 lines (141 loc) · 4.84 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
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
package main
import (
model "Trophy-System/model"
"encoding/json"
"html/template"
"log"
"net/http"
"strconv"
"time"
)
type pageVariables struct {
Date string
Time string
}
type Cookie struct {
Name string
Value string
Path string
Domain string
Expires time.Time
RawExpires string
// MaxAge=0 means no 'Max-Age' attribute specified.
// MaxAge<0 means delete cookie now, equivalently 'Max-Age: 0'
// MaxAge>0 means Max-Age attribute present and given in seconds
MaxAge int
Secure bool
HTTPOnly bool
Raw string
Unparsed []string // Raw text of unparsed attribute-value pairs
}
/*
This is the start of the web server with the REST API
*/
func main() {
// http.HandleFunc() takes two inputs, the first being a pattern which is a string
// and the second is a handler (a function that needs a ResponseWriter and a pointer to a Request).
// Handle static files such as CSS and JS for the webpages
fs := http.FileServer(http.Dir("view/static/"))
http.Handle("/static/", http.StripPrefix("/static/", fs))
http.HandleFunc("/", homePage)
http.HandleFunc("/login", loginOrcreate)
http.HandleFunc("/createuser", createUser)
http.HandleFunc("/userlogin", login)
http.HandleFunc("/about", about)
http.ListenAndServe(":8080", nil)
}
func homePage(w http.ResponseWriter, r *http.Request) {
_, err := r.Cookie("email")
if err != nil {
log.Print("User not logged in!")
//http.Redirect(w, r, "/login", 301)
}
now := time.Now() // find the time right now
homePageVars := pageVariables{ //store the date,time and username in a struct
Date: now.Format("02-01-2006"),
Time: now.Format("15:04:05"),
}
t, err := template.ParseFiles("view/mainpage.html") //parse the html file homepage.html
if err != nil { // if there is an error
log.Print("template parsing error: ", err) // log it
}
err = t.Execute(w, homePageVars) //execute the template and pass it the homePageVars struct to fill in the gaps
if err != nil { // if there is an error
log.Print("template executing error: ", err) //log it
}
/*
expiration := time.Now().Add(365 * 24 * time.Hour)
cookie := http.Cookie{Name: "email", Value: "", Expires: expiration}
http.SetCookie(w, &cookie)
cookie, _ = r.Cookie("email")
//Check if username exists
if(geusernamefromdb == cookie.Value){
//show Username webpage
}
w.WriteHeader(http.StatusOK)
*/
}
func login(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
email := r.Form.Get("email")
password := r.Form.Get("password")
match, usernameDB := model.Login(email, password)
log.Print(match)
log.Print(usernameDB)
expiration := time.Now().Add(365 * 24 * time.Hour)
cookie := http.Cookie{Name: "email", Value: email, Expires: expiration}
http.SetCookie(w, &cookie)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
mapD := map[string]bool{"status": match}
mapB, _ := json.Marshal(mapD)
//jsonData := []byte(`{"status":match}`)
w.Write(mapB)
}
func createUser(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
username := r.Form.Get("username")
password := r.Form.Get("password")
email := r.Form.Get("email")
ok, message := model.CreateAccount(username, password, email)
log.Print("Answer:", message, "=", ok) //log it
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
s := strconv.FormatBool(ok)
mapD := map[string]string{"status": s,
"message": message}
mapB, _ := json.Marshal(mapD)
w.Write(mapB)
}
func loginOrcreate(w http.ResponseWriter, r *http.Request) {
_, err := r.Cookie("email") //cookieValue was the 1st variable
if err == nil {
log.Print("User already logged in!")
http.Redirect(w, r, "/", 301)
}
//fmt.Fprint(w, cookieValue)
t, err := template.ParseFiles("view/login_create.html") //parse the html file homepage.html
if err != nil { // if there is an error
log.Print("template parsing error: ", err) // log it
}
err = t.Execute(w, 0) //execute the template and pass it the homePageVars struct to fill in the gaps
if err != nil { // if there is an error
log.Print("template executing error: ", err) //log it
}
log.Print("I am here! Login or create")
}
func about(w http.ResponseWriter, r *http.Request) {
_, err := r.Cookie("email")
if err != nil {
log.Print("User not logged in!")
//http.Redirect(w, r, "/login", 301)
}
t, err := template.ParseFiles("view/about.html") //parse the html file homepage.html
if err != nil { // if there is an error
log.Print("template parsing error: ", err) // log it
}
err = t.Execute(w, 0) //execute the template and pass it the homePageVars struct to fill in the gaps
if err != nil { // if there is an error
log.Print("template executing error: ", err) //log it
}
}