Skip to content

Commit aacaf04

Browse files
committed
Add create blog
1 parent 575d131 commit aacaf04

File tree

3 files changed

+136
-4
lines changed

3 files changed

+136
-4
lines changed

create.sh

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/bin/bash
2+
# Script written by Faraz
3+
# 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" ]
8+
then
9+
echo 'Error, not enough arguments!'
10+
exit 2
11+
fi
12+
SITENAME=journey-$SITENAME
13+
mkdir -p /var/www
14+
cd /var/www
15+
echo "This will delete the folder $SITENAME! Press CTRL + C to quit, or press enter to continue!"
16+
read input
17+
mkdir $SITENAME
18+
cd $SITENAME
19+
echo "Getting latest release!" # TODO make this a symlink for all except config and db files
20+
wget https://github.com/kabukky/journey/releases/download/v0.1.9/journey-linux-amd64.zip
21+
unzip journey-linux-amd64.zip
22+
rm journey-linux-amd64.zip
23+
mv journey-linux-amd64/ journey
24+
cd journey
25+
echo "Configuring config.json"
26+
sed -i -e "s/8084/$PORT/g" config.json
27+
sed -i -e "s/127.0.0.1:$PORT/$SITEURL:$PORT/g" config.json
28+
echo "start on runlevel [2345]" >> /etc/init/$SITENAME.conf
29+
echo "stop on runlevel [!2345]" >> /etc/init/$SITENAME.conf
30+
echo "respawn" >> /etc/init/$SITENAME.conf
31+
echo "console none" >> /etc/init/$SITENAME.conf
32+
echo "exec /var/www/$SITENAME/journey/journey -log=/var/www/$SITENAME/journey/log.txt" >> /etc/init/$SITENAME.conf
33+
cd /etc/nginx/sites-enabled
34+
echo "Proxying port from 80 to $PORT"
35+
echo "server {" >> $SITENAME.conf
36+
echo "listen 0.0.0.0:80;" >> $SITENAME.conf
37+
echo "server_name $SITEURL;" >> $SITENAME.conf
38+
echo "access_log /var/log/nginx/$SITENAME.log;" >> $SITENAME.conf
39+
echo "location / {" >> $SITENAME.conf
40+
echo "proxy_pass http://127.0.0.1:$PORT;" >> $SITENAME.conf
41+
echo " }" >> $SITENAME.conf
42+
echo "}" >> $SITENAME.conf
43+
echo "server {" >> $SITENAME.conf
44+
echo "listen 0.0.0.0:80;" >> $SITENAME.conf
45+
echo "server_name www.$SITEURL;" >> $SITENAME.conf
46+
echo "access_log /var/log/nginx/$SITENAME.log;" >> $SITENAME.conf
47+
echo "location / {" >> $SITENAME.conf
48+
echo "proxy_pass http://127.0.0.1:$PORT;" >> $SITENAME.conf
49+
echo " }" >> $SITENAME.conf
50+
echo "}" >> $SITENAME.conf
51+
service $SITENAME start
52+
service nginx restart
53+
echo "---------------------------------------------------"
54+
echo "You may need to oonfigure your DNS Records if you used a custom domain!"
55+
echo "ALL DONE! $SITEURL is viewable as a Journey blog!"
56+
echo "Setup at $SITEURL/admin"

main.go

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"log"
77
"math/rand"
88
"net/http"
9+
"os/exec"
910
"time"
1011

1112
"github.com/boltdb/bolt"
@@ -40,7 +41,7 @@ func init() {
4041
db.Update(func(tx *bolt.Tx) error {
4142
_, err := tx.CreateBucketIfNotExists([]byte("BlogMappingBucket")) // random string -> email
4243
if err != nil {
43-
return fmt.Errorf("Error with BlockMappingBucket: %s", err)
44+
return fmt.Errorf("Error with BlogMappingBucket: %s", err)
4445
}
4546
return nil
4647
})
@@ -112,7 +113,6 @@ func SignupHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params)
112113
password := r.FormValue("password")
113114

114115
if addUser(email, password) {
115-
fmt.Println("Success!")
116116
cookie := http.Cookie{Name: "goblog", Value: RandomString(), Expires: time.Now().Add(time.Hour * 24 * 7 * 52), HttpOnly: true, MaxAge: 50000, Path: "/"}
117117
http.SetCookie(w, &cookie)
118118
db, err := bolt.Open("goblog.db", 0600, nil)
@@ -153,6 +153,44 @@ func AdminPage(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
153153
}
154154
}
155155

156+
func AdminHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
157+
blogname := r.FormValue("blogname")
158+
website := r.FormValue("website")
159+
port := 1000
160+
blogcheck := []byte("")
161+
162+
if getUser(w, r) != "" {
163+
db, err := bolt.Open("goblog.db", 0600, nil)
164+
if err != nil {
165+
fmt.Println(err)
166+
}
167+
defer db.Close()
168+
db.View(func(tx *bolt.Tx) error {
169+
b := tx.Bucket([]byte("BlogMappingBucket"))
170+
blogcheck = b.Get([]byte(blogname))
171+
return nil
172+
})
173+
if blogcheck == nil {
174+
create, err := exec.Command("./create.sh", blogname, website, string(port)).Output()
175+
if err != nil {
176+
fmt.Println(err)
177+
} else {
178+
fmt.Fprintf(w, "%s", create)
179+
db.Update(func(tx *bolt.Tx) error {
180+
b := tx.Bucket([]byte("UsersBucket"))
181+
err := b.Put([]byte(blogname), []byte(website))
182+
return err
183+
})
184+
}
185+
} else {
186+
fmt.Println("Failure!")
187+
http.Redirect(w, r, "/", http.StatusFound)
188+
}
189+
} else {
190+
fmt.Fprintf(w, "You must be authenticated!") // TODO make this look better
191+
}
192+
}
193+
156194
func verifyUser(w http.ResponseWriter, r *http.Request, email string, password string) bool {
157195
correctpass := []byte("")
158196
db, err := bolt.Open("goblog.db", 0600, nil)
@@ -273,6 +311,7 @@ func main() {
273311
router.GET("/signup/", SignupPage)
274312
router.POST("/signup/", SignupHandler)
275313
router.GET("/admin/", AdminPage)
314+
router.POST("/admin/", AdminHandler)
276315
router.GET("/logout/", LogoutHandler)
277316
log.Fatal(http.ListenAndServe(":1338", router))
278317
}

pages.go

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,44 @@ var login = `
113113

114114
var admin = `
115115
{{define "content"}}
116-
Admin
116+
<div class="col-md-6 col-md-offset-3">
117+
118+
<form class="form-horizontal" action="/admin/" method="POST">
119+
<fieldset>
120+
121+
<!-- Form Name -->
122+
<legend>Create a new blog</legend>
123+
124+
<!-- Text input-->
125+
<div class="form-group">
126+
<label class="col-md-4 control-label" for="blog">Blog Name</label>
127+
<div class="col-md-6">
128+
<input id="blogname" name="blogname" type="text" placeholder="Blog Name" class="form-control input-md" required="">
129+
130+
</div>
131+
</div>
132+
133+
<!-- Text input-->
134+
<div class="form-group">
135+
<label class="col-md-4 control-label" for="website">Blog Website</label>
136+
<div class="col-md-6">
137+
<input id="blogname" name="website" type="text" placeholder="Blog Website" class="form-control input-md" required="">
138+
139+
</div>
140+
</div>
141+
142+
<!-- Button -->
143+
<div class="form-group">
144+
<label class="col-md-4 control-label" for="submit"></label>
145+
<div class="col-md-4">
146+
<button id="submit" name="submit" class="btn btn-success">Create Blog</button>
147+
</div>
148+
</div>
149+
150+
</fieldset>
151+
</form>
152+
153+
</div>
117154
{{end}}
118155
`
119156

@@ -131,7 +168,7 @@ var signup = `
131168
<div class="form-group">
132169
<label class="col-md-4 control-label" for="email">E-mail address</label>
133170
<div class="col-md-6">
134-
<input id="email" name="email" type="text" placeholder="E-mail address" class="form-control input-md" required="">
171+
<input id="email" name="email" type="email" placeholder="E-mail address" class="form-control input-md" required="">
135172
136173
</div>
137174
</div>

0 commit comments

Comments
 (0)