forked from InkProject/ink
/
api.go
156 lines (143 loc) · 3.83 KB
/
api.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package main
import (
"crypto/md5"
"encoding/hex"
"encoding/json"
"github.com/InkProject/ink.go"
"github.com/facebookgo/symwalk"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"
)
type NewArticle struct {
Name string
Content string
}
type OldArticle struct {
Content string
}
var articleCache map[string]interface{}
func replyJSON(ctx *ink.Context, status int, data interface{}) {
jsonStr, err := json.Marshal(data)
if err != nil {
http.Error(ctx.Res, err.Error(), http.StatusInternalServerError)
ctx.Stop()
return
}
if status == http.StatusOK {
ctx.Header().Set("Content-Type", "application/json")
ctx.Header().Set("Access-Control-Allow-Origin", "*")
ctx.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
ctx.Res.Write(jsonStr)
} else {
Warn(data)
http.Error(ctx.Res, data.(string), status)
}
ctx.Stop()
}
func UpdateArticleCache() {
articleCache = make(map[string]interface{}, 0)
symwalk.Walk(sourcePath, func(path string, info os.FileInfo, err error) error {
fileExt := strings.ToLower(filepath.Ext(path))
if fileExt == ".md" {
fileName := strings.TrimSuffix(strings.ToLower(filepath.Base(path)), ".md")
config, _ := ParseArticleConfig(path)
md5Hex := md5.Sum([]byte(path))
id := hex.EncodeToString(md5Hex[:])
articleCache[string(id)] = map[string]interface{}{
"name": fileName,
"path": path,
"article": config,
}
}
return nil
})
}
func ApiListArticle(ctx *ink.Context) {
UpdateArticleCache()
replyJSON(ctx, http.StatusOK, articleCache)
}
func ApiGetArticle(ctx *ink.Context) {
UpdateArticleCache()
article, ok := articleCache[ctx.Param["id"]]
if !ok {
replyJSON(ctx, http.StatusNotFound, "Not Found")
return
}
filePath := article.(map[string]interface{})["path"].(string)
data, err := ioutil.ReadFile(filePath)
if err != nil {
replyJSON(ctx, http.StatusInternalServerError, err.Error())
return
}
replyJSON(ctx, http.StatusOK, string(data))
}
func ApiRemoveArticle(ctx *ink.Context) {
UpdateArticleCache()
article, ok := articleCache[ctx.Param["id"]]
if !ok {
replyJSON(ctx, http.StatusNotFound, "Not Found")
return
}
filePath := article.(map[string]interface{})["path"].(string)
err := os.Remove(filePath)
if err != nil {
replyJSON(ctx, http.StatusInternalServerError, err.Error())
return
}
replyJSON(ctx, http.StatusOK, nil)
}
func ApiCreateArticle(ctx *ink.Context) {
decoder := json.NewDecoder(ctx.Req.Body)
var article NewArticle
err := decoder.Decode(&article)
if err != nil {
replyJSON(ctx, http.StatusBadRequest, err.Error())
return
}
filePath := filepath.Join(sourcePath, article.Name+".md")
err = ioutil.WriteFile(filePath, []byte(article.Content), 0644)
if err != nil {
replyJSON(ctx, http.StatusInternalServerError, err.Error())
return
}
replyJSON(ctx, http.StatusOK, nil)
}
func ApiSaveArticle(ctx *ink.Context) {
UpdateArticleCache()
decoder := json.NewDecoder(ctx.Req.Body)
var article OldArticle
err := decoder.Decode(&article)
if err != nil {
replyJSON(ctx, http.StatusBadRequest, err.Error())
return
}
cacheArticle, ok := articleCache[ctx.Param["id"]]
if !ok {
replyJSON(ctx, http.StatusNotFound, "Not Found")
return
}
// Write
path := cacheArticle.(map[string]interface{})["path"].(string)
err = ioutil.WriteFile(path, []byte(article.Content), 0644)
if err != nil {
replyJSON(ctx, http.StatusInternalServerError, err.Error())
return
}
replyJSON(ctx, http.StatusOK, nil)
}
// Rename
// cacheArticle, ok := articleCache[ctx.Param["id"]]
// if !ok {
// replyJSON(ctx, http.StatusNotFound, "Not Found")
// return
// }
// oldPath := cacheArticle.(map[string]interface{})["path"].(string)
// newPath := filepath.Join(sourcePath, newArticle.Name+".md")
// err = os.Rename(oldPath, newPath)
// if err != nil {
// replyJSON(ctx, http.StatusInternalServerError, err.Error())
// return
// }