1
1
package main
2
2
3
3
import (
4
- "encoding/json"
5
4
"fmt"
6
5
"html/template"
7
6
"log"
8
7
"math/rand"
9
8
"net/http"
10
- "net/url"
11
9
"os/exec"
12
- "regexp"
13
10
"strconv"
14
11
"time"
15
12
16
13
"github.com/boltdb/bolt"
17
14
"github.com/julienschmidt/httprouter"
18
15
)
19
16
20
- const DEBUG bool = false
21
-
22
- type BlogDetails struct {
23
- Blogname string `json:"blogname"`
24
- Website string `json:"website"`
25
- }
26
-
27
17
// TODO add bcrypt
28
18
29
19
func init () {
@@ -56,13 +46,6 @@ func init() {
56
46
}
57
47
return nil
58
48
})
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
- })
66
49
}
67
50
68
51
func LoginPage (w http.ResponseWriter , req * http.Request , _ httprouter.Params ) {
@@ -82,7 +65,7 @@ func LoginHandler(w http.ResponseWriter, req *http.Request, p httprouter.Params)
82
65
if verifyUser (w , req , email , password ) {
83
66
http .Redirect (w , req , "/admin/" , http .StatusFound )
84
67
} else {
85
- http . Redirect (w , req , "/error/ Invalid email or password", http . StatusFound )
68
+ fmt . Fprintf (w , " Invalid email/ password" )
86
69
}
87
70
}
88
71
@@ -116,17 +99,6 @@ func MainPage(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
116
99
})
117
100
}
118
101
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
-
130
102
func SignupPage (w http.ResponseWriter , r * http.Request , _ httprouter.Params ) {
131
103
baseT := template .Must (template .New ("base" ).Parse (base ))
132
104
baseT = template .Must (baseT .Parse (signup ))
@@ -168,45 +140,27 @@ func SignupHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params)
168
140
}
169
141
170
142
func 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 ) != "" {
178
144
179
145
baseT := template .Must (template .New ("base" ).Parse (base ))
180
146
baseT = template .Must (baseT .Parse (admin ))
181
147
182
- baseT .ExecuteTemplate (w , "base" , map [string ]interface {} {
148
+ baseT .ExecuteTemplate (w , "base" , map [string ]string {
183
149
"PageName" : "admin" ,
184
- "User" : username ,
185
- "Blogs" : getBlogsForUser (db , username ),
150
+ "User" : getUser (w , r ),
186
151
})
187
152
} 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
189
154
}
190
155
}
191
156
192
157
func 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
195
160
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
-
206
161
blogcheck := []byte ("" )
207
162
208
- username := getUser (w , r )
209
- if username != "" {
163
+ if getUser (w , r ) != "" {
210
164
db , err := bolt .Open ("goblog.db" , 0600 , nil )
211
165
if err != nil {
212
166
fmt .Println (err )
@@ -217,69 +171,27 @@ func AdminHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params)
217
171
blogcheck = b .Get ([]byte (blogname ))
218
172
return nil
219
173
})
220
-
221
174
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 {
224
177
fmt .Println (err )
225
178
} else {
226
179
fmt .Println ("80 -> " + strconv .Itoa (port ))
227
- fmt .Println ( create )
180
+ fmt .Fprintf ( w , "%s" , create )
228
181
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
231
184
return err
232
185
})
233
- addBlogToUser (db , username , blogname , website )
234
- http .Redirect (w , r , "/admin/" , http .StatusFound )
235
- return
236
186
}
237
187
} 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!" )
240
189
}
241
190
} 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
244
192
}
245
193
}
246
194
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
-
283
195
func verifyUser (w http.ResponseWriter , r * http.Request , email string , password string ) bool {
284
196
correctpass := []byte ("" )
285
197
db , err := bolt .Open ("goblog.db" , 0600 , nil )
@@ -393,16 +305,6 @@ func getUserFromCookie(value string) string {
393
305
return ""
394
306
}
395
307
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
-
406
308
func main () {
407
309
fmt .Println ("Started server on port 1337" )
408
310
router := httprouter .New ()
@@ -413,6 +315,5 @@ func main() {
413
315
router .GET ("/admin/" , AdminPage )
414
316
router .POST ("/admin/" , AdminHandler )
415
317
router .GET ("/logout/" , LogoutHandler )
416
- router .GET ("/error/:errorcode/" , ErrorPage )
417
318
log .Fatal (http .ListenAndServe (":1337" , router ))
418
319
}
0 commit comments