summaryrefslogtreecommitdiff
path: root/server.go
blob: d7605f6cccff812cdd645d6fb31fb279fc663a3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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)

}