-
Notifications
You must be signed in to change notification settings - Fork 0
/
login.go
157 lines (136 loc) · 3.81 KB
/
login.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
package main
import (
"fmt"
"github.com/gorilla/mux"
"github.com/gorilla/securecookie"
"net/http"
"gophernews"
)
// cookie handling
var cookieHandler = securecookie.New(
securecookie.GenerateRandomKey(64),
securecookie.GenerateRandomKey(32))
func setSession(userName string, response http.ResponseWriter) {
value := map[string]string{
"name": userName,
}
if encoded, err := cookieHandler.Encode("session", value); err == nil {
cookie := &http.Cookie{
Name: "session",
Value: encoded,
Path: "/",
}
http.SetCookie(response, cookie)
}
}
func clearSession(response http.ResponseWriter) {
cookie := &http.Cookie{
Name: "session",
Value: "",
Path: "/",
MaxAge: -1,
}
http.SetCookie(response, cookie)
}
func getUserName(request *http.Request) (userName string) {
if cookie, err := request.Cookie("session"); err == nil {
cookieValue := make(map[string]string)
if err = cookieHandler.Decode("session", cookie.Value, &cookieValue); err == nil {
userName = cookieValue["name"]
}
}
return userName
}
// login handler
func loginHandler(response http.ResponseWriter, request *http.Request) {
name := request.FormValue("name")
pass := request.FormValue("password")
redirectTarget := "/"
if name == "sai" && pass == "sai" {
// .. check credentials ..
setSession(name, response)
redirectTarget = "/hackerNews"
}
http.Redirect(response, request, redirectTarget, 302)
}
func logoutHandler(response http.ResponseWriter, request *http.Request) {
clearSession(response)
http.Redirect(response, request, "/", 302)
}
// index page
const indexPage = `
<h1>Login</h1>
<form method="post" action="/login">
<label for="name">User name</label>
<input type="text" id="name" name="name">
<label for="password">Password</label>
<input type="password" id="password" name="password">
<button type="submit">Login</button>
</form>
`
func indexPageHandler(response http.ResponseWriter, request *http.Request) {
fmt.Fprintf(response, indexPage)
}
// hackerNews page
const hackerNewsPage = `
<h1>%s</h1>
<hr>
<small>User: %s</small>
<div>
Click <a href="%s"> here </a> to access the link
</div>
<form method="post" action="/hackerNews">
<button type="submit">Next</button>
</form>
<form method="post" action="/logout">
<button type="submit">Logout</button>
</form>
`
func hackerNewsPageHandler(response http.ResponseWriter, request *http.Request) {
client := gophernews.NewClient()
topStories, _ := client.GetTop100();
// currentStory, _ := client.GetMaxItem();
// story, _ := client.GetStory(8412605) //=> Returns a Story struct
// comment, _ := client.GetComment(8412767) //=> Returns a Comment struct
// poll, _ := client.GetPoll(126809) //=> Returns a Poll struct
// part, _ := client.GetPart(160705)
// fmt.Println(currentStory)
i := 0;
test, _ := client.GetStory(topStories[i])
// fmt.Println("len:", topStories[150])
// for i := 0; i < 5; i++ {
// // int test
// // test = topStories[i];
// fmt.Println(client.GetStory(topStories[i]))
// }
userName := getUserName(request)
if userName != "" {
// keys := make([]int, len(test))
// i := 0
// for k := range test {
// keys[i] = k
// i++
// }
fmt.Println(test)
fmt.Println(test.Title)
fmt.Println(test.By)
fmt.Println(test.Score)
fmt.Println(test.Time)
fmt.Println(test.Type)
fmt.Println(test.URL)
fmt.Println(test.ID)
fmt.Fprintf(response, hackerNewsPage, test.Title, test.By, test.URL)
} else {
http.Redirect(response, request, "/", 302)
}
}
// server main method
var router = mux.NewRouter()
func main() {
router.HandleFunc("/", indexPageHandler)
router.HandleFunc("/hackerNews", hackerNewsPageHandler)
router.HandleFunc("/login", loginHandler).Methods("POST")
router.HandleFunc("/logout", logoutHandler).Methods("POST")
http.Handle("/", router)
http.ListenAndServe(":8000", nil)
}