handlers.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. /*if endpoint == "" {
  21. //in cazul in care apelam root
  22. endpoint = "index"
  23. }
  24. path := filepath.Join("templates", endpoint+".tmpl")
  25. _, err := os.Stat(path)
  26. if err != nil {
  27. w.WriteHeader(http.StatusNotFound)
  28. w.Write(inout.FileToBytes("templates/error.tmpl"))
  29. return
  30. }*/
  31. path := filepath.Join(base_dir, endpoint)
  32. _, err := os.Stat(path)
  33. if err != nil {
  34. w.WriteHeader(http.StatusInternalServerError)
  35. w.Write(inout.FileToBytes("templates/error.tmpl"))
  36. }
  37. w.Write(inout.FileToBytes(path))
  38. return
  39. }
  40. }
  41. func API(w http.ResponseWriter, r *http.Request, notesFolder string) {
  42. action := strings.TrimPrefix(r.URL.Path, "/api/")
  43. if r.Method == "POST" {
  44. //w.WriteHeader(http.StatusNotImplemented)
  45. //w.Write(fmt.Appendf(nil, "action not implemented: %s", action))
  46. switch action {
  47. case "save_note":
  48. r.ParseForm()
  49. fisier := fmt.Sprintf("notita-%s.json", time.Now().Format("2006-01-02-150405.999999"))
  50. //fmt.Println(fisier, r.Form)
  51. //return
  52. n := types.Notita{}
  53. n.Titlu = r.Form.Get("titlu")
  54. n.Continut = r.Form.Get("notita")
  55. err := inout.ObjToFile(filepath.Join(notesFolder, 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. if referinta != "" {
  63. http.Redirect(w, r, referinta, http.StatusSeeOther)
  64. } else {
  65. http.Redirect(w, r, "/index.html", http.StatusSeeOther)
  66. }
  67. }
  68. }
  69. w.WriteHeader(http.StatusMethodNotAllowed)
  70. w.Write([]byte("methoda nepermisa"))
  71. }
  72. func Notes(w http.ResponseWriter, r *http.Request, fisier string) {
  73. var buf bytes.Buffer
  74. notita := types.Notita{}
  75. err := inout.FileToObj(fisier, &notita)
  76. if err != nil {
  77. w.WriteHeader(http.StatusInternalServerError)
  78. w.Write([]byte(WEB_ERR))
  79. fmt.Printf("%+v\n", err)
  80. return
  81. }
  82. if err := goldmark.Convert([]byte(notita.Continut), &buf); err != nil {
  83. w.WriteHeader(http.StatusInternalServerError)
  84. w.Write([]byte(WEB_ERR))
  85. fmt.Printf("%+v\n", err)
  86. return
  87. }
  88. notita.HTML = buf.String()
  89. tmpl := template.New("notita")
  90. _, err = tmpl.ParseFiles("./templates/notita.tmpl")
  91. if err != nil {
  92. w.WriteHeader(http.StatusInternalServerError)
  93. w.Write([]byte(WEB_ERR))
  94. fmt.Printf("%+v\n", err)
  95. return
  96. }
  97. err = tmpl.ExecuteTemplate(w, "notita.tmpl", notita)
  98. if err != nil {
  99. w.WriteHeader(http.StatusInternalServerError)
  100. w.Write([]byte(WEB_ERR))
  101. fmt.Printf("%+v\n", err)
  102. return
  103. }
  104. }