-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.go
65 lines (53 loc) · 1.78 KB
/
index.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
package main
import (
"log"
"net/http"
"text/template"
"github.com/SrijanSriv/gorum/data"
)
type Info struct {
Threads []data.Thread
Username string
}
func index(w http.ResponseWriter, r *http.Request) {
/*hardcoding input and parsing a slice*/
// input := Info{"test", "Some Body", "srijan", time.Now().Format(time.ANSIC)}
// inputAgain := Info{"testtesttesttesttest", "Some Body", "srijansriv", time.Now().Format(time.ANSIC)}
// multiInput := []Info{input, inputAgain, input, inputAgain, input, inputAgain, input, inputAgain, input, inputAgain}
/*above is all the hardcoded input*/
threads := data.GetThreads()
var templates *template.Template
public_temp_files := []string{"templates/public.layout.html", "templates/index.html", "templates/public.navbar.html", "templates/aside.html"}
private_temp_files := []string{"templates/private.layout.html", "templates/index.html", "templates/private.navbar.html", "templates/aside.html"}
_, username, err := session(w, r)
stuff := Info{threads, username}
if err != nil {
templates = template.Must(template.ParseFiles(public_temp_files...))
} else {
templates = template.Must(template.ParseFiles(private_temp_files...))
}
templates.Execute(w, stuff)
}
func session(w http.ResponseWriter, r *http.Request) (sess data.Session, username string, err error) {
cookie, err := r.Cookie("_cookie")
if err == nil {
sess = data.Session{Uuid: cookie.Value}
if ok, _ := sess.CheckSession(); !ok {
log.Fatal()
}
user, err := data.UserByUuid(cookie.Value)
if err != nil {
log.Fatal(err)
}
username = user.Name
}
return
}
func logout(w http.ResponseWriter, r *http.Request) {
cookie, err := r.Cookie("_cookie")
if err == nil {
cookie.MaxAge = -1
http.SetCookie(w, cookie)
}
http.Redirect(w, r, "/", http.StatusFound)
}