/
controller.go
72 lines (58 loc) · 1.22 KB
/
controller.go
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
package main
import (
"encoding/json"
"net/http"
"path/filepath"
"github.com/labstack/echo"
"github.com/schollz/jsonstore"
)
const (
storeFile = "mk.json.gz"
)
type markdown struct {
String string `json:"md"`
}
func getRoot(c echo.Context) error {
html, err := Asset("data/index.html")
if err != nil {
return err
}
return c.HTML(http.StatusOK, string(html))
}
func storeJSON(mk markdown) (err error) {
ks := new(jsonstore.JSONStore)
err = ks.Set("mk:1", mk)
if err != nil {
return
}
err = jsonstore.Save(ks, filepath.Join(SavePath, storeFile))
return
}
func getMarkdown(c echo.Context) (err error) {
var ks2 *jsonstore.JSONStore
ks2, err = jsonstore.Open(filepath.Join(SavePath, storeFile))
if err != nil {
return
}
var mk markdown
err = ks2.Get("mk:1", &mk)
if err != nil {
return
}
mkRaw, err := json.Marshal(mk)
if err != nil {
return
}
return c.JSON(http.StatusOK, string(mkRaw))
}
func postMarkdown(c echo.Context) error {
req := c.Request()
dec := json.NewDecoder(req.Body)
var m markdown
dec.Decode(&m)
err := storeJSON(m)
if err != nil {
return err
}
return c.JSON(http.StatusOK, `{"message":"success"}`)
}