11package main
22
33import (
4- "encoding/json"
54 "fmt"
65 "html/template"
76 "log"
87 "math/rand"
98 "net/http"
10- "net/url"
119 "os/exec"
12- "regexp"
1310 "strconv"
1411 "time"
1512
1613 "github.com/boltdb/bolt"
1714 "github.com/julienschmidt/httprouter"
1815)
1916
20- const DEBUG bool = false
21-
22- type BlogDetails struct {
23- Blogname string `json:"blogname"`
24- Website string `json:"website"`
25- }
26-
2717// TODO add bcrypt
2818
2919func init () {
@@ -56,13 +46,6 @@ func init() {
5646 }
5747 return nil
5848 })
59- db .Update (func (tx * bolt.Tx ) error {
60- _ , err := tx .CreateBucketIfNotExists ([]byte ("UserToBlog" )) // user -> blogdetails
61- if err != nil {
62- return fmt .Errorf ("Error with UserToBlog: %s" , err )
63- }
64- return nil
65- })
6649}
6750
6851func LoginPage (w http.ResponseWriter , req * http.Request , _ httprouter.Params ) {
@@ -82,7 +65,7 @@ func LoginHandler(w http.ResponseWriter, req *http.Request, p httprouter.Params)
8265 if verifyUser (w , req , email , password ) {
8366 http .Redirect (w , req , "/admin/" , http .StatusFound )
8467 } else {
85- http . Redirect (w , req , "/error/ Invalid email or password", http . StatusFound )
68+ fmt . Fprintf (w , " Invalid email/ password" )
8669 }
8770}
8871
@@ -116,17 +99,6 @@ func MainPage(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
11699 })
117100}
118101
119- func ErrorPage (w http.ResponseWriter , r * http.Request , pm httprouter.Params ) {
120- baseT := template .Must (template .New ("base" ).Parse (base ))
121- baseT = template .Must (baseT .Parse (errorPage ))
122-
123- baseT .ExecuteTemplate (w , "base" , map [string ]string {
124- "PageName" : "error" ,
125- "User" : getUser (w , r ),
126- "Error" : pm .ByName ("errorcode" ),
127- })
128- }
129-
130102func SignupPage (w http.ResponseWriter , r * http.Request , _ httprouter.Params ) {
131103 baseT := template .Must (template .New ("base" ).Parse (base ))
132104 baseT = template .Must (baseT .Parse (signup ))
@@ -168,45 +140,27 @@ func SignupHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params)
168140}
169141
170142func AdminPage (w http.ResponseWriter , r * http.Request , ps httprouter.Params ) {
171- username := getUser (w , r )
172- if username != "" {
173- db , err := bolt .Open ("goblog.db" , 0600 , nil )
174- if err != nil {
175- fmt .Println (err )
176- }
177- defer db .Close ()
143+ if getUser (w , r ) != "" {
178144
179145 baseT := template .Must (template .New ("base" ).Parse (base ))
180146 baseT = template .Must (baseT .Parse (admin ))
181147
182- baseT .ExecuteTemplate (w , "base" , map [string ]interface {} {
148+ baseT .ExecuteTemplate (w , "base" , map [string ]string {
183149 "PageName" : "admin" ,
184- "User" : username ,
185- "Blogs" : getBlogsForUser (db , username ),
150+ "User" : getUser (w , r ),
186151 })
187152 } else {
188- http . Redirect (w , r , "/error/ You must be authenticated!", http . StatusFound )
153+ fmt . Fprintf (w , " You must be authenticated!") // TODO make this look better
189154 }
190155}
191156
192157func AdminHandler (w http.ResponseWriter , r * http.Request , ps httprouter.Params ) {
193- blogname := r .FormValue ("blogname" )
194- websiteOriginal := r . FormValue ( "website" )
158+ blogname := r .FormValue ("blogname" ) // <- subdomain
159+ website := blogname + ".goblog.pw" // Change this
195160 port := rand .Intn (63000 ) + 2000
196-
197- website , err := checkUrl (websiteOriginal )
198- if err != nil {
199- http .Redirect (w , r , fmt .Sprintf ("/error/%s is not a valid url" , websiteOriginal ), http .StatusFound )
200- return
201- }
202-
203- re := regexp .MustCompile ("[^A-Za-z]" )
204- blogname = re .ReplaceAllString (blogname , "" )
205-
206161 blogcheck := []byte ("" )
207162
208- username := getUser (w , r )
209- if username != "" {
163+ if getUser (w , r ) != "" {
210164 db , err := bolt .Open ("goblog.db" , 0600 , nil )
211165 if err != nil {
212166 fmt .Println (err )
@@ -217,69 +171,27 @@ func AdminHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params)
217171 blogcheck = b .Get ([]byte (blogname ))
218172 return nil
219173 })
220-
221174 if blogcheck == nil {
222- create , err := exec .Command ("./create.sh" , blogname , website , strconv .Itoa (port )).Output ()
223- if err != nil && ! DEBUG {
175+ create , err := exec .Command ("./create.sh" , blogname , strconv .Itoa (port )).Output ()
176+ if err != nil {
224177 fmt .Println (err )
225178 } else {
226179 fmt .Println ("80 -> " + strconv .Itoa (port ))
227- fmt .Println ( create )
180+ fmt .Fprintf ( w , "%s" , create )
228181 db .Update (func (tx * bolt.Tx ) error {
229- b := tx .Bucket ([]byte ("BlogMappingBucket " ))
230- err := b .Put ([]byte (blogname ), []byte (website ))
182+ b := tx .Bucket ([]byte ("UsersBucket " ))
183+ err := b .Put ([]byte (blogname ), []byte (website )) // <- TODO change this
231184 return err
232185 })
233- addBlogToUser (db , username , blogname , website )
234- http .Redirect (w , r , "/admin/" , http .StatusFound )
235- return
236186 }
237187 } else {
238- http .Redirect (w , r , "/error/Failure creating blog! Please choose a different name!" , http .StatusFound )
239- return
188+ fmt .Fprintf (w , "Failure creating blog! Please choose a different name!" )
240189 }
241190 } else {
242- http .Redirect (w , r , "/error/You must be authenticated!" , http .StatusFound )
243- return
191+ fmt .Fprintf (w , "You must be authenticated!" ) // TODO make this look better
244192 }
245193}
246194
247- func addBlogToUser (db * bolt.DB , username string , blogname string , website string ) {
248- existingblogs := []byte ("" )
249-
250- db .View (func (tx * bolt.Tx ) error {
251- b := tx .Bucket ([]byte ("UserToBlog" ))
252- existingblogs = b .Get ([]byte (username ))
253- return nil
254- })
255-
256- var v []BlogDetails = make ([]BlogDetails , 0 )
257- json .Unmarshal (existingblogs , & v )
258-
259- db .Update (func (tx * bolt.Tx ) error {
260- b := tx .Bucket ([]byte ("UserToBlog" ))
261- v = append (v , BlogDetails {blogname , website })
262- m , _ := json .Marshal (v )
263- err := b .Put ([]byte (username ), m )
264- return err
265- })
266- }
267-
268- func getBlogsForUser (db * bolt.DB , username string ) []BlogDetails {
269- existingblogs := []byte ("" )
270-
271- db .View (func (tx * bolt.Tx ) error {
272- b := tx .Bucket ([]byte ("UserToBlog" ))
273- existingblogs = b .Get ([]byte (username ))
274- return nil
275- })
276-
277- var v []BlogDetails = make ([]BlogDetails , 0 )
278- json .Unmarshal (existingblogs , & v )
279-
280- return v
281- }
282-
283195func verifyUser (w http.ResponseWriter , r * http.Request , email string , password string ) bool {
284196 correctpass := []byte ("" )
285197 db , err := bolt .Open ("goblog.db" , 0600 , nil )
@@ -393,16 +305,6 @@ func getUserFromCookie(value string) string {
393305 return ""
394306}
395307
396- func checkUrl (s string ) (string , error ) {
397- u , err := url .Parse (s )
398-
399- if err != nil || u .Host == "" {
400- u , err = url .Parse ("http://" + s )
401- }
402-
403- return u .Host , err
404- }
405-
406308func main () {
407309 fmt .Println ("Started server on port 1337" )
408310 router := httprouter .New ()
@@ -413,6 +315,5 @@ func main() {
413315 router .GET ("/admin/" , AdminPage )
414316 router .POST ("/admin/" , AdminHandler )
415317 router .GET ("/logout/" , LogoutHandler )
416- router .GET ("/error/:errorcode/" , ErrorPage )
417318 log .Fatal (http .ListenAndServe (":1337" , router ))
418319}
0 commit comments