diff options
Diffstat (limited to 'system/api')
-rw-r--r-- | system/api/external.go | 46 | ||||
-rw-r--r-- | system/api/server.go | 2 |
2 files changed, 48 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) + } +} diff --git a/system/api/server.go b/system/api/server.go index da73382..816bc21 100644 --- a/system/api/server.go +++ b/system/api/server.go @@ -9,4 +9,6 @@ func Run() { http.HandleFunc("/api/posts", CORS(postsHandler)) http.HandleFunc("/api/post", CORS(postHandler)) + + http.HandleFunc("/api/external/posts", CORS(externalPostsHandler)) } |