/
main.go
277 lines (256 loc) · 6.4 KB
/
main.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package main
import (
"encoding/json"
"fmt"
"html/template"
"net/http"
"net/rpc"
"os"
"path/filepath"
"strings"
"time"
"github.com/everfore/exc"
"github.com/everfore/rpcsv"
"github.com/toukii/goutils"
"github.com/toukii/jsnm"
)
func main() {
defer rpc_client.Close()
walkRPCRdr()
upload := &goutils.UploadHandler{}
mux := http.NewServeMux()
mux.Handle("/upload", upload)
mux.Handle("/upload/streamUpload", upload)
mux.HandleFunc("/callback", callback)
mux.HandleFunc("/update", update)
mux.HandleFunc("/TiNews", TiNews)
mux.HandleFunc("/TiNewsAPI", TiNewsAPI)
mux.Handle("/", http.StripPrefix("/", http.FileServer(http.Dir("./MDFs"))))
http.ListenAndServe(":80", mux)
}
var (
exc_cmd *exc.CMD
rpc_client *rpc.Client
tpl *template.Template
bs []byte
// rpc_tcp_server = "localhost:8800"
// rpc_tcp_server = "tcphub.t0.daoapp.io:61142"
// rpc_tcp_server = "rpchub.t0.daoapp.io:61142"
rpc_tcp_server = "rpchub.t1.daoapp.io:61160"
// rpc_tcp_server = "192.168.1.114:8800"
)
func init() {
var err error
exc_cmd = exc.NewCMD("ls").Debug()
rpc_client = rpcsv.RPCClient(rpc_tcp_server)
if rpc_client == nil {
panic("rpc_client is nil!")
}
tpl, err = template.ParseFiles("theme.thm")
if goutils.CheckErr(err) {
tpl = defaultTheme()
}
}
func connect() {
rpc_client = rpcsv.RPCClientWithCodec(rpc_tcp_server)
go func() {
time.Sleep(2e9)
rpc_client.Close()
}()
}
func defaultTheme() *template.Template {
dtpl, err := template.New("default").Parse("{{.MDContent}}")
if goutils.CheckErr(err) {
panic(err)
}
return dtpl
}
func TiNewsAPI(rw http.ResponseWriter, req *http.Request) {
type TiNews struct {
Main string `json:"main"`
AuthorName string `json:"author_name"`
}
news := struct {
Data []TiNews `json:"data"`
}{
Data: []TiNews{
TiNews{
Main: "发布平台运营专员兼软件开发时永宾已确定从银联商务离职。" + time.Now().Format(" 2006/1/2 15:04 "),
AuthorName: "Ti媒体记者/toukii",
},
},
}
bs, err := json.Marshal(news)
if goutils.CheckErr(err) {
fmt.Fprint(rw, err)
} else {
rw.Write(bs)
}
}
func TiNews(rw http.ResponseWriter, req *http.Request) {
out := make([]byte, 1)
i := 0
retry:
err := rpc_client.Call("RPC.TiNews", &i, &out)
if goutils.CheckErr(err) {
if strings.Contains(err.Error(), "connection") && i < 6 {
connect()
i++
goto retry
}
fmt.Fprint(rw, err)
return
}
rw.Write(out)
}
func update(rw http.ResponseWriter, req *http.Request) {
updateTheme()
}
func updateTheme() {
fmt.Println("update theme")
exc_cmd.Reset("rm -rf MDFs").Execute()
tpl1, err := template.ParseFiles("theme.thm")
if goutils.CheckErr(err) {
return
}
tpl = tpl1
walkRPCRdr()
}
// Webhooks callback
func callback(rw http.ResponseWriter, req *http.Request) {
// fmt.Printf("Refer:%s\n", req.Referer())
// fmt.Printf("req:%#v\n", req)
usa := req.UserAgent()
// fmt.Printf("UserAgent:%s\n", usa)
if !strings.Contains(usa, "GitHub-Hookshot/") && !strings.Contains(usa, "Coding.net Hook") {
fmt.Println("CSRF Attack!")
http.Redirect(rw, req, "/", 302)
return
}
/*// coding
if strings.Contains(usa, "Coding.net Hook") {
exc_cmd.Reset("git pull origin master:master").Execute()
rpcsv.UpdataTheme()
updateTheme()
return
}*/
// coding
hj := jsnm.ReaderFmt(req.Body)
ma := hj.ArrGet("commits", "0", "modified").Arr()
pull := false
if len(ma) > 0 {
exc_cmd.Reset("git pull origin master:master").Execute()
pull = true
}
for i, it := range ma {
fs := it.RawData().String()
fmt.Printf("modified-%d:%v\n", i, fs)
if strings.EqualFold(fs, "theme.thm") {
rpcsv.UpdataTheme()
updateTheme()
return
}
if strings.HasSuffix(fs, ".md") {
modifiedMD(fs, "./MDFs")
} else {
goutils.ReWriteFile(filepath.Join("./MDFs", fs), goutils.ReadFile(fs))
}
}
aa := hj.ArrGet("commits", "0", "added").Arr()
if aa != nil && !pull {
exc_cmd.Reset("git pull origin master:master").Execute()
}
for i, it := range aa {
fs := it.RawData().String()
fmt.Printf("added-%d:%v\n", i, fs)
if strings.HasSuffix(fs, ".md") {
modifiedMD(fs, "./MDFs")
} else {
goutils.ReWriteFile(filepath.Join("./MDFs", fs), goutils.ReadFile(fs))
}
}
ra := hj.ArrGet("commits", "0", "removed").Arr()
if ra != nil && !pull {
exc_cmd.Reset("git pull origin master:master").Execute()
}
for i, it := range ra {
fs := it.RawData().String()
fmt.Printf("removed-%d:%v\n", i, fs)
if strings.HasSuffix(fs, ".md") {
removeMD(fs, "./MDFs")
}
}
}
func removeMD(file_in, dir_out string) {
fs := strings.Split(file_in, ".")
goutils.DeleteFile(fmt.Sprintf("%s.html", filepath.Join(dir_out, fs[0])))
}
// in: Linux/index.md
// out: ./MDFs
func modifiedMD(file_in, dir_out string) {
finfo, err := os.Stat(file_in)
if goutils.CheckErr(err) {
return
}
filename := finfo.Name()
dir := filepath.Dir(file_in)
fs := strings.Split(filename, ".")
in := goutils.ReadFile(file_in)
out := make([]byte, 1)
i := 0
retry:
i++
err = rpc_client.Call("RPC.Markdown", &in, &out)
// err = rpcsv.Markdown(rpc_client, &in, &out)
if goutils.CheckErr(err) {
if i < 6 {
connect()
goto retry
}
return
}
target := fmt.Sprintf("%s.html", filepath.Join(dir_out, dir, fs[0]))
// goutils.Mkdir(fmt.Sprintf("%s", filepath.Join(dir_out, dir)))
outfile, erro := os.OpenFile(target, os.O_CREATE|os.O_WRONLY, 0666)
if goutils.CheckErr(erro) {
return
}
defer outfile.Close()
dt := make(map[string]interface{})
dt["MDContent"] = template.HTML(goutils.ToString(out))
erre := tpl.Execute(outfile, dt)
if !goutils.CheckErr(erre) {
fmt.Println(file_in, " ==> ", target)
}
}
func copyFile(file_in, dir_out string) {
goutils.WriteFile(filepath.Join(dir_out, file_in), goutils.ReadFile(file_in))
fmt.Printf("copy file:%s ==> %s\n", file_in, filepath.Join(dir_out, file_in))
}
// base: ./
// target: ./MDFs
func walkRPCRdr() {
filepath.Walk("./", walkCond)
}
var (
abs, _ = filepath.Abs("./MDFs")
)
func walkCond(path string, info os.FileInfo, err error) error {
if strings.EqualFold(info.Name(), ".git") || strings.Contains(info.Name(), "MDFs") {
return filepath.SkipDir
}
abspath := filepath.Join(abs, path)
if info.IsDir() {
goutils.Mkdir(abspath)
fmt.Printf("mkdir %s\n", abspath)
return nil
}
copyFile(path, abs)
/*goutils.ReWriteFile(abspath, goutils.ReadFile(path))
fmt.Printf("copy file:%s ==> %s\n", path, abspath)*/
if info.IsDir() || !strings.HasSuffix(info.Name(), ".md") {
return nil
}
modifiedMD(path, abs)
return nil
}