handlers.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. err := os.MkdirAll(n.Folder, 0755)
  48. if err != nil {
  49. w.WriteHeader(http.StatusBadRequest)
  50. fmt.Fprintf(w, "ParseForm error: %+v", err)
  51. }
  52. fisier := fmt.Sprintf("notita-%s.json", time.Now().Format("2006-01-02-150405.999999"))
  53. //fmt.Println(fisier, r.Form)
  54. //return
  55. err = inout.ObjToFile(filepath.Join(n.Folder, fisier), n, true)
  56. if err != nil {
  57. w.WriteHeader(http.StatusInternalServerError)
  58. fmt.Fprintf(w, "%+v", err)
  59. return
  60. }
  61. referinta := r.Referer()
  62. fmt.Println(referinta)
  63. if referinta != "" {
  64. http.Redirect(w, r, referinta, http.StatusSeeOther)
  65. } else {
  66. http.Redirect(w, r, "/index.html", http.StatusSeeOther)
  67. }
  68. }
  69. }
  70. w.WriteHeader(http.StatusMethodNotAllowed)
  71. w.Write([]byte("methoda nepermisa"))
  72. }
  73. func Notes(w http.ResponseWriter, r *http.Request, fisier string) {
  74. var buf bytes.Buffer
  75. notita := types.Notita{}
  76. err := inout.FileToObj(fisier, &notita)
  77. if err != nil {
  78. w.WriteHeader(http.StatusInternalServerError)
  79. w.Write([]byte(WEB_ERR))
  80. fmt.Printf("%+v\n", err)
  81. return
  82. }
  83. if err := goldmark.Convert([]byte(notita.Continut), &buf); err != nil {
  84. w.WriteHeader(http.StatusInternalServerError)
  85. w.Write([]byte(WEB_ERR))
  86. fmt.Printf("%+v\n", err)
  87. return
  88. }
  89. notita.HTML = buf.String()
  90. tmpl := template.New("notita")
  91. _, err = tmpl.ParseFiles("./templates/notita.tmpl")
  92. if err != nil {
  93. w.WriteHeader(http.StatusInternalServerError)
  94. w.Write([]byte(WEB_ERR))
  95. fmt.Printf("%+v\n", err)
  96. return
  97. }
  98. err = tmpl.ExecuteTemplate(w, "notita.tmpl", notita)
  99. if err != nil {
  100. w.WriteHeader(http.StatusInternalServerError)
  101. w.Write([]byte(WEB_ERR))
  102. fmt.Printf("%+v\n", err)
  103. return
  104. }
  105. }