From afe2ad4d1ead83b6d437fef57cc0feecaa5ac0ce Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Sat, 24 Sep 2016 02:06:54 -0700 Subject: creating JSON API server, initial version working --- system/admin/admin.go | 3 -- system/admin/server.go | 122 ++++++++++++++++++++++++++++++++++++++++++++ system/api/server.go | 135 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 257 insertions(+), 3 deletions(-) create mode 100644 system/admin/server.go create mode 100644 system/api/server.go (limited to 'system') diff --git a/system/admin/admin.go b/system/admin/admin.go index 8febbf7..a4053ad 100644 --- a/system/admin/admin.go +++ b/system/admin/admin.go @@ -4,7 +4,6 @@ package admin import ( "bytes" - "fmt" "html/template" "github.com/nilslice/cms/content" @@ -55,8 +54,6 @@ func Admin(manager []byte) []byte { Subview: template.HTML(manager), } - fmt.Println(a.Types) - buf := &bytes.Buffer{} tmpl := template.Must(template.New("admin").Parse(adminHTML)) tmpl.Execute(buf, a) diff --git a/system/admin/server.go b/system/admin/server.go new file mode 100644 index 0000000..730561a --- /dev/null +++ b/system/admin/server.go @@ -0,0 +1,122 @@ +package admin + +import ( + "bytes" + "encoding/json" + "fmt" + "net/http" + + "github.com/nilslice/cms/content" + "github.com/nilslice/cms/management/editor" + "github.com/nilslice/cms/management/manager" + "github.com/nilslice/cms/system/db" +) + +func init() { + http.HandleFunc("/admin", func(res http.ResponseWriter, req *http.Request) { + adminView := Admin(nil) + + res.Header().Set("Content-Type", "text/html") + res.Write(adminView) + }) + + http.HandleFunc("/admin/posts", func(res http.ResponseWriter, req *http.Request) { + q := req.URL.Query() + t := q.Get("type") + if t == "" { + res.WriteHeader(http.StatusBadRequest) + } + + posts := db.GetAll(t) + b := &bytes.Buffer{} + p := content.Types[t]().(editor.Editable) + + html := `New ` + t + ` +