-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
74 lines (65 loc) · 1.46 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
package main
import (
"github.com/gorilla/context"
"github.com/gorilla/sessions"
"io"
"net/http"
)
var store = sessions.NewCookieStore([]byte("secret-password"))
func authenticate(res http.ResponseWriter, req *http.Request) {
session, _ := store.Get(req, "session")
// check log in
if req.Method == "POST" {
password := req.FormValue("password")
if password == "secret" {
session.Values["loggedin"] = "true"
}
}
// if logout, then logout
if req.URL.Path == "/logout" {
session.Values["loggedin"] = "false"
}
session.Save(req, res)
var html string
// not logged in
if session.Values["loggedin"] == "false" ||
session.Values["loggedin"] == nil {
html = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h1>LOG IN</h1>
<form method="post" action="/">
<h3>User name</h3>
<input type="text" name="userName" id="userName">
<h3>Password</h3>
<input type="text" name="password" id="password">
<br>
<input type="submit">
<input type="submit" name="logout" value="logout">
</form>
</body>
</html>`
} else {
html = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h1><a href="logout">LOG OUT</a></h1>
</body>
</html>`
}
io.WriteString(res, html)
}
func main() {
http.HandleFunc("/", authenticate)
http.ListenAndServe(":8080", context.ClearHandler(http.DefaultServeMux))
}