|
|
@@ -23,15 +23,11 @@ func Notes(w http.ResponseWriter, r *http.Request, fisier string) {
|
|
|
notita := types.Notita{}
|
|
|
err := inout.FileToObj(fisier, ¬ita)
|
|
|
if err != nil {
|
|
|
- w.WriteHeader(http.StatusInternalServerError)
|
|
|
- w.Write([]byte(WEB_ERR))
|
|
|
- template.Must(template.ParseFiles("templates/error.tmpl")).Execute(w, err)
|
|
|
+ WriteError(w, err)
|
|
|
return
|
|
|
}
|
|
|
if err := goldmark.Convert([]byte(notita.Continut), &buf); err != nil {
|
|
|
- w.WriteHeader(http.StatusInternalServerError)
|
|
|
- w.Write([]byte(WEB_ERR))
|
|
|
- template.Must(template.ParseFiles("templates/error.tmpl")).Execute(w, err)
|
|
|
+ WriteError(w, err)
|
|
|
return
|
|
|
}
|
|
|
notita.HTML = buf.String()
|
|
|
@@ -46,10 +42,7 @@ func ListNotes(w http.ResponseWriter, r *http.Request, s *Server) {
|
|
|
f := inout.Folder{}
|
|
|
err := inout.GetFolderStructure(s.NotesDir, &f)
|
|
|
if err != nil {
|
|
|
- resp, code := inout.ParseTemplate("templates/error.tmpl", err)
|
|
|
- w.Header().Set("Content-Type", "text/html")
|
|
|
- w.WriteHeader(code)
|
|
|
- w.Write(resp)
|
|
|
+ WriteError(w, err)
|
|
|
return
|
|
|
}
|
|
|
resp, code := inout.ParseTemplate("templates/list_notes.tmpl", f)
|
|
|
@@ -68,18 +61,15 @@ func EditNotes(w http.ResponseWriter, r *http.Request, filename string) {
|
|
|
f := types.Notita{}
|
|
|
err := inout.FileToObj(filename, &f)
|
|
|
if err != nil {
|
|
|
- resp, code := inout.ParseTemplate("templates/error.tmpl", err)
|
|
|
- w.WriteHeader(code)
|
|
|
- w.Write(resp)
|
|
|
+ WriteError(w, err)
|
|
|
+ return
|
|
|
}
|
|
|
resp, code := inout.ParseTemplate("templates/edit_notes.tmpl", f)
|
|
|
w.WriteHeader(code)
|
|
|
w.Write(resp)
|
|
|
return
|
|
|
}
|
|
|
- resp, code := inout.ParseTemplate("templates/error.tmpl", fmt.Errorf("method %s not allowed", r.Method))
|
|
|
- w.WriteHeader(code)
|
|
|
- w.Write(resp)
|
|
|
+ WriteError(w, fmt.Errorf("method %s not allowed", r.Method))
|
|
|
}
|
|
|
|
|
|
func MDToHTML(w http.ResponseWriter, r *http.Request) {
|
|
|
@@ -106,7 +96,8 @@ func MDToHTML(w http.ResponseWriter, r *http.Request) {
|
|
|
}
|
|
|
|
|
|
func API(w http.ResponseWriter, r *http.Request, s *Server) {
|
|
|
- action := strings.TrimPrefix(r.URL.Path, "/api/")
|
|
|
+ action := strings.Trim(strings.TrimPrefix(r.URL.Path, "/api/"), "/")
|
|
|
+ fmt.Println(action)
|
|
|
if r.Method == "POST" {
|
|
|
//w.WriteHeader(http.StatusNotImplemented)
|
|
|
//w.Write(fmt.Appendf(nil, "action not implemented: %s", action))
|
|
|
@@ -122,20 +113,14 @@ func API(w http.ResponseWriter, r *http.Request, s *Server) {
|
|
|
}
|
|
|
err := inout.SafeDir(n.Folder)
|
|
|
if err != nil {
|
|
|
- resp, code := inout.ParseTemplate("templates/error.tmpl", err)
|
|
|
- w.Header().Set("Content-Type", "text/html")
|
|
|
- w.WriteHeader(code)
|
|
|
- w.Write(resp)
|
|
|
+ WriteError(w, err)
|
|
|
return
|
|
|
}
|
|
|
path := filepath.Join(s.NotesDir, n.Folder)
|
|
|
err = os.MkdirAll(path, 0755)
|
|
|
fmt.Println(n.Folder)
|
|
|
if err != nil {
|
|
|
- resp, code := inout.ParseTemplate("templates/error.tmpl", err)
|
|
|
- w.Header().Set("Content-Type", "text/html")
|
|
|
- w.WriteHeader(code)
|
|
|
- w.Write(resp)
|
|
|
+ WriteError(w, err)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
@@ -146,10 +131,7 @@ func API(w http.ResponseWriter, r *http.Request, s *Server) {
|
|
|
|
|
|
err = inout.ObjToFile(filepath.Join(s.NotesDir, n.Folder, fisier), n, true)
|
|
|
if err != nil {
|
|
|
- resp, code := inout.ParseTemplate("templates/error.tmpl", err)
|
|
|
- w.Header().Set("Content-Type", "text/html")
|
|
|
- w.WriteHeader(code)
|
|
|
- w.Write(resp)
|
|
|
+ WriteError(w, err)
|
|
|
return
|
|
|
}
|
|
|
referinta := r.Referer()
|
|
|
@@ -169,8 +151,22 @@ func API(w http.ResponseWriter, r *http.Request, s *Server) {
|
|
|
fmt.Println(n.Path)
|
|
|
err := inout.ObjToFile(n.Path, n, true)
|
|
|
if err != nil {
|
|
|
- w.WriteHeader(http.StatusInternalServerError)
|
|
|
- fmt.Fprintf(w, "%+v", err)
|
|
|
+ WriteError(w, err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ referinta := r.Referer()
|
|
|
+ if referinta != "" {
|
|
|
+ http.Redirect(w, r, referinta, http.StatusSeeOther)
|
|
|
+ } else {
|
|
|
+ http.Redirect(w, r, "/index.html", http.StatusSeeOther)
|
|
|
+ }
|
|
|
+ case "create_dir":
|
|
|
+ r.ParseForm()
|
|
|
+ path := filepath.Join("notes_folder", r.Form.Get("director"), r.Form.Get("subdirector"))
|
|
|
+ fmt.Println(path)
|
|
|
+ err := os.Mkdir(path, 0755)
|
|
|
+ if err != nil {
|
|
|
+ WriteError(w, err)
|
|
|
return
|
|
|
}
|
|
|
referinta := r.Referer()
|
|
|
@@ -179,7 +175,6 @@ func API(w http.ResponseWriter, r *http.Request, s *Server) {
|
|
|
} else {
|
|
|
http.Redirect(w, r, "/index.html", http.StatusSeeOther)
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
}
|
|
|
resp, code := inout.ParseTemplate("templates/error.tmpl", fmt.Errorf("method %s not allowed", r.Method))
|
|
|
@@ -187,3 +182,44 @@ func API(w http.ResponseWriter, r *http.Request, s *Server) {
|
|
|
w.WriteHeader(code)
|
|
|
w.Write(resp)
|
|
|
}
|
|
|
+
|
|
|
+func Design_viewer(w http.ResponseWriter, r *http.Request, s *Server) {
|
|
|
+ f := inout.Folder{}
|
|
|
+ err := inout.GetFolderStructure(s.NotesDir, &f)
|
|
|
+ if err != nil {
|
|
|
+ WriteError(w, err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ data := struct {
|
|
|
+ Folders inout.Folder
|
|
|
+ }{
|
|
|
+ Folders: f,
|
|
|
+ }
|
|
|
+ renderPage(w, []string{
|
|
|
+ "templates/design.tmpl",
|
|
|
+ "templates/design/sections/nav_big.tmpl",
|
|
|
+ }, data)
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+func WriteError(w http.ResponseWriter, err error) {
|
|
|
+ resp, code := inout.ParseTemplate("templates/error.tmpl", err)
|
|
|
+ w.Header().Set("Content-Type", "text/html")
|
|
|
+ w.WriteHeader(code)
|
|
|
+ w.Write(resp)
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+func renderPage(w http.ResponseWriter, templates []string, data any) {
|
|
|
+ tmpl, err := template.New(filepath.Base(templates[0])).Funcs(inout.GetFuncMap()).ParseFiles(templates...)
|
|
|
+ if err != nil {
|
|
|
+ WriteError(w, err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ err = tmpl.ExecuteTemplate(w, filepath.Base(templates[0]), data)
|
|
|
+ if err != nil {
|
|
|
+ WriteError(w, err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+}
|