-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtinycms.go
More file actions
57 lines (50 loc) · 1.57 KB
/
tinycms.go
File metadata and controls
57 lines (50 loc) · 1.57 KB
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
// Tinycms broadcasts view and edit functions for html
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"github.com/gorilla/mux"
)
var db map[string]string
// add some user auth?
// add some csrf auth?
// make some html templates?
func main() {
db = make(map[string]string)
r := mux.NewRouter()
r.HandleFunc("/index.html", servePage("index.html"))
r.HandleFunc("/tinycms.js", servePage("tinycms.js"))
r.PathPrefix("/view/").HandlerFunc(view)
r.PathPrefix("/edit/").HandlerFunc(edit)
http.Handle("/", r)
log.Fatal(http.ListenAndServe("localhost:8000", nil))
}
func servePage(filename string) func(w http.ResponseWriter, r *http.Request) {
data, err := ioutil.ReadFile(filename)
if err != nil {
log.Fatalf("servePage: %v\n", err)
}
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
_, _ = w.Write(data)
}
}
func view(w http.ResponseWriter, r *http.Request) {
k := r.RequestURI[6:]
v := db[k]
fmt.Printf("Getting db[%#v]=%#v\n", k, v)
w.Header().Set("Content-Type", "text/html")
// http://stackoverflow.com/questions/49547/making-sure-a-web-page-is-not-cached-across-all-browsers
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate") // HTTP 1.1.
w.Header().Set("Pragma", "no-cache") // HTTP 1.0.
w.Header().Set("Expires", "0") // Proxies.
_, _ = w.Write([]byte(v))
}
func edit(w http.ResponseWriter, r *http.Request) {
k := r.RequestURI[6:]
v := r.FormValue("data")
fmt.Printf("writing db[%#v]=%#v\n", k, v)
db[k] = v
}