diff options
author | Steve Manuel <nilslice@gmail.com> | 2016-10-26 03:55:15 -0700 |
---|---|---|
committer | Steve Manuel <nilslice@gmail.com> | 2016-10-26 03:55:15 -0700 |
commit | d6147f3fc99151687e08d40c91555e65578515d8 (patch) | |
tree | 5032556f63c299954fe1b1f621326e9af481387e /system | |
parent | e8945192276de251022bd4732178cd0143f41f78 (diff) |
adding capability to accept external content type submissions
Diffstat (limited to 'system')
-rw-r--r-- | system/db/content.go | 5 | ||||
-rw-r--r-- | system/external/server.go | 48 |
2 files changed, 52 insertions, 1 deletions
diff --git a/system/db/content.go b/system/db/content.go index 9ab1f89..c50706d 100644 --- a/system/db/content.go +++ b/system/db/content.go @@ -107,7 +107,10 @@ func insert(ns string, data url.Values) (int, error) { return 0, err } - go SortContent(ns) + // since sorting can be expensive, limit sort to non-externally created posts + if !strings.Contains(ns, "_external") { + go SortContent(ns) + } return effectedID, nil } diff --git a/system/external/server.go b/system/external/server.go new file mode 100644 index 0000000..1efb23f --- /dev/null +++ b/system/external/server.go @@ -0,0 +1,48 @@ +package external + +import ( + "net/http" + + "github.com/bosssauce/ponzu/content" + "github.com/bosssauce/ponzu/system/db" +) + +// Externalable accepts or rejects external POST requests to /external/posts?type=Review +type Externalable interface { + Accept() bool +} + +func init() { + http.HandleFunc("/api/external/posts", externalPostsHandler) +} + +func externalPostsHandler(res http.ResponseWriter, req *http.Request) { + if req.Method != http.MethodPost { + res.WriteHeader(http.StatusMethodNotAllowed) + return + } + + t := req.URL.Query().Get("type") + if t == "" { + res.WriteHeader(http.StatusBadRequest) + return + } + + p, found := content.Types[t] + if !found { + res.WriteHeader(http.StatusNotFound) + return + } + + post := p() + + ext, ok := post.(Externalable) + if !ok { + res.WriteHeader(http.StatusNotFound) + return + } + + if ext.Accept() { + db.SetContent(t+"_external"+":-1", req.Form) + } +} |