Skip to content

Commit a529600

Browse files
committed
make stuff a little more generic
1 parent 2d7c3ad commit a529600

File tree

2 files changed

+20
-30
lines changed

2 files changed

+20
-30
lines changed

main.go

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ type Blog struct {
3434
fb *formbuilder.FormBuilder
3535
c *cookiestore.CookieStore
3636
ia *goindieauth.IndieAuth
37-
li *LoginInfo
37+
profile *Profile
3838
wm *webmention.WebMention
3939
gp *gopub.GoPub
4040
}
@@ -48,20 +48,31 @@ var (
4848
Port = flag.Int("Port", 3000, "Specifies the port to listen on")
4949
)
5050

51+
type Profile struct {
52+
Name string
53+
Password string
54+
Host string
55+
Github string
56+
}
57+
5158
func main() {
5259
flag.Parse()
60+
5361
theme, _ := tartheme.LoadDir("theme")
5462
db, _ := bolt.Open("blog.db", 0666, nil)
5563

5664
db.Update(func(tx *bolt.Tx) error {
5765
tx.CreateBucketIfNotExists([]byte("config"))
5866
tx.CreateBucketIfNotExists([]byte("posts"))
5967
tx.CreateBucketIfNotExists([]byte("subs"))
60-
6168
return nil
6269
})
6370

6471
blog := &Blog{}
72+
73+
data, _ := ioutil.ReadFile("profile.json")
74+
json.Unmarshal(data, &blog.profile)
75+
6576
blog.templates = theme.Prefix("templates/").AddTemplates(template.New("default").Funcs(template.FuncMap{
6677
"Route": blog.Route,
6778
"AbsRoute": blog.AbsRoute,
@@ -75,7 +86,7 @@ func main() {
7586
"SafeHTML": SafeHTML,
7687
}))
7788
mainrouter := mux.NewRouter()
78-
blog.router = mainrouter.Host("vendaria.net").Subrouter()
89+
blog.router = mainrouter.Host(blog.profile.Host).Subrouter()
7990
blog.db = db
8091

8192
blog.fb = formbuilder.New(theme.Prefix("templates/form/").Templates())
@@ -124,9 +135,6 @@ func main() {
124135

125136
blog.gp.Hub, _ = pubhubroute.URL()
126137

127-
data, _ := ioutil.ReadFile("login.json")
128-
json.Unmarshal(data, &blog.li)
129-
130138
http.ListenAndServe(fmt.Sprintf(":%d", *Port), blog.router)
131139
}
132140

@@ -247,13 +255,6 @@ func (b *Blog) Post(rw http.ResponseWriter, req *http.Request) {
247255
}
248256

249257
func (b *Blog) AdminProfile(rw http.ResponseWriter, req *http.Request) {
250-
var profile *Profile
251-
252-
b.db.View(func(tx *bolt.Tx) error {
253-
profiledata := tx.Bucket([]byte("config")).Get([]byte("Profile"))
254-
json.Unmarshal(profiledata, profile)
255-
return nil
256-
})
257258
profileform := b.fb.GetForm("profile")
258259
data := struct {
259260
Name string
@@ -262,7 +263,7 @@ func (b *Blog) AdminProfile(rw http.ResponseWriter, req *http.Request) {
262263
}{
263264
"Profile",
264265
"Profile",
265-
profileform.Render(nil, UrlToPath(b.router.Get("AdminProfilePost").URL()), "POST"),
266+
profileform.Render(b.profile, UrlToPath(b.router.Get("AdminProfilePost").URL()), "POST"),
266267
}
267268
if err := b.templates.ExecuteTemplate(rw, "form.tpl", data); err != nil {
268269
fmt.Println(err)
@@ -271,14 +272,9 @@ func (b *Blog) AdminProfile(rw http.ResponseWriter, req *http.Request) {
271272

272273
func (b *Blog) AdminProfilePost(rw http.ResponseWriter, req *http.Request) {
273274
form := b.fb.GetForm("profile")
274-
data := &Profile{}
275-
form.Parse(req.FormValue, data)
276-
b.db.Update(func(tx *bolt.Tx) error {
277-
config := tx.Bucket([]byte("config"))
278-
jsondata, _ := json.Marshal(data)
279-
config.Put([]byte("Profile"), jsondata)
280-
return nil
281-
})
275+
form.Parse(req.FormValue, &b.profile)
276+
data, _ := json.Marshal(b.profile)
277+
ioutil.WriteFile("profile.json", data, 0600)
282278
http.Redirect(rw, req, UrlToPath(b.router.Get("AdminProfile").URL()), http.StatusSeeOther)
283279
}
284280

@@ -314,8 +310,8 @@ func (b *Blog) IACheckLogin(rw http.ResponseWriter, req *http.Request, user, pas
314310
s := b.c.GetSession(req)
315311
_, ok := s.Values["user"]
316312
s.Save(rw)
317-
if user == b.li.Me && (ok || password == b.li.Password) {
318-
s.Values["user"] = b.li.Me
313+
if user == b.AbsRoute("Home") && (ok || password == b.profile.Password) {
314+
s.Values["user"] = b.AbsRoute("Home")
319315
return true
320316
}
321317
return false

models.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,6 @@ import (
1111
"github.com/andyleap/microformats"
1212
)
1313

14-
type Profile struct {
15-
Name string
16-
HomeURL string
17-
Github string
18-
}
19-
2014
type PostData struct {
2115
Type string
2216
Content *json.RawMessage

0 commit comments

Comments
 (0)