diff options
author | Steve Manuel <nilslice@gmail.com> | 2016-09-24 02:06:54 -0700 |
---|---|---|
committer | Steve Manuel <nilslice@gmail.com> | 2016-09-24 02:06:54 -0700 |
commit | afe2ad4d1ead83b6d437fef57cc0feecaa5ac0ce (patch) | |
tree | 606e6e73fba227988b826503e66eddd333d9474a | |
parent | f2039ffbfb5a978ddcf671a099eb125bfd180fd4 (diff) |
creating JSON API server, initial version working
-rw-r--r-- | cmd/cms/main.go | 4 | ||||
-rw-r--r-- | system/admin/admin.go | 3 | ||||
-rw-r--r-- | system/admin/server.go (renamed from cmd/cms/server.go) | 14 | ||||
-rw-r--r-- | system/api/server.go | 135 |
4 files changed, 145 insertions, 11 deletions
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/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/cmd/cms/server.go b/system/admin/server.go index d3037d2..730561a 100644 --- a/cmd/cms/server.go +++ b/system/admin/server.go @@ -1,4 +1,4 @@ -package main +package admin import ( "bytes" @@ -9,13 +9,12 @@ import ( "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) + adminView := Admin(nil) res.Header().Set("Content-Type", "text/html") res.Write(adminView) @@ -43,7 +42,7 @@ func init() { } html = html + b.String() - adminView := admin.Admin([]byte(html)) + adminView := Admin([]byte(html)) res.Header().Set("Content-Type", "text/html") res.Write(adminView) @@ -81,7 +80,7 @@ func init() { } m, err := manager.Manage(post.(editor.Editable), t) - adminView := admin.Admin(m) + adminView := Admin(m) if err != nil { fmt.Println(err) res.WriteHeader(http.StatusInternalServerError) @@ -117,6 +116,7 @@ func init() { }) } -func serve() { - http.ListenAndServe(":8080", nil) +// Run starts the Admin system on the port provided +func Run(port string) { + http.ListenAndServe(":"+port, nil) } diff --git a/system/api/server.go b/system/api/server.go new file mode 100644 index 0000000..5fa0bfc --- /dev/null +++ b/system/api/server.go @@ -0,0 +1,135 @@ +package api + +import ( + "bytes" + "encoding/json" + "log" + "net/http" + + "github.com/nilslice/cms/content" + "github.com/nilslice/cms/system/db" +) + +func init() { + http.HandleFunc("/api/types", func(res http.ResponseWriter, req *http.Request) { + var types = []string{} + for t := range content.Types { + types = append(types, string(t)) + } + + j, err := toJSON(types) + if err != nil { + res.WriteHeader(http.StatusInternalServerError) + return + } + + res.Header().Set("Content-Type", "application/json") + res.Write(j) + }) + + http.HandleFunc("/api/posts", func(res http.ResponseWriter, req *http.Request) { + q := req.URL.Query() + t := q.Get("type") + // TODO: implement pagination + // num := q.Get("num") + // page := q.Get("page") + + if t == "" { + res.WriteHeader(http.StatusBadRequest) + return + } + + posts := db.GetAll(t) + var all = []json.RawMessage{} + for _, post := range posts { + all = append(all, post) + } + + j, err := fmtJSON(all...) + if err != nil { + res.WriteHeader(http.StatusInternalServerError) + return + } + + res.Header().Set("Content-Type", "application/json") + res.Write(j) + }) + + http.HandleFunc("/api/post", func(res http.ResponseWriter, req *http.Request) { + q := req.URL.Query() + id := q.Get("id") + t := q.Get("type") + + if t == "" || id == "" { + res.WriteHeader(http.StatusBadRequest) + return + } + + post, err := db.Get(t + ":" + id) + if err != nil { + res.WriteHeader(http.StatusInternalServerError) + return + } + + j, err := fmtJSON(json.RawMessage(post)) + if err != nil { + res.WriteHeader(http.StatusInternalServerError) + return + } + + res.Header().Set("Content-Type", "application/json") + res.Write(j) + }) + +} + +func fmtJSON(data ...json.RawMessage) ([]byte, error) { + var msg = []json.RawMessage{} + for _, d := range data { + msg = append(msg, d) + } + + resp := map[string][]json.RawMessage{ + "data": msg, + } + + var buf = &bytes.Buffer{} + enc := json.NewEncoder(buf) + err := enc.Encode(resp) + if err != nil { + log.Println("Failed to encode data to JSON:", err) + return nil, err + } + + return buf.Bytes(), nil +} + +func toJSON(data []string) ([]byte, error) { + var buf = &bytes.Buffer{} + enc := json.NewEncoder(buf) + resp := map[string][]string{ + "data": data, + } + + err := enc.Encode(resp) + if err != nil { + log.Println("Failed to encode data to JSON:", err) + return nil, err + } + + return buf.Bytes(), nil +} + +func wrapJSON(json []byte) []byte { + var buf = &bytes.Buffer{} + buf.Write([]byte("{data:")) + buf.Write(json) + buf.Write([]byte("}")) + + return buf.Bytes() +} + +// Run start the JSON API +func Run(port string) { + log.Fatal(http.ListenAndServe(":"+port, nil)) +} |