11package main
22
33import (
4+ "encoding/json"
45 "fmt"
56 "html/template"
67 "log"
@@ -14,6 +15,13 @@ import (
1415 "github.com/julienschmidt/httprouter"
1516)
1617
18+ const DEBUG bool = false
19+
20+ type BlogDetails struct {
21+ Blogname string `json:"blogname"`
22+ Website string `json:"website"`
23+ }
24+
1725// TODO add bcrypt
1826
1927func init () {
@@ -46,6 +54,13 @@ func init() {
4654 }
4755 return nil
4856 })
57+ db .Update (func (tx * bolt.Tx ) error {
58+ _ , err := tx .CreateBucketIfNotExists ([]byte ("UserToBlog" )) // random string -> email
59+ if err != nil {
60+ return fmt .Errorf ("Error with UserToBlog: %s" , err )
61+ }
62+ return nil
63+ })
4964}
5065
5166func LoginPage (w http.ResponseWriter , req * http.Request , _ httprouter.Params ) {
@@ -140,14 +155,21 @@ func SignupHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params)
140155}
141156
142157func AdminPage (w http.ResponseWriter , r * http.Request , ps httprouter.Params ) {
143- if getUser (w , r ) != "" {
158+ username := getUser (w , r )
159+ if username != "" {
160+ db , err := bolt .Open ("goblog.db" , 0600 , nil )
161+ if err != nil {
162+ fmt .Println (err )
163+ }
164+ defer db .Close ()
144165
145166 baseT := template .Must (template .New ("base" ).Parse (base ))
146167 baseT = template .Must (baseT .Parse (admin ))
147168
148- baseT .ExecuteTemplate (w , "base" , map [string ]string {
169+ baseT .ExecuteTemplate (w , "base" , map [string ]interface {} {
149170 "PageName" : "admin" ,
150- "User" : getUser (w , r ),
171+ "User" : username ,
172+ "Blogs" : getBlogsForUser (db , username ),
151173 })
152174 } else {
153175 fmt .Fprintf (w , "You must be authenticated!" ) // TODO make this look better
@@ -160,7 +182,8 @@ func AdminHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params)
160182 port := 1001
161183 blogcheck := []byte ("" )
162184
163- if getUser (w , r ) != "" {
185+ username := getUser (w , r )
186+ if username != "" {
164187 db , err := bolt .Open ("goblog.db" , 0600 , nil )
165188 if err != nil {
166189 fmt .Println (err )
@@ -173,15 +196,17 @@ func AdminHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params)
173196 })
174197 if blogcheck == nil {
175198 create , err := exec .Command ("./create.sh" , blogname , website , strconv .Itoa (port )).Output ()
176- if err != nil {
199+ if err != nil && ! DEBUG {
177200 fmt .Println (err )
178201 } else {
179- fmt .Fprintf ( w , "%s" , create )
202+ fmt .Printf ( "%s" , create )
180203 db .Update (func (tx * bolt.Tx ) error {
181- b := tx .Bucket ([]byte ("UsersBucket " ))
204+ b := tx .Bucket ([]byte ("BlogMappingBucket " ))
182205 err := b .Put ([]byte (blogname ), []byte (website ))
183206 return err
184207 })
208+ addBlogToUser (db , username , blogname , website )
209+ http .Redirect (w , r , "/admin/" , http .StatusFound )
185210 }
186211 } else {
187212 fmt .Fprintf (w , "Failure creating blog! Please choose a different name!" )
@@ -191,6 +216,42 @@ func AdminHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params)
191216 }
192217}
193218
219+ func addBlogToUser (db * bolt.DB , username string , blogname string , website string ) {
220+ existingblogs := []byte ("" )
221+
222+ db .View (func (tx * bolt.Tx ) error {
223+ b := tx .Bucket ([]byte ("UserToBlog" ))
224+ existingblogs = b .Get ([]byte (username ))
225+ return nil
226+ })
227+
228+ var v []BlogDetails = make ([]BlogDetails , 0 )
229+ json .Unmarshal (existingblogs , & v )
230+
231+ db .Update (func (tx * bolt.Tx ) error {
232+ b := tx .Bucket ([]byte ("UserToBlog" ))
233+ v = append (v , BlogDetails {blogname , website })
234+ m , _ := json .Marshal (v )
235+ err := b .Put ([]byte (username ), m )
236+ return err
237+ })
238+ }
239+
240+ func getBlogsForUser (db * bolt.DB , username string ) []BlogDetails {
241+ existingblogs := []byte ("" )
242+
243+ db .View (func (tx * bolt.Tx ) error {
244+ b := tx .Bucket ([]byte ("UserToBlog" ))
245+ existingblogs = b .Get ([]byte (username ))
246+ return nil
247+ })
248+
249+ var v []BlogDetails = make ([]BlogDetails , 0 )
250+ json .Unmarshal (existingblogs , & v )
251+
252+ return v
253+ }
254+
194255func verifyUser (w http.ResponseWriter , r * http.Request , email string , password string ) bool {
195256 correctpass := []byte ("" )
196257 db , err := bolt .Open ("goblog.db" , 0600 , nil )
0 commit comments