11package main
22
33import (
4+ "fmt"
45 "html/template"
56 "log"
67 "net/http"
8+
9+ "github.com/julienschmidt/httprouter"
710)
811
9- func LoginPage (w http.ResponseWriter , req * http.Request ) {
12+ func LoginPage (w http.ResponseWriter , req * http.Request , _ httprouter. Params ) {
1013 baseT := template .Must (template .New ("base" ).Parse (base ))
1114 baseT = template .Must (baseT .Parse (login ))
1215
@@ -15,40 +18,55 @@ func LoginPage(w http.ResponseWriter, req *http.Request) {
1518 })
1619}
1720
18- func SignupPage (w http.ResponseWriter , req * http.Request ) {
21+ func MainPage (w http.ResponseWriter , r * http.Request , _ httprouter. Params ) {
1922 baseT := template .Must (template .New ("base" ).Parse (base ))
20- baseT = template .Must (baseT .Parse (signup ))
23+ baseT = template .Must (baseT .Parse (mainPage ))
2124
2225 baseT .ExecuteTemplate (w , "base" , map [string ]string {
23- "PageName" : "signup " ,
26+ "PageName" : "main " ,
2427 })
2528}
2629
27- func AdminPage (w http.ResponseWriter , req * http.Request ) {
30+ func SignupPage (w http.ResponseWriter , r * http.Request , ps httprouter.Params ) {
31+ username := ps .ByName ("username" )
32+ password := ps .ByName ("password" )
33+
2834 baseT := template .Must (template .New ("base" ).Parse (base ))
29- baseT = template .Must (baseT .Parse (admin ))
35+ baseT = template .Must (baseT .Parse (signup ))
3036
3137 baseT .ExecuteTemplate (w , "base" , map [string ]string {
32- "PageName" : "admin " ,
38+ "PageName" : "signup " ,
3339 })
40+
41+ // create new user
42+ if len (username )+ len (password ) < 11 {
43+ fmt .Fprintf (w , "New user" + username + " with pass:" + password + "has signed up" )
44+ } else {
45+ fmt .Fprintf (w , "Invalid arguments!" )
46+ }
3447}
3548
36- func MainPage (w http.ResponseWriter , req * http.Request ) {
49+ func AdminPage (w http.ResponseWriter , r * http.Request , ps httprouter.Params ) {
50+ // authenticate username and password
51+ blogname := ps .ByName ("blogname" )
52+
53+ if len (blogname ) < 20 {
54+ // create new blog
55+ }
56+
3757 baseT := template .Must (template .New ("base" ).Parse (base ))
38- baseT = template .Must (baseT .Parse (mainPage ))
58+ baseT = template .Must (baseT .Parse (admin ))
3959
4060 baseT .ExecuteTemplate (w , "base" , map [string ]string {
41- "PageName" : "main " ,
61+ "PageName" : "admin " ,
4262 })
4363}
4464
4565func main () {
46- http .HandleFunc ("/login" , LoginPage )
47- http .HandleFunc ("/admin" , AdminPage )
48- http .HandleFunc ("/signup" , SignupPage )
49- http .HandleFunc ("/" , MainPage )
50- err := http .ListenAndServe (":1337" , nil )
51- if err != nil {
52- log .Fatal ("ListenAndServe: " , err )
53- }
66+ router := httprouter .New ()
67+ router .GET ("/" , MainPage )
68+ router .GET ("/login/" , LoginPage )
69+ router .GET ("/signup/" , SignupPage )
70+ router .GET ("/admin/" , AdminPage )
71+ log .Fatal (http .ListenAndServe (":1337" , router ))
5472}
0 commit comments