| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- package server
- import (
- "bytes"
- "fmt"
- "net/http"
- "os"
- "path/filepath"
- "strings"
- "text/template"
- "time"
- "git.linuxit.ro/turos.robert/mynotes/cmds/webservice/types"
- "git.linuxit.ro/turos.robert/mynotes/lib/inout"
- "github.com/yuin/goldmark"
- )
- func Wildcard(w http.ResponseWriter, r *http.Request) {
- if r.Method == "GET" {
- base_dir := "./templates/"
- endpoint := strings.TrimPrefix(r.URL.Path, "/")
- fmt.Println(endpoint)
- /*if endpoint == "" {
- //in cazul in care apelam root
- endpoint = "index"
- }
- path := filepath.Join("templates", endpoint+".tmpl")
- _, err := os.Stat(path)
- if err != nil {
- w.WriteHeader(http.StatusNotFound)
- w.Write(inout.FileToBytes("templates/error.tmpl"))
- return
- }*/
- path := filepath.Join(base_dir, endpoint)
- _, err := os.Stat(path)
- if err != nil {
- w.WriteHeader(http.StatusInternalServerError)
- w.Write(inout.FileToBytes("templates/error.tmpl"))
- }
- w.Write(inout.FileToBytes(path))
- return
- }
- }
- func API(w http.ResponseWriter, r *http.Request, notesFolder string) {
- action := strings.TrimPrefix(r.URL.Path, "/api/")
- if r.Method == "POST" {
- //w.WriteHeader(http.StatusNotImplemented)
- //w.Write(fmt.Appendf(nil, "action not implemented: %s", action))
- switch action {
- case "save_note":
- r.ParseForm()
- fisier := fmt.Sprintf("notita-%s.json", time.Now().Format("2006-01-02-150405.999999"))
- //fmt.Println(fisier, r.Form)
- //return
- n := types.Notita{}
- n.Titlu = r.Form.Get("titlu")
- n.Continut = r.Form.Get("notita")
- err := inout.ObjToFile(filepath.Join(notesFolder, fisier), n, true)
- if err != nil {
- w.WriteHeader(http.StatusInternalServerError)
- fmt.Fprintf(w, "%+v", err)
- return
- }
- referinta := r.Referer()
- if referinta != "" {
- http.Redirect(w, r, referinta, http.StatusSeeOther)
- } else {
- http.Redirect(w, r, "/index.html", http.StatusSeeOther)
- }
- }
- }
- w.WriteHeader(http.StatusMethodNotAllowed)
- w.Write([]byte("methoda nepermisa"))
- }
- func Notes(w http.ResponseWriter, r *http.Request, fisier string) {
- var buf bytes.Buffer
- notita := types.Notita{}
- err := inout.FileToObj(fisier, ¬ita)
- if err != nil {
- w.WriteHeader(http.StatusInternalServerError)
- w.Write([]byte(WEB_ERR))
- fmt.Printf("%+v\n", err)
- return
- }
- if err := goldmark.Convert([]byte(notita.Continut), &buf); err != nil {
- w.WriteHeader(http.StatusInternalServerError)
- w.Write([]byte(WEB_ERR))
- fmt.Printf("%+v\n", err)
- return
- }
- notita.HTML = buf.String()
- tmpl := template.New("notita")
- _, err = tmpl.ParseFiles("./templates/notita.tmpl")
- if err != nil {
- w.WriteHeader(http.StatusInternalServerError)
- w.Write([]byte(WEB_ERR))
- fmt.Printf("%+v\n", err)
- return
- }
- err = tmpl.ExecuteTemplate(w, "notita.tmpl", notita)
- if err != nil {
- w.WriteHeader(http.StatusInternalServerError)
- w.Write([]byte(WEB_ERR))
- fmt.Printf("%+v\n", err)
- return
- }
- }
|