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 --- cmd/cms/main.go | 4 +- cmd/cms/server.go | 122 -------------------------------------------- system/admin/admin.go | 3 -- system/admin/server.go | 122 ++++++++++++++++++++++++++++++++++++++++++++ system/api/server.go | 135 +++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 260 insertions(+), 126 deletions(-) delete mode 100644 cmd/cms/server.go create mode 100644 system/admin/server.go create mode 100644 system/api/server.go diff --git a/cmd/cms/main.go b/cmd/cms/main.go index 0b451c5..123c65e 100644 --- a/cmd/cms/main.go +++ b/cmd/cms/main.go @@ -5,6 +5,7 @@ import ( "fmt" "os" + "github.com/nilslice/cms/system/api" "github.com/nilslice/cms/system/db" ) @@ -97,7 +98,8 @@ func main() { } case "serve", "s": db.Init() - serve() + // admin.Run("8080") + api.Run("8000") case "": flag.PrintDefaults() diff --git a/cmd/cms/server.go b/cmd/cms/server.go deleted file mode 100644 index d3037d2..0000000 --- a/cmd/cms/server.go +++ /dev/null @@ -1,122 +0,0 @@ -package main - -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/admin" - "github.com/nilslice/cms/system/db" -) - -func init() { - http.HandleFunc("/admin", func(res http.ResponseWriter, req *http.Request) { - adminView := admin.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 + ` -