diff options
author | Steve Manuel <nilslice@gmail.com> | 2016-10-26 12:30:48 -0700 |
---|---|---|
committer | Steve Manuel <nilslice@gmail.com> | 2016-10-26 12:30:48 -0700 |
commit | 699a1a5076d683d0e59b2edbe9e05b6886c0bc88 (patch) | |
tree | b23bea9b36163fd57b5840ed6d5a11eaed82bb2d /system/api/external.go | |
parent | fcea30925bfadb43d0b5895e8aa2ec22bec14b35 (diff) |
debugging and code reorg
Diffstat (limited to 'system/api/external.go')
-rw-r--r-- | system/api/external.go | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/system/api/external.go b/system/api/external.go new file mode 100644 index 0000000..19dc1d0 --- /dev/null +++ b/system/api/external.go @@ -0,0 +1,46 @@ +package api + +import ( + "log" + "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 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 { + log.Println("Attempt to submit content", t, "by", req.RemoteAddr) + res.WriteHeader(http.StatusNotFound) + return + } + + post := p() + + ext, ok := post.(Externalable) + if !ok { + res.WriteHeader(http.StatusInternalServerError) + return + } + + if ext.Accept() { + db.SetContent(t+"_external"+":-1", req.Form) + } +} |