From 3791fadda7b761ffba38c567da29e2e71acd1dfb Mon Sep 17 00:00:00 2001 From: Steve Date: Mon, 19 Dec 2016 11:19:53 -0800 Subject: [addons] Creating foundation for plugin-like system "Addons" (#24) * adding addons dir and sample addon which enables the use of a new input element in forms for referencing other content. "addons" is a conceptual plugin-like feature, similar to wordpress "plugins" dir, but not as sophisticated --- system/addon/api.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 system/addon/api.go (limited to 'system/addon/api.go') diff --git a/system/addon/api.go b/system/addon/api.go new file mode 100644 index 0000000..4c3152b --- /dev/null +++ b/system/addon/api.go @@ -0,0 +1,47 @@ +// Package addon provides an API for Ponzu addons to interface with the system +package addon + +import ( + "bytes" + "fmt" + "io/ioutil" + "log" + "net/http" + "time" + + "github.com/bosssauce/ponzu/system/db" +) + +// ContentAll retrives all items from the HTTP API within the provided namespace +func ContentAll(namespace string) []byte { + host := db.ConfigCache("domain") + port := db.ConfigCache("http_port") + endpoint := "http://%s:%s/api/contents?type=%s" + buf := []byte{} + r := bytes.NewReader(buf) + url := fmt.Sprintf(endpoint, host, port, namespace) + + req, err := http.NewRequest(http.MethodGet, url, r) + if err != nil { + log.Println("Error creating request for reference of:", namespace) + return nil + } + + c := http.Client{ + Timeout: time.Duration(time.Second * 5), + } + res, err := c.Do(req) + if err != nil { + log.Println("Error making HTTP request for reference of:", namespace) + return nil + } + defer res.Body.Close() + + j, err := ioutil.ReadAll(res.Body) + if err != nil { + log.Println("Error reading request body for reference of:", namespace) + return nil + } + + return j +} -- cgit v1.2.3