|
|
@@ -1,19 +1,27 @@
|
|
|
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, "/")
|
|
|
- if endpoint == "" {
|
|
|
+ fmt.Println(endpoint)
|
|
|
+ /*if endpoint == "" {
|
|
|
//in cazul in care apelam root
|
|
|
endpoint = "index"
|
|
|
}
|
|
|
@@ -23,9 +31,81 @@ func Wildcard(w http.ResponseWriter, r *http.Request) {
|
|
|
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
|
|
|
+ }
|
|
|
+}
|