11package main
22
33import (
4- "fmt"
54 "html/template"
65 "log"
76 "net/http"
7+ "time"
88
99 "github.com/julienschmidt/httprouter"
1010)
@@ -15,34 +15,51 @@ func LoginPage(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
1515
1616 baseT .ExecuteTemplate (w , "base" , map [string ]string {
1717 "PageName" : "login" ,
18+ "User" : getUser (w , req ),
1819 })
1920}
2021
22+ func LoginHandler (w http.ResponseWriter , req * http.Request , p httprouter.Params ) {
23+ username := p .ByName ("email" )
24+ password := p .ByName ("password" )
25+
26+ if verifyUser (w , req , username , password ) {
27+ http .Redirect (w , req , "/admin/" , http .StatusFound )
28+ }
29+ }
30+
31+ func LogoutHandler (w http.ResponseWriter , req * http.Request , p httprouter.Params ) {
32+ http .Redirect (w , req , "/" , http .StatusFound )
33+ }
34+
2135func MainPage (w http.ResponseWriter , r * http.Request , _ httprouter.Params ) {
2236 baseT := template .Must (template .New ("base" ).Parse (base ))
2337 baseT = template .Must (baseT .Parse (mainPage ))
2438
2539 baseT .ExecuteTemplate (w , "base" , map [string ]string {
2640 "PageName" : "main" ,
41+ "User" : getUser (w , r ),
2742 })
2843}
2944
30- func SignupPage (w http.ResponseWriter , r * http.Request , ps httprouter.Params ) {
31- username := ps .ByName ("username" )
32- password := ps .ByName ("password" )
33-
45+ func SignupPage (w http.ResponseWriter , r * http.Request , _ httprouter.Params ) {
3446 baseT := template .Must (template .New ("base" ).Parse (base ))
3547 baseT = template .Must (baseT .Parse (signup ))
3648
3749 baseT .ExecuteTemplate (w , "base" , map [string ]string {
3850 "PageName" : "signup" ,
51+ "User" : getUser (w , r ),
3952 })
53+ }
54+
55+ func SignupHandler (w http.ResponseWriter , r * http.Request , ps httprouter.Params ) {
56+ username := ps .ByName ("email" )
57+ password := ps .ByName ("password" )
4058
41- // create new user
42- if len (username )+ len (password ) < 11 {
43- fmt .Fprintf (w , "New user" + username + " with pass:" + password + "has signed up" )
59+ if addUser (username , password ) {
60+ http .Redirect (w , r , "/admin/" , http .StatusFound )
4461 } else {
45- fmt . Fprintf (w , "Invalid arguments!" )
62+ http . Redirect (w , r , "/signup/" , http . StatusFound )
4663 }
4764}
4865
@@ -59,14 +76,45 @@ func AdminPage(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
5976
6077 baseT .ExecuteTemplate (w , "base" , map [string ]string {
6178 "PageName" : "admin" ,
79+ "User" : getUser (w , r ),
6280 })
6381}
6482
83+ func verifyUser (w http.ResponseWriter , r * http.Request , username string , password string ) bool {
84+ cookie := http.Cookie {Name : "goblog" , Value : "blah" , Expires : time .Now ().Add (time .Hour * 24 * 7 * 52 ), HttpOnly : true , MaxAge : 50000 , Path : "/" }
85+ http .SetCookie (w , & cookie )
86+ return true
87+ }
88+
89+ func addUser (username string , password string ) bool {
90+ // Insert into database
91+
92+ return true
93+ }
94+
95+ func getUser (w http.ResponseWriter , r * http.Request ) string {
96+ cookie , err := r .Cookie ("goblog" )
97+
98+ if err == nil {
99+ if cookie .Value != "" {
100+ return cookieToUsername (cookie .Value )
101+ }
102+ }
103+ return ""
104+ }
105+
106+ func cookieToUsername (cookie string ) string {
107+ 108+ }
109+
65110func main () {
66111 router := httprouter .New ()
67112 router .GET ("/" , MainPage )
68- router .GET ("/login/" , LoginPage )
113+ //router.GET("/login/", LoginPage)
114+ router .POST ("/login/" , LoginHandler )
69115 router .GET ("/signup/" , SignupPage )
116+ router .POST ("/signup/" , SignupHandler )
70117 router .GET ("/admin/" , AdminPage )
118+ router .GET ("/logout/" , LogoutHandler )
71119 log .Fatal (http .ListenAndServe (":1337" , router ))
72120}
0 commit comments