summaryrefslogtreecommitdiff
path: root/system/external/server.go
blob: 8f40ad19f72bd66fb15807bc9a0f2169ce3eb2ab (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
package external

import (
	"fmt"
	"net/http"

	"github.com/bosssauce/ponzu/content"
	"github.com/bosssauce/ponzu/system/api"
	"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", api.CORS(externalPostsHandler))
}

func externalPostsHandler(res http.ResponseWriter, req *http.Request) {
	fmt.Println("got request")
	res.Header().Set("X-TEST", "Request ack")
	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 {
		fmt.Println(t, content.Types, p)
		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)
	}
}