Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ed37d3b
commit a26ae53
Showing
6 changed files
with
190 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package main | ||
|
||
import ( | ||
"html/template" | ||
"net/http" | ||
|
||
uuid "github.com/satori/go.uuid" | ||
) | ||
|
||
type user struct { | ||
UserName string | ||
Password string | ||
First string | ||
Last string | ||
} | ||
|
||
var tpl *template.Template | ||
var dbUsers = map[string]user{} | ||
var dbSessions = make(map[string]string) | ||
|
||
func init() { | ||
tpl = template.Must(template.ParseGlob("templates/*")) | ||
} | ||
|
||
func main() { | ||
http.HandleFunc("/", index) | ||
http.HandleFunc("/bar", bar) | ||
http.HandleFunc("/signup", signup) | ||
http.Handle("/favicon.ico", http.NotFoundHandler()) | ||
http.ListenAndServe(":8080", nil) | ||
} | ||
|
||
func index(w http.ResponseWriter, req *http.Request) { | ||
u := getUser(w, req) | ||
tpl.ExecuteTemplate(w, "index.gohtml", u) | ||
} | ||
|
||
func bar(w http.ResponseWriter, req *http.Request) { | ||
|
||
c, err := req.Cookie("session-id") | ||
if err != nil { | ||
http.Redirect(w, req, "/", http.StatusSeeOther) | ||
return | ||
} | ||
un, ok := dbSessions[c.Value] | ||
if !ok { | ||
http.Redirect(w, req, "/", http.StatusSeeOther) | ||
return | ||
} | ||
u := dbUsers[un] | ||
tpl.ExecuteTemplate(w, "bar.gohtml", u) | ||
} | ||
|
||
func signup(w http.ResponseWriter, req *http.Request) { | ||
if alreadyLoggedIn(req) { | ||
http.Redirect(w, req, "/", http.StatusSeeOther) | ||
return | ||
} | ||
|
||
if req.Method == http.MethodPost { | ||
|
||
un := req.FormValue("username") | ||
p := req.FormValue("password") | ||
f := req.FormValue("firstname") | ||
l := req.FormValue("lastname") | ||
|
||
if _, ok := dbUsers[un]; ok { | ||
http.Error(w, "Username already taken", http.StatusForbidden) | ||
return | ||
} | ||
|
||
sID, _ := uuid.NewV4() | ||
c := &http.Cookie{ | ||
Name: "session-id", | ||
Value: sID.String(), | ||
} | ||
http.SetCookie(w, c) | ||
dbSessions[c.Value] = un | ||
|
||
u := user{un, p, f, l} | ||
dbUsers[un] = u | ||
|
||
http.Redirect(w, req, "/", http.StatusSeeOther) | ||
return | ||
} | ||
|
||
tpl.ExecuteTemplate(w, "signup.gohtml", nil) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package main | ||
|
||
import ( | ||
"net/http" | ||
|
||
uuid "github.com/satori/go.uuid" | ||
) | ||
|
||
func getUser(w http.ResponseWriter, req *http.Request) user { | ||
|
||
c, err := req.Cookie("session-id") | ||
if err != nil { | ||
sID, _ := uuid.NewV4() | ||
c = &http.Cookie{ | ||
Name: "session-id", | ||
Value: sID.String(), | ||
} | ||
|
||
} | ||
|
||
var u user | ||
if un, ok := dbSessions[c.Value]; ok { | ||
u = dbUsers[un] | ||
} | ||
return u | ||
} | ||
|
||
func alreadyLoggedIn(req *http.Request) bool { | ||
c, err := req.Cookie("session-id") | ||
if err != nil { | ||
return false | ||
} | ||
un := dbSessions[c.Value] | ||
_, ok := dbUsers[un] | ||
return ok | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>BAR</title> | ||
</head> | ||
<body> | ||
|
||
<h1>Welcome to the bar. What can I get you to drink?</h1> | ||
|
||
{{if .First}} | ||
USER NAME {{.UserName}}<br> | ||
FIRST {{.First}}<br> | ||
LAST {{.Last}}<br> | ||
{{end}} | ||
|
||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>Document</title> | ||
</head> | ||
<body> | ||
|
||
<form method="post"> | ||
|
||
<input type="email" name="username" placeholder="email"><br> | ||
<input type="text" name="firstname" placeholder="first name"><br> | ||
<input type="text" name="lastname" placeholder="last name"><br> | ||
<input type="submit"> | ||
|
||
</form> | ||
|
||
|
||
{{if .First}} | ||
USER NAME {{.UserName}}<br> | ||
FIRST {{.First}}<br> | ||
LAST {{.Last}}<br> | ||
{{end}} | ||
|
||
<br> | ||
<h2>Go to <a href="/bar">the bar</a></h2> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>Document</title> | ||
</head> | ||
<body> | ||
|
||
<form method="post"> | ||
|
||
<input type="email" name="username" placeholder="email"><br> | ||
<input type="text" name="password" placeholder="password"><br> | ||
<input type="text" name="firstname" placeholder="first name"><br> | ||
<input type="text" name="lastname" placeholder="last name"><br> | ||
<input type="submit"> | ||
|
||
</form> | ||
|
||
</body> | ||
</html> |