diff options
author | Steve Manuel <nilslice@gmail.com> | 2016-09-19 02:09:29 -0700 |
---|---|---|
committer | Steve Manuel <nilslice@gmail.com> | 2016-09-19 02:09:29 -0700 |
commit | 2ed153f8d287b3ffb5e8d1667ab51c922d82c504 (patch) | |
tree | 8bde4e8865e5502ff72487252bcd5fcc09cd89c2 /server.go | |
parent | d62f31d1932125db59c4cf813c54d95a4a0200ee (diff) |
reorganizing files and dir structure. adding initial (incomplete) management, types, system and db functionality.
Diffstat (limited to 'server.go')
-rw-r--r-- | server.go | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/server.go b/server.go new file mode 100644 index 0000000..d7605f6 --- /dev/null +++ b/server.go @@ -0,0 +1,78 @@ +package main + +import ( + "fmt" + "net/http" + + "github.com/nilslice/cms/content" + "github.com/nilslice/cms/management/manager" +) + +const ( + // ErrTypeNotRegistered means content type isn't registered (not found in content.Types map) + ErrTypeNotRegistered = `Error: +There is no type registered for %[1]s + +Add this to the file which defines %[1]s{} in the 'content' package: +--------------------------------+ + +func init() { + Types["%[1]s"] = %[1]s{} +} + +--------------------------------+ +` +) + +func main() { + // p := content.Post{ + // Title: []byte("Profound introduction"), + // Content: []byte("<h3>H</h3>ello. My name is <em>Steve</em>."), + // Author: []byte("Steve Manuel"), + // Timestamp: []byte("2016-09-16"), + // } + // p.ID = 1 + + http.HandleFunc("/admin/edit", func(res http.ResponseWriter, req *http.Request) { + switch req.Method { + case http.MethodGet: + err := req.ParseForm() + if err != nil { + res.WriteHeader(http.StatusBadRequest) + return + } + + t := req.FormValue("type") + contentType, ok := content.Types[t] + if !ok { + fmt.Fprintf(res, ErrTypeNotRegistered, t) + return + } + view, err := manager.Manage(contentType) + if err != nil { + res.WriteHeader(http.StatusInternalServerError) + return + } + res.Header().Set("Content-Type", "text/html") + res.Write(view) + + case http.MethodPost: + err := req.ParseForm() + if err != nil { + res.WriteHeader(http.StatusBadRequest) + return + } + + id := req.FormValue("contentId") + if id == "0" { + res.Write([]byte("This would create a new post")) + return + } + + res.Write([]byte("Updated post " + id)) + } + }) + + http.ListenAndServe(":8080", nil) + +} |