forked from GoesToEleven/GolangTraining
/
main.go
77 lines (68 loc) · 1.82 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
package main
import (
"encoding/json"
"github.com/dustin/go-humanize"
"github.com/julienschmidt/httprouter"
"html/template"
"net/http"
)
var tpl *template.Template
func init() {
r := httprouter.New()
http.Handle("/", r)
r.GET("/", Home)
r.GET("/form/login", Login)
r.GET("/form/signup", Signup)
r.POST("/api/checkusername", checkUserName)
r.POST("/api/createuser", createUser)
r.POST("/api/login", loginProcess)
r.POST("/api/tweet", tweetProcess)
r.GET("/api/logout", logout)
http.Handle("/favicon.ico", http.NotFoundHandler())
http.Handle("/public/", http.StripPrefix("/public", http.FileServer(http.Dir("public/"))))
tpl = template.New("roottemplate")
tpl = tpl.Funcs(template.FuncMap{
"humanize_time": humanize.Time,
})
tpl = template.Must(tpl.ParseGlob("templates/html/*.html"))
}
func Home(res http.ResponseWriter, req *http.Request, _ httprouter.Params) {
//get tweets
tweets, err := getTweets(req, nil)
if err != nil {
// lala
}
memItem, err := getSession(req)
var sd SessionData
if err == nil {
// logged in
json.Unmarshal(memItem.Value, &sd)
sd.LoggedIn = true
}
sd.Tweets = tweets
tpl.ExecuteTemplate(res, "home.html", &sd)
}
func Login(res http.ResponseWriter, req *http.Request, _ httprouter.Params) {
serveTemplate(res, req, "login.html")
}
func Signup(res http.ResponseWriter, req *http.Request, _ httprouter.Params) {
serveTemplate(res, req, "signup.html")
}
/*
TO DO:
session
-memcache templates
- uuid in a cookie
--- https while logged in? - depends upon security required
- encrypt password on datastore?
--- never store an unencrypted password, so, resoundingly, YES
--- sha-256 fast hash value
- user memcache?
- datastore / memcache
session interface change
- change login button to logout when user logged in
post tweets
follow people
see tweets for everyone
see tweets for individual user
*/