-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
168 lines (148 loc) · 3.34 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
package main
import (
"github.com/gin-gonic/gin"
"net/http"
"os"
"fmt"
"flag"
"io/ioutil"
"regexp"
"strings"
"path"
)
var (
RootPath string
)
func main() {
goPath := os.Getenv("GOPATH")
if goPath == "" {
goPath = "/test"
}
RootPath = fmt.Sprintf("%s/src/github.com/illidan33/markdown_text", goPath)
port := flag.Int("port", 8001, "listen port")
flag.Parse()
gin.SetMode(gin.ReleaseMode)
router := gin.New()
router.LoadHTMLGlob(RootPath + "/html/*")
router.Static("/js", RootPath+"/js")
router.Static("/css", RootPath+"/css")
router.Static("/files", RootPath+"/create_files")
router.GET("/", IndexRouter)
router.GET("/Detail/:name", DetailRouter)
router.GET("/New", NewRouter)
router.POST("/save", SaveRouter)
router.Run(fmt.Sprintf(":%d", *port))
}
func IndexRouter(c *gin.Context) {
fileNames := []string{}
files, _ := ioutil.ReadDir(fmt.Sprintf("%s/create_files", RootPath))
for _, file := range files {
if file.IsDir() {
continue
} else {
fileNames = append(fileNames, strings.TrimSuffix(file.Name(), path.Ext(file.Name())))
}
}
c.HTML(http.StatusOK, "index.html", gin.H{
"files": fileNames,
})
}
func NewRouter(c *gin.Context) {
c.HTML(http.StatusOK, "detail.html", gin.H{
"content": "",
"name": "",
})
}
func DetailRouter(c *gin.Context) {
content := []byte("")
name := c.Param("name")
if name != "" {
filePath := fmt.Sprintf("%s/create_files/%s.tpl", RootPath, name)
if IsExists(filePath) {
handle, err := os.Open(filePath)
if err != nil {
c.HTML(http.StatusInternalServerError, "500", gin.H{})
return
}
defer handle.Close()
content, _ = ioutil.ReadAll(handle)
}
}
c.HTML(http.StatusOK, "detail.html", gin.H{
"content": string(content),
"name": name,
})
}
func SaveRouter(c *gin.Context) {
var err error
var newPath string
c.Request.ParseForm()
content := c.PostForm("content")
name := c.PostForm("name")
oldName := c.PostForm("old_name")
// 校验名称
if name == "" {
c.JSON(http.StatusOK, gin.H{
"msg": "name empty",
})
return
} else {
reg := regexp.MustCompile("^[\\-_0-9a-zA-Z\u4E00-\u9FA5]+$")
if ok := reg.MatchString(name); !ok {
c.JSON(http.StatusOK, gin.H{
"msg": "名称只能为中文、数字、字母、-、_,不能含有特殊字符!",
})
return
}
}
// 如果改名,不能覆盖同名文件
if name != oldName {
newPath = fmt.Sprintf("/Detail/%s", name)
files, err := ioutil.ReadDir(fmt.Sprintf("%s/create_files/", RootPath))
if err != nil {
c.JSON(http.StatusOK, gin.H{
"msg": err.Error(),
})
return
}
nameStr := fmt.Sprintf("%s.tpl", name)
for _, file := range files {
if file.Name() == nameStr {
c.JSON(http.StatusOK, gin.H{
"msg": "已有同名文件",
})
return
}
}
}
filePath := fmt.Sprintf("%s/create_files/%s.tpl", RootPath, name)
var handle *os.File
if IsExists(filePath) {
os.Remove(filePath)
}
handle, err = os.Create(filePath)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"msg": err.Error(),
})
return
}
defer handle.Close()
handle.Write([]byte(content))
if newPath != "" {
os.Remove(fmt.Sprintf("%s/create_files/%s.tpl", RootPath, oldName))
}
c.JSON(http.StatusOK, gin.H{
"path": newPath,
})
}
func IsExists(path string) bool {
_, err := os.Stat(path)
if err != nil {
if os.IsExist(err) {
return true
}
return false
}
return true
}