-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
128 lines (115 loc) · 3.27 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
package main
import (
"html/template"
"log"
"net/http"
"github.com/go-redis/redis"
"github.com/gorilla/mux"
"github.com/gorilla/sessions"
"golang.org/x/crypto/bcrypt"
)
var templates *template.Template
var client *redis.Client
var store = sessions.NewCookieStore([]byte("t0p-se3ret"))
func main() {
r := mux.NewRouter()
templates = template.Must(template.ParseGlob("*.html"))
client = redis.NewClient(&redis.Options{
Addr: "localhost:6379",
})
r.HandleFunc("/", Authorization(GetComments)).Methods("get")
r.HandleFunc("/", Authorization(PostComments)).Methods("post")
r.HandleFunc("/login", LoginGetHandler).Methods("get")
r.HandleFunc("/login", LoginPostHandler).Methods("post")
r.HandleFunc("/register", RegisterGetHandler).Methods("get")
r.HandleFunc("/register", RegisterPostHandler).Methods("post")
r.HandleFunc("/test", TestLogin).Methods("Get")
// fs := http.FileServer(http.Dir("./static/"))
// r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", fs))
r.Handle("", r)
http.ListenAndServe(":8080", r)
}
//MIddleware part
func Authorization(handler http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
session, _ := store.Get(r, "session")
_, ok := session.Values["username"]
if !ok {
http.Redirect(w, r, "/login", 302)
return
}
handler.ServeHTTP(w, r)
}
}
func GetComments(w http.ResponseWriter, r *http.Request) {
comments, err := client.LRange("comments", 0, 10).Result()
var newComments []string
for _, comm := range comments {
if comm == "" {
continue
} else {
newComments = append(newComments, comm)
}
}
if err != nil {
return
}
templates.ExecuteTemplate(w, "index.html", newComments)
}
func PostComments(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
comment := r.PostForm.Get("comment")
client.LPush("comments", comment)
http.Redirect(w, r, "/", 302)
}
func LoginGetHandler(w http.ResponseWriter, r *http.Request) {
templates.ExecuteTemplate(w, "login.html", nil)
}
func LoginPostHandler(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
username := r.PostForm.Get("username")
password := r.PostForm.Get("password")
hash, err := client.Get("user:" + username).Bytes()
if err != nil {
log.Fatal(http.StatusInternalServerError)
return
}
err = bcrypt.CompareHashAndPassword(hash, []byte(password))
if err != nil {
w.Write([]byte("Invaild User!!"))
return
}
session, _ := store.Get(r, "session")
session.Values["username"] = username
session.Save(r, w)
http.Redirect(w, r, "/", 302)
}
//lets test the login
func TestLogin(w http.ResponseWriter, r *http.Request) {
session, _ := store.Get(r, "session")
value, ok := session.Values["username"]
if !ok {
return
}
username, ok := value.(string)
if !ok {
return
}
w.Write([]byte(username))
}
func RegisterGetHandler(w http.ResponseWriter, r *http.Request) {
templates.ExecuteTemplate(w, "register.html", nil)
}
func RegisterPostHandler(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
username := r.PostForm.Get("username")
password := r.PostForm.Get("password")
cost := bcrypt.DefaultCost
hash, err := bcrypt.GenerateFromPassword([]byte(password), cost)
if err != nil {
log.Fatal(http.StatusInternalServerError)
return
}
client.Set("user:"+username, hash, 0)
http.Redirect(w, r, "/login", 302)
}