Skip to content

Commit 3791bdc

Browse files
committed
Subdomain
1 parent 6bde166 commit 3791bdc

File tree

4 files changed

+20
-128
lines changed

4 files changed

+20
-128
lines changed

GoBlog

280 Bytes
Binary file not shown.

create.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
#!/bin/bash
22
# Script written by Faraz
33
# Automates creating a new Journey Blog with nginx
4-
SITENAME="$1" # Site Name (Without URL)
5-
SITEURL="$2" # Site URL excluding http:// or www.
6-
PORT="$3" # <- Random port passed in
7-
if [ -z "$SITENAME" ] || [ -z "$SITEURL" ] || [ -z "$PORT" ]
4+
SITENAME="$1" # <- Subdomain
5+
PORT="$2" # <- Random port passed in
6+
if [ -z "$SITENAME" ] || [ -z "$PORT" ]
87
then
98
echo 'Error, not enough arguments!'
109
exit 2
1110
fi
11+
SITEURL=$SITENAME.goghost.pw
1212
SITENAME=journey-$SITENAME
1313
mkdir -p /var/www/$SITENAME
1414
cd /var/www/$SITENAME

main.go

Lines changed: 15 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,19 @@
11
package main
22

33
import (
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

2919
func 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

6851
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)
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-
130102
func 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

170142
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) != "" {
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

192157
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
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-
283195
func 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-
406308
func 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
}

pages.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -135,22 +135,13 @@ var admin = `
135135
136136
<!-- Text input-->
137137
<div class="form-group">
138-
<label class="col-md-4 control-label" for="blog">Blog Name</label>
138+
<label class="col-md-4 control-label" for="blog">Blog Name/Subdomain (.ghost.pw)</label>
139139
<div class="col-md-6">
140140
<input id="blogname" name="blogname" type="text" placeholder="exampleblog" class="form-control input-md" required="">
141141
142142
</div>
143143
</div>
144144
145-
<!-- Text input-->
146-
<div class="form-group">
147-
<label class="col-md-4 control-label" for="website">Blog Website</label>
148-
<div class="col-md-6">
149-
<input id="blogname" name="website" type="text" placeholder="example.com" class="form-control input-md" required="">
150-
151-
</div>
152-
</div>
153-
154145
<!-- Button -->
155146
<div class="form-group">
156147
<label class="col-md-4 control-label" for="submit"></label>

0 commit comments

Comments
 (0)