summaryrefslogtreecommitdiff
path: root/system/api/external.go
blob: b34da936aa1b4bbf0da2c90df879273a3b69ca7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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) {
	log.Println("External request")
	if req.Method != http.MethodPost {
		res.WriteHeader(http.StatusMethodNotAllowed)
		return
	}

	t := req.URL.Query().Get("type")
	if t == "" {
		res.WriteHeader(http.StatusBadRequest)
		return
	}

	log.Println("type:", t)
	log.Println("of:", content.Types)
	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() {
		_, err := db.SetContent(t+"_external"+":-1", req.Form)
		if err != nil {
			log.Println("[External]", err)
			res.WriteHeader(http.StatusInternalServerError)
			return
		}
	}
}