-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
executable file
·223 lines (207 loc) · 6.58 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
//Редактирование, добавление, удаление и просмотр заметок
//происходит при помощи создания тестового файла название которого является название заметки
//так же как и название файла с изображением является название заметки
//РАЗМЕТКА
//Для поддержки адаптивности используются единицы измерения как hv,hw и %, большего не требуется т к вёрстка относительно проста
//
///usr/bin/true; exec /usr/bin/env go run "$0" "$@"
package main
import (
"fmt"
"html/template"
"io/fs"
"io/ioutil"
"log"
"math/rand"
"net/http"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
)
type Page struct {
Title string
Body []byte
ImageName string
}
type File struct {
NameFile []string
BegimFileName []string
}
type Name struct {
FileN File
}
func (p *Page) save() error { //запись и сохранение файла
filename := p.Title + ".txt"
return ioutil.WriteFile(filename, p.Body, 0600)
}
func (p *Page) delete() error { //удаление файла
filename := p.Title + ".txt"
return os.Remove(filename)
}
func getFileName() *Name { //
files, err := ioutil.ReadDir(".")
if err != nil {
log.Fatal(err)
}
fileN := File{NameFile: getTxt(files), BegimFileName: getBegin(files)}
return &Name{FileN: fileN}
}
func getTxt(fileInf []fs.FileInfo) []string { //
var fileTxt []string
for _, file := range fileInf {
if strings.HasSuffix(file.Name(), "txt") {
fileTxt = append(fileTxt, file.Name())
}
}
return fileTxt
}
func getBegin(fileInf []fs.FileInfo) []string {
var beginFile []string
for _, file := range fileInf {
if strings.HasSuffix(file.Name(), "txt") {
beginFile = append(beginFile, strings.Trim(file.Name(), ".txt"))
}
}
return beginFile
}
func initImage(_nameFile string, title string) { //добавление изображения
var path, er = os.Getwd() //получаем путь где расположен main.go
if er != nil {
fmt.Println("Absolute:", path)
}
var nameFile string
var err error
nameFile, err = filepath.Abs(_nameFile) //получаем путь изображениия
if err != nil {
fmt.Println("Absolute:", nameFile)
}
if checkPath(_nameFile, title) && !strings.Contains(nameFile, "/static/images/") { //содержит ли static/images файлы равные текщему названию изображения либо заметки
nameFile = path + "/static/images/" + _nameFile
}
var end = filepath.Ext(nameFile)
var s = path + "/static/images/" + title + end
os.Rename(nameFile, s)
}
func checkPath(_path string, title string) bool {
files, err := ioutil.ReadDir("./static/images")
if err != nil {
log.Fatal(err)
}
var end = filepath.Ext(_path)
var path, er = os.Getwd()
if er != nil {
fmt.Println("Absolute:", path)
}
for _, file := range files {
if strings.TrimSuffix(file.Name(), end) == title || file.Name() == _path {
var endTitle = filepath.Ext(_path)
err = os.Rename(path+"/static/images/"+title+endTitle, path+"/static/images/"+strconv.Itoa(rand.Int())+end)
if err != nil {
return false
} //если у редактируемой заметки уже было изображение то его название смениться на набор случайных чисел
return true
}
}
return false
}
func loadPage(title string) (*Page, error) {
filename := title + ".txt"
files, err := ioutil.ReadDir("./static/images")
var im string
if err != nil {
log.Fatal(err)
}
for _, file := range files {
if file.Name()[:strings.IndexByte(file.Name(), '.')] == title {
im = file.Name()
}
}
body, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
return &Page{Title: title, Body: body, ImageName: im}, nil
}
func viewHandler(w http.ResponseWriter, r *http.Request, title string) {
p, err := loadPage(title)
if err != nil {
http.Redirect(w, r, "/edit/"+title, http.StatusFound)
return
}
renderTemplate(w, "view", p)
}
func editHandler(w http.ResponseWriter, _ *http.Request, title string) {
p, err := loadPage(title)
if err != nil {
p = &Page{Title: title}
}
renderTemplate(w, "edit", p)
}
func saveHandler(w http.ResponseWriter, r *http.Request, title string) {
body := r.FormValue("body")
image := r.FormValue("file")
if len(image) > 0 {
initImage(image, title)
}
p := &Page{Title: title, Body: []byte(body), ImageName: image}
err := p.save()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
http.Redirect(w, r, "/view/"+title, http.StatusFound)
}
func deleteHandler(w http.ResponseWriter, r *http.Request, title string) {
body := r.FormValue("body")
p := &Page{Title: title, Body: []byte(body)}
err := p.delete()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
http.Redirect(w, r, "/", http.StatusFound)
}
func createHandler(w http.ResponseWriter, r *http.Request, title string) {
title = r.FormValue("name")
http.Redirect(w, r, "/edit/"+title, http.StatusFound)
}
func indexHandler(w http.ResponseWriter, _ *http.Request) {
p := getFileName()
renderIndex(w, "index", p)
}
var templates = template.Must(template.ParseFiles("edit.html", "view.html", "index.html"))
func renderTemplate(w http.ResponseWriter, tmpl string, p *Page) {
err := templates.ExecuteTemplate(w, tmpl+".html", p)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func renderIndex(w http.ResponseWriter, tmpl string, n *Name) {
err := templates.ExecuteTemplate(w, tmpl+".html", n)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
var validPath = regexp.MustCompile("^/(edit|save|view|delete|create|style|static)/([a-zA-Z0-9]+)$")
func makeHandler(fn func(http.ResponseWriter, *http.Request, string)) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
m := validPath.FindStringSubmatch(r.URL.Path)
if m == nil {
http.NotFound(w, r)
return
}
fn(w, r, m[2])
}
}
func main() {
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
http.HandleFunc("/view/", makeHandler(viewHandler))
http.HandleFunc("/edit/", makeHandler(editHandler))
http.HandleFunc("/save/", makeHandler(saveHandler))
http.HandleFunc("/delete/", makeHandler(deleteHandler))
http.HandleFunc("/create/", makeHandler(createHandler))
http.HandleFunc("/", indexHandler)
log.Fatal(http.ListenAndServe(":8098", nil))
}