forked from GoesToEleven/GolangTraining
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
116 lines (106 loc) · 3.03 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
package main
import (
"bytes"
"fmt"
"github.com/julienschmidt/httprouter"
"google.golang.org/appengine"
"google.golang.org/appengine/datastore"
"google.golang.org/appengine/log"
"google.golang.org/appengine/memcache"
"html/template"
"io"
"io/ioutil"
"net/http"
)
type User struct {
Email string
UserName string `datastore:"-"`
Password string
}
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)
http.Handle("/favicon.ico", http.NotFoundHandler()) // maybe not needed b/c of schmidt router
http.Handle("/public/", http.StripPrefix("/public", http.FileServer(http.Dir("public/"))))
tpl = template.Must(template.ParseGlob("templates/html/*.html"))
}
func Home(res http.ResponseWriter, req *http.Request, _ httprouter.Params) {
ctx := appengine.NewContext(req)
i, err := memcache.Get(ctx, "Homepage")
if err != nil {
buf := new(bytes.Buffer)
writ := io.MultiWriter(res, buf)
tpl.ExecuteTemplate(writ, "home.html", nil)
memcache.Set(ctx, &memcache.Item{
Value: buf.Bytes(),
Key: "Homepage",
})
return
}
io.WriteString(res, string(i.Value))
}
func Login(res http.ResponseWriter, req *http.Request, _ httprouter.Params) {
tpl.ExecuteTemplate(res, "login.html", nil)
}
func Signup(res http.ResponseWriter, req *http.Request, _ httprouter.Params) {
tpl.ExecuteTemplate(res, "signup.html", nil)
}
func checkUserName(res http.ResponseWriter, req *http.Request, _ httprouter.Params) {
ctx := appengine.NewContext(req)
bs, err := ioutil.ReadAll(req.Body)
sbs := string(bs)
log.Infof(ctx, "REQUEST BODY: %v", sbs)
var user User
key := datastore.NewKey(ctx, "Users", sbs, 0, nil)
err = datastore.Get(ctx, key, &user)
// if there is an err, there is NO user
log.Infof(ctx, "ERR: %v", err)
if err != nil {
// there is an err, there is a NO user
fmt.Fprint(res, "false")
return
} else {
fmt.Fprint(res, "true")
}
}
func createUser(res http.ResponseWriter, req *http.Request, _ httprouter.Params) {
ctx := appengine.NewContext(req)
NewUser := User{
Email: req.FormValue("email"),
UserName: req.FormValue("userName"),
Password: req.FormValue("password"),
}
key := datastore.NewKey(ctx, "Users", NewUser.UserName, 0, nil)
key, err := datastore.Put(ctx, key, &NewUser)
// this is the only error checking I added; any others on this page needed?
if err != nil {
log.Errorf(ctx, "error adding todo: %v", err)
http.Error(res, err.Error(), 500)
return
}
http.Redirect(res, req, "/", 302)
}
/*
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
*/