handlers.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package server
  2. import (
  3. "bytes"
  4. "fmt"
  5. "net/http"
  6. "os"
  7. "path/filepath"
  8. "strings"
  9. "text/template"
  10. "time"
  11. "git.linuxit.ro/turos.robert/mynotes/cmds/webservice/types"
  12. "git.linuxit.ro/turos.robert/mynotes/lib/inout"
  13. "github.com/yuin/goldmark"
  14. )
  15. func Wildcard(w http.ResponseWriter, r *http.Request) {
  16. if r.Method == "GET" {
  17. base_dir := "./templates/"
  18. endpoint := strings.TrimPrefix(r.URL.Path, "/")
  19. //fmt.Println(endpoint)
  20. //fmt.Println(r.URL.Path)
  21. if endpoint == "" {
  22. //in cazul in care apelam root
  23. endpoint = "index.html"
  24. }
  25. path := filepath.Join(base_dir, endpoint)
  26. _, err := os.Stat(path)
  27. if err != nil {
  28. w.WriteHeader(http.StatusInternalServerError)
  29. w.Write(inout.FileToBytes("templates/error.tmpl"))
  30. }
  31. w.Write(inout.FileToBytes(path))
  32. return
  33. }
  34. }
  35. func API(w http.ResponseWriter, r *http.Request) {
  36. action := strings.TrimPrefix(r.URL.Path, "/api/")
  37. if r.Method == "POST" {
  38. //w.WriteHeader(http.StatusNotImplemented)
  39. //w.Write(fmt.Appendf(nil, "action not implemented: %s", action))
  40. switch action {
  41. case "save_note":
  42. r.ParseForm()
  43. n := types.Notita{}
  44. n.Titlu = r.Form.Get("titlu")
  45. n.Continut = r.Form.Get("notita")
  46. n.Folder = r.Form.Get("director")
  47. if n.Folder == "" {
  48. n.Folder = "default"
  49. }
  50. err := safeDirectory(n.Folder)
  51. if err != nil {
  52. w.WriteHeader(http.StatusBadRequest)
  53. fmt.Fprintf(w, "Error : %+v, %s", err, n.Folder)
  54. return
  55. }
  56. err = os.MkdirAll(filepath.Join("notes_folder", n.Folder), 0755)
  57. fmt.Println(n.Folder)
  58. if err != nil {
  59. w.WriteHeader(http.StatusBadRequest)
  60. fmt.Fprintf(w, "ParseForm error: %+v , %s", err, n.Folder)
  61. return
  62. }
  63. fisier := fmt.Sprintf("notita-%s.json", time.Now().Format("2006-01-02-150405.999999"))
  64. //fmt.Println(fisier, r.Form)
  65. //return
  66. err = inout.ObjToFile(filepath.Join("notes_folder", n.Folder, fisier), n, true)
  67. if err != nil {
  68. w.WriteHeader(http.StatusInternalServerError)
  69. fmt.Fprintf(w, "%+v", err)
  70. return
  71. }
  72. referinta := r.Referer()
  73. fmt.Println(referinta)
  74. if referinta != "" {
  75. http.Redirect(w, r, referinta, http.StatusSeeOther)
  76. } else {
  77. http.Redirect(w, r, "/index.html", http.StatusSeeOther)
  78. }
  79. }
  80. }
  81. w.WriteHeader(http.StatusMethodNotAllowed)
  82. w.Write([]byte("methoda nepermisa"))
  83. }
  84. func Notes(w http.ResponseWriter, r *http.Request, fisier string) {
  85. var buf bytes.Buffer
  86. notita := types.Notita{}
  87. err := inout.FileToObj(fisier, &notita)
  88. if err != nil {
  89. w.WriteHeader(http.StatusInternalServerError)
  90. w.Write([]byte(WEB_ERR))
  91. fmt.Printf("%+v\n", err)
  92. return
  93. }
  94. if err := goldmark.Convert([]byte(notita.Continut), &buf); err != nil {
  95. w.WriteHeader(http.StatusInternalServerError)
  96. w.Write([]byte(WEB_ERR))
  97. fmt.Printf("%+v\n", err)
  98. return
  99. }
  100. notita.HTML = buf.String()
  101. tmpl := template.New("notita")
  102. _, err = tmpl.ParseFiles("./templates/notita.tmpl")
  103. if err != nil {
  104. w.WriteHeader(http.StatusInternalServerError)
  105. w.Write([]byte(WEB_ERR))
  106. fmt.Printf("%+v\n", err)
  107. return
  108. }
  109. err = tmpl.ExecuteTemplate(w, "notita.tmpl", notita)
  110. if err != nil {
  111. w.WriteHeader(http.StatusInternalServerError)
  112. w.Write([]byte(WEB_ERR))
  113. fmt.Printf("%+v\n", err)
  114. return
  115. }
  116. }
  117. func safeDirectory(path string) error {
  118. path = filepath.Clean(path)
  119. if strings.HasPrefix(path, "..") || filepath.IsAbs(path) {
  120. return fmt.Errorf("Invalid Path")
  121. }
  122. return nil
  123. }
  124. func ListNotes(w http.ResponseWriter, r *http.Request) {
  125. if r.Method == "GET" {
  126. f := types.Folder{}
  127. err := buildFolderStructure("./notes_folder", &f)
  128. if err != nil {
  129. fmt.Println("EROARE la buildFolderStructure:", err)
  130. w.WriteHeader(http.StatusBadRequest)
  131. fmt.Fprintf(w, "%+v", err)
  132. return
  133. }
  134. tmpl := template.New("list_notes_test")
  135. _, err = tmpl.ParseFiles("./templates/list_notes.tmpl")
  136. if err != nil {
  137. fmt.Println("EROARE la buildFolderStructure:", err)
  138. w.WriteHeader(http.StatusBadRequest)
  139. fmt.Fprintf(w, "%+v", err)
  140. return
  141. }
  142. err = tmpl.ExecuteTemplate(w, "list_notes.tmpl", f)
  143. if err != nil {
  144. fmt.Println("EROARE la buildFolderStructure:", err)
  145. w.WriteHeader(http.StatusBadRequest)
  146. fmt.Fprintf(w, "%+v", err)
  147. return
  148. }
  149. return
  150. }
  151. w.WriteHeader(http.StatusMethodNotAllowed)
  152. w.Write([]byte("methoda nepermisa"))
  153. }
  154. func buildFolderStructure(path string, f *types.Folder) error {
  155. entries, err := os.ReadDir(path)
  156. if err != nil {
  157. return err
  158. }
  159. f.Nume = filepath.Clean(path)
  160. for _, entry := range entries {
  161. if entry.IsDir() {
  162. sub := types.Folder{}
  163. err = buildFolderStructure(filepath.Join(path, entry.Name()), &sub)
  164. if err != nil {
  165. return err
  166. }
  167. f.Subfoldere = append(f.Subfoldere, sub)
  168. } else {
  169. fisier := types.Fisier{
  170. Nume: entry.Name(),
  171. Path: filepath.Join(path, entry.Name()),
  172. }
  173. f.Fisiere = append(f.Fisiere, fisier)
  174. }
  175. }
  176. return nil
  177. }