diff options
author | Steve Manuel <nilslice@gmail.com> | 2016-10-28 12:58:25 -0700 |
---|---|---|
committer | Steve Manuel <nilslice@gmail.com> | 2016-10-28 12:58:25 -0700 |
commit | c178f2e403b0b711d8aa3c0155d1368827f88afd (patch) | |
tree | 3550104fd9ed23859947ecce1cff8c41480d6288 | |
parent | 5c187f700fd1e868078d831b53fbfc0e256142b5 (diff) |
adding some clean up code and UI toggle for future external vs. internal posted content
-rw-r--r-- | cmd/ponzu/main.go | 6 | ||||
-rw-r--r-- | content/post.go | 5 | ||||
-rw-r--r-- | system/admin/handlers.go | 55 | ||||
-rw-r--r-- | system/db/content.go | 2 | ||||
-rw-r--r-- | system/db/init.go | 18 |
5 files changed, 71 insertions, 15 deletions
diff --git a/cmd/ponzu/main.go b/cmd/ponzu/main.go index 48aef9e..234d3b6 100644 --- a/cmd/ponzu/main.go +++ b/cmd/ponzu/main.go @@ -11,6 +11,7 @@ import ( "github.com/bosssauce/ponzu/system/admin" "github.com/bosssauce/ponzu/system/api" + "github.com/bosssauce/ponzu/system/api/analytics" "github.com/bosssauce/ponzu/system/db" "github.com/bosssauce/ponzu/system/tls" ) @@ -169,6 +170,11 @@ func main() { case "serve", "s": db.Init() + defer db.Close() + + analytics.Init() + defer analytics.Close() + if len(args) > 1 { services := strings.Split(args[1], ",") diff --git a/content/post.go b/content/post.go index 37ad660..dcdfeff 100644 --- a/content/post.go +++ b/content/post.go @@ -88,8 +88,3 @@ func (p *Post) SetSlug(slug string) { p.Slug = slug } // Editor partially implements editor.Editable func (p *Post) Editor() *editor.Editor { return &p.editor } - -// // Accepts accepts or recjects external requests to submit Post content -// func (p *Post) Accepts() bool { -// return true -// } diff --git a/system/admin/handlers.go b/system/admin/handlers.go index d508ef2..8cbfe0a 100644 --- a/system/admin/handlers.go +++ b/system/admin/handlers.go @@ -15,6 +15,7 @@ import ( "github.com/bosssauce/ponzu/management/manager" "github.com/bosssauce/ponzu/system/admin/config" "github.com/bosssauce/ponzu/system/admin/user" + "github.com/bosssauce/ponzu/system/api" "github.com/bosssauce/ponzu/system/db" "github.com/nilslice/jwt" @@ -535,7 +536,21 @@ func postsHandler(res http.ResponseWriter, req *http.Request) { posts := db.ContentAll(t + "_sorted") b := &bytes.Buffer{} - p, ok := content.Types[t]().(editor.Editable) + + if _, ok := content.Types[t]; !ok { + res.WriteHeader(http.StatusBadRequest) + errView, err := Error405() + if err != nil { + return + } + + res.Write(errView) + return + } + + pt := content.Types[t]() + + p, ok := pt.(editor.Editable) if !ok { res.WriteHeader(http.StatusInternalServerError) errView, err := Error500() @@ -547,6 +562,12 @@ func postsHandler(res http.ResponseWriter, req *http.Request) { return } + var hasExt bool + ext, ok := pt.(api.Externalable) + if ok { + hasExt = true + } + html := `<div class="col s9 card"> <div class="card-content"> <div class="row"> @@ -604,8 +625,36 @@ func postsHandler(res http.ResponseWriter, req *http.Request) { <input type="hidden" name="type" value="` + t + `" /> </div> </form> - </div> - <ul class="posts row">` + </div>` + if hasExt { + created := q.Get("created") + switch created { + q.Set("created", "internal") + intURL := strings.TrimPrefix(req.URL.String(), req.URL.Scheme+req.URL.Host) + + q.Set("created", "external") + extURL := strings.TrimPrefix(req.URL.String(), req.URL.Scheme+req.URL.Host) + + case "internal": + + html += `<div class="row"> + Created by: + <a class="active" href="`+ intURL +`">Internal</a> + | + <a href="`+ extURL +`">External</a> + </div>` + + case "external": + html += `<div class="row"> + Created by: + <a href="`+ intURL +`">Internal</a> + | + <a class="active" href="`+ extURL +`">External</a> + </div>` + } + + } + html += `<ul class="posts row">` if order == "desc" || order == "" { // keep natural order of posts slice, as returned from sorted bucket diff --git a/system/db/content.go b/system/db/content.go index 39ab5c2..6e70c1b 100644 --- a/system/db/content.go +++ b/system/db/content.go @@ -24,8 +24,6 @@ func SetContent(target string, data url.Values) (int, error) { t := strings.Split(target, ":") ns, id := t[0], t[1] - log.Println(ns, id, data) - // check if content id == -1 (indicating new post). // if so, run an insert which will assign the next auto incremented int. // this is done because boltdb begins its bucket auto increment value at 0, diff --git a/system/db/init.go b/system/db/init.go index 1a5ed25..e5a588a 100644 --- a/system/db/init.go +++ b/system/db/init.go @@ -13,12 +13,20 @@ import ( var store *bolt.DB +// Close exports the abillity to close our db file. Should be called with defer +// after call to Init() from the same place. +func Close() { + err := store.Close() + if err != nil { + log.Println(err) + } +} + // Init creates a db connection, initializes db with required info, sets secrets func Init() { - var err error - store, err = bolt.Open("store.db", 0666, nil) + store, err := bolt.Open("system.db", 0666, nil) if err != nil { - log.Fatal(err) + log.Fatalln(err) } err = store.Update(func(tx *bolt.Tx) error { @@ -67,7 +75,7 @@ func Init() { return nil }) if err != nil { - log.Fatal("Coudn't initialize db with buckets.", err) + log.Fatalln("Coudn't initialize db with buckets.", err) } // sort all content into type_sorted buckets @@ -99,7 +107,7 @@ func SystemInitComplete() bool { }) if err != nil { complete = false - log.Fatal(err) + log.Fatalln(err) } return complete |