Browse Source

improved wildcard handler

turos.robert 2 weeks ago
parent
commit
0f01621780

+ 8 - 1
cmds/webservice/bin/mynotes_cfg.json

@@ -1,5 +1,12 @@
 {
 {
   "srvhost": "0.0.0.0",
   "srvhost": "0.0.0.0",
   "srvport": "3000",
   "srvport": "3000",
-  "notes_directory":"notes_folder"
+  "notes_directory": "notes_folder",
+  "routes": {
+    "/list_notes/": "LIST_NOTES",
+    "/notes/": "VIEW_NOTE",
+    "/edit/": "EDIT_NOTE",
+    "/api/" : "API",
+    "/live_preview/" : "MKDOWN_CONVERT"
+  }
 }
 }

+ 1 - 1
cmds/webservice/bin/notes_folder/Scoala/notita-2026-03-08-144007.727634.json

@@ -1,6 +1,6 @@
 {
 {
   "titlu": "Scoala",
   "titlu": "Scoala",
-  "continut": "Trebuie să adaugi faviconul **în interiorul tagului `\u003chead\u003e`**.\r\n\r\nÎn cazul tău, îl pui **după `\u003ctitle\u003e` și înainte de scripturi**.\r\n\r\n### Modificarea corectă a codului tău\r\n\r\n```html\r\n\u003chead\u003e\r\n  \u003cmeta charset=\"UTF-8\"\u003e\r\n  \u003cmeta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"\u003e\r\n  \u003ctitle\u003eMyNotes\u003c/title\u003e\r\n\r\n  \u003clink rel=\"icon\" type=\"image/png\" href=\"/favicon.png\"\u003e\r\n\r\n  \u003cscript src=\"https://cdn.tailwindcss.com\"\u003e\u003c/script\u003e\r\n\u003c/head\u003e\r\n```\r\n\r\n---\r\n\r\n### Unde trebuie să fie fișierul\r\n\r\nCel mai simplu este să pui:\r\n\r\n```\r\nfavicon.png\r\n```\r\n\r\nîn **același folder din care serverul tău servește fișiere statice**.\r\n\r\nDe exemplu dacă serverul tău servește:\r\n\r\n```\r\ntemplates/\r\n```\r\n\r\nsau\r\n\r\n```\r\nstatic/\r\n```\r\n\r\natunci structura ar putea fi:\r\n\r\n```\r\nmynotes/\r\n ├─ templates/\r\n │   ├─ index.html\r\n │   ├─ list_notes.tmpl\r\n │\r\n ├─ static/\r\n │   └─ favicon.png\r\n```\r\n\r\nși atunci linkul devine:\r\n\r\n```html\r\n\u003clink rel=\"icon\" type=\"image/png\" href=\"/static/favicon.png\"\u003e\r\n```\r\n\r\n---\r\n\r\n### Problema cea mai comună\r\n\r\nBrowserul **cache-uiește faviconul**.\r\nDacă nu apare:\r\n\r\n* apasă **Ctrl + Shift + R**\r\n* sau deschide pagina în **incognito**.\r\n\r\n---\r\n\r\n### Extra (recomandat pentru proiectul tău)\r\n\r\nAdaugă și:\r\n\r\n```html\r\n\u003clink rel=\"shortcut icon\" href=\"/favicon.png\"\u003e\r\n```\r\n\r\nUnele browsere îl caută așa.\r\n\r\n---\r\n\r\nDacă vrei, îți pot arăta și **cea mai curată metodă de a servi faviconul în Go în 3 linii**, fără să îți strice handlerul `Wildcard`.\r\n",
+  "continut": "Trebuie să adaugi faviconul **în interiorul tagului `\u003chead\u003e`**.\r\n\r\nÎn cazul tău, îl pui **după `\u003ctitle\u003e` și înainte de scripturi**.\r\n\r\n### Modificarea corectă a codului tău\r\n\r\n```html\r\n\u003chead\u003e\r\n  \u003cmeta charset=\"UTF-8\"\u003e\r\n  \u003cmeta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"\u003e\r\n  \u003ctitle\u003eMyNote\u003c/title\u003e\r\n\r\n  \u003clink rel=\"icon\" type=\"image/png\" href=\"/favicon.png\"\u003e\r\n\r\n  \u003cscript src=\"https://cdn.tailwindcss.com\"\u003e\u003c/script\u003e\r\n\u003c/head\u003e\r\n```\r\n\r\n---\r\n\r\n### Unde trebuie să fie fișierul\r\n\r\nCel mai simplu este să pui:\r\n\r\n```\r\nfavicon.png\r\n```\r\n\r\nîn **același folder din care serverul tău servește fișiere statice**.\r\n\r\nDe exemplu dacă serverul tău servește:\r\n\r\n```\r\ntemplates/\r\n```\r\n\r\nsau\r\n\r\n```\r\nstatic/\r\n```\r\n\r\natunci structura ar putea fi:\r\n\r\n```\r\nmynotes/\r\n ├─ templates/\r\n │   ├─ index.html\r\n │   ├─ list_notes.tmpl\r\n │\r\n ├─ static/\r\n │   └─ favicon.png\r\n```\r\n\r\nși atunci linkul devine:\r\n\r\n```html\r\n\u003clink rel=\"icon\" type=\"image/png\" href=\"/static/favicon.png\"\u003e\r\n```\r\n\r\n---\r\n\r\n### Problema cea mai comună\r\n\r\nBrowserul **cache-uiește faviconul**.\r\nDacă nu apare:\r\n\r\n* apasă **Ctrl + Shift + R**\r\n* sau deschide pagina în **incognito**.\r\n\r\n---\r\n\r\n### Extra (recomandat pentru proiectul tău)\r\n\r\nAdaugă și:\r\n\r\n```html\r\n\u003clink rel=\"shortcut icon\" href=\"/favicon.png\"\u003e\r\n```\r\n\r\nUnele browsere îl caută așa.\r\n\r\n---\r\n\r\nDacă vrei, îți pot arăta și **cea mai curată metodă de a servi faviconul în Go în 3 linii**, fără să îți strice handlerul `Wildcard`.\r\n",
   "director": "Scoala",
   "director": "Scoala",
   "Path": "notes_folder/Scoala/notita-2026-03-08-144007.727634.json"
   "Path": "notes_folder/Scoala/notita-2026-03-08-144007.727634.json"
 }
 }

+ 6 - 0
cmds/webservice/bin/notes_folder/wadw/notita-2026-03-19-223507.006962.json

@@ -0,0 +1,6 @@
+{
+  "titlu": "dwasd",
+  "continut": "wasdwasd",
+  "director": "wadw",
+  "Path": "notes_folder/wadw/notita-2026-03-19-223507.006962.json"
+}

+ 6 - 0
cmds/webservice/bin/notes_folder/wadwasd/notita-2026-03-19-212505.434651.json

@@ -0,0 +1,6 @@
+{
+  "titlu": "wasdwa",
+  "continut": "dwasdws",
+  "director": "wadwasd",
+  "Path": "notes_folder/wadwasd/notita-2026-03-19-212505.434651.json"
+}

+ 1 - 1
cmds/webservice/bin/templates/design.tmpl

@@ -106,7 +106,7 @@ const updatePreview = async () => {
 };
 };
 
 
 // Listen for input, trigger every 500ms of "pause"
 // Listen for input, trigger every 500ms of "pause"
-editor.addEventListener('input', debounce(updatePreview, 500));
+editor.addEventListener('input', debounce(updatePreview, 100));
 </script>
 </script>
 
 
 </body>
 </body>

+ 1 - 0
cmds/webservice/bin/templates/design/sections/head.tmpl

@@ -5,6 +5,7 @@
     <meta name="viewport" content="width=device-width, initial-scale=1.0">
     <meta name="viewport" content="width=device-width, initial-scale=1.0">
     <title>Obsidian Admin Interface</title>
     <title>Obsidian Admin Interface</title>
     <script src="https://cdn.tailwindcss.com"></script>
     <script src="https://cdn.tailwindcss.com"></script>
+    <link rel="icon" type="image/webp" href="/favicon.webp">
     <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/atom-one-dark.min.css">
     <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/atom-one-dark.min.css">
     <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600&display=swap" rel="stylesheet">
     <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600&display=swap" rel="stylesheet">
     <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
     <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>

+ 189 - 0
lib/server/endpoint_funcs.go

@@ -0,0 +1,189 @@
+package server
+
+import (
+	"bytes"
+	"encoding/json"
+	"fmt"
+	"io"
+	"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"
+	"github.com/yuin/goldmark/extension"
+)
+
+func Notes(w http.ResponseWriter, r *http.Request, fisier string) {
+	var buf bytes.Buffer
+	notita := types.Notita{}
+	err := inout.FileToObj(fisier, &notita)
+	if err != nil {
+		w.WriteHeader(http.StatusInternalServerError)
+		w.Write([]byte(WEB_ERR))
+		template.Must(template.ParseFiles("templates/error.tmpl")).Execute(w, err)
+		return
+	}
+	if err := goldmark.Convert([]byte(notita.Continut), &buf); err != nil {
+		w.WriteHeader(http.StatusInternalServerError)
+		w.Write([]byte(WEB_ERR))
+		template.Must(template.ParseFiles("templates/error.tmpl")).Execute(w, err)
+		return
+	}
+	notita.HTML = buf.String()
+	resp, code := inout.ParseTemplate("templates/notita.tmpl", notita)
+	w.WriteHeader(code)
+	w.Write(resp)
+}
+
+func ListNotes(w http.ResponseWriter, r *http.Request, s *Server) {
+
+	if r.Method == "GET" {
+		f := inout.Folder{}
+		err := inout.GetFolderStructure(s.NotesDir, &f)
+		if err != nil {
+			resp, code := inout.ParseTemplate("templates/error.tmpl", err)
+			w.Header().Set("Content-Type", "text/html")
+			w.WriteHeader(code)
+			w.Write(resp)
+			return
+		}
+		resp, code := inout.ParseTemplate("templates/list_notes.tmpl", f)
+		w.Header().Set("Content-Type", "text/html")
+		w.WriteHeader(code)
+		w.Write(resp)
+		return
+	}
+	w.WriteHeader(http.StatusMethodNotAllowed)
+	w.Write([]byte("methoda nepermisa"))
+}
+
+func EditNotes(w http.ResponseWriter, r *http.Request, filename string) {
+
+	if r.Method == "GET" {
+		f := types.Notita{}
+		err := inout.FileToObj(filename, &f)
+		if err != nil {
+			resp, code := inout.ParseTemplate("templates/error.tmpl", err)
+			w.WriteHeader(code)
+			w.Write(resp)
+		}
+		resp, code := inout.ParseTemplate("templates/edit_notes.tmpl", f)
+		w.WriteHeader(code)
+		w.Write(resp)
+		return
+	}
+	resp, code := inout.ParseTemplate("templates/error.tmpl", fmt.Errorf("method %s not allowed", r.Method))
+	w.WriteHeader(code)
+	w.Write(resp)
+}
+
+func MDToHTML(w http.ResponseWriter, r *http.Request) {
+	body, err := io.ReadAll(r.Body)
+	if err != nil {
+		fmt.Fprintf(w, "%+v", err)
+	}
+	mp := make(map[string]any)
+	err = json.Unmarshal(body, &mp)
+	if err != nil {
+		fmt.Fprintf(w, "%+v", err)
+	}
+	var buff bytes.Buffer
+	md := goldmark.New(
+		goldmark.WithExtensions(extension.Table,
+			extension.DefinitionList,
+			extension.Footnote,
+			extension.Typographer))
+	err = md.Convert([]byte(mp["markdown"].(string)), &buff)
+	if err != nil {
+		fmt.Fprintf(w, "%+v", err)
+	}
+	w.Write(buff.Bytes())
+}
+
+func API(w http.ResponseWriter, r *http.Request, s *Server) {
+	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()
+			n := types.Notita{}
+			n.Titlu = r.Form.Get("titlu")
+			n.Continut = r.Form.Get("notita")
+			n.Folder = r.Form.Get("director")
+			if n.Folder == "" {
+				n.Folder = "default"
+			}
+			err := inout.SafeDir(n.Folder)
+			if err != nil {
+				resp, code := inout.ParseTemplate("templates/error.tmpl", err)
+				w.Header().Set("Content-Type", "text/html")
+				w.WriteHeader(code)
+				w.Write(resp)
+				return
+			}
+			path := filepath.Join(s.NotesDir, n.Folder)
+			err = os.MkdirAll(path, 0755)
+			fmt.Println(n.Folder)
+			if err != nil {
+				resp, code := inout.ParseTemplate("templates/error.tmpl", err)
+				w.Header().Set("Content-Type", "text/html")
+				w.WriteHeader(code)
+				w.Write(resp)
+				return
+			}
+
+			fisier := fmt.Sprintf("notita-%s.json", time.Now().Format("2006-01-02-150405.999999"))
+			n.Path = filepath.Join("notes_folder", n.Folder, fisier)
+			//fmt.Println(fisier, r.Form)
+			//return
+
+			err = inout.ObjToFile(filepath.Join(s.NotesDir, n.Folder, fisier), n, true)
+			if err != nil {
+				resp, code := inout.ParseTemplate("templates/error.tmpl", err)
+				w.Header().Set("Content-Type", "text/html")
+				w.WriteHeader(code)
+				w.Write(resp)
+				return
+			}
+			referinta := r.Referer()
+			fmt.Println(referinta)
+			if referinta != "" {
+				http.Redirect(w, r, referinta, http.StatusSeeOther)
+			} else {
+				http.Redirect(w, r, "/index.html", http.StatusSeeOther)
+			}
+		case "update_note":
+			r.ParseForm()
+			n := types.Notita{}
+			n.Titlu = r.Form.Get("titlu")
+			n.Path = r.Form.Get("path")
+			n.Folder = r.Form.Get("director")
+			n.Continut = r.Form.Get("notita")
+			fmt.Println(n.Path)
+			err := inout.ObjToFile(n.Path, 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)
+			}
+
+		}
+	}
+	resp, code := inout.ParseTemplate("templates/error.tmpl", fmt.Errorf("method %s not allowed", r.Method))
+	w.Header().Set("Content-Type", "text/html")
+	w.WriteHeader(code)
+	w.Write(resp)
+}

+ 66 - 178
lib/server/handlers.go

@@ -1,216 +1,104 @@
 package server
 package server
 
 
 import (
 import (
-	"bytes"
-	"encoding/json"
 	"fmt"
 	"fmt"
-	"io"
 	"net/http"
 	"net/http"
 	"os"
 	"os"
 	"path/filepath"
 	"path/filepath"
 	"strings"
 	"strings"
-	"text/template"
-	"time"
 
 
-	"git.linuxit.ro/turos.robert/mynotes/cmds/webservice/types"
 	"git.linuxit.ro/turos.robert/mynotes/lib/inout"
 	"git.linuxit.ro/turos.robert/mynotes/lib/inout"
-	"github.com/yuin/goldmark"
-	"github.com/yuin/goldmark/extension"
 )
 )
 
 
-func Wildcard(w http.ResponseWriter, r *http.Request) {
+func Wildcard(w http.ResponseWriter, r *http.Request, s *Server) {
+	baseDir := "./templates/"
+	endpoint := strings.TrimPrefix(r.URL.Path, "/")
 
 
-	if r.Method == "GET" {
-		base_dir := "./templates/"
-		endpoint := strings.TrimPrefix(r.URL.Path, "/")
-		//fmt.Println(endpoint)
-		//fmt.Println(r.URL.Path)
-		if endpoint == "" {
-			//in cazul in care apelam root
-			endpoint = "index.html"
-		}
-		path := filepath.Join(base_dir, endpoint)
-		_, err := os.Stat(path)
-		if err != nil {
-			w.Header().Add("Content-Type", "text/html")
+	if action, ok := findRoute(r.URL.Path, s.Routes); ok {
+		handlers := makeFuncMap()
 
 
-			resp, code := inout.ParseTemplate("templates/design.tmpl", err)
+		handler, exists := handlers[action]
+		if !exists {
+			resp, code := inout.ParseTemplate("templates/error.tmpl", fmt.Errorf("handler %s not found", action))
+			w.Header().Set("Content-Type", "text/html")
 			w.WriteHeader(code)
 			w.WriteHeader(code)
 			w.Write(resp)
 			w.Write(resp)
 			return
 			return
 		}
 		}
-		w.Write(inout.FileToBytes(path))
+
+		handler(w, r)
 		return
 		return
 	}
 	}
 
 
-}
-
-func API(w http.ResponseWriter, r *http.Request, s *Server) {
-	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()
-			n := types.Notita{}
-			n.Titlu = r.Form.Get("titlu")
-			n.Continut = r.Form.Get("notita")
-			n.Folder = r.Form.Get("director")
-			if n.Folder == "" {
-				n.Folder = "default"
-			}
-			err := inout.SafeDir(n.Folder)
-			if err != nil {
-				resp, code := inout.ParseTemplate("templates/error.tmpl", err)
-				w.Header().Set("Content-Type", "text/html")
-				w.WriteHeader(code)
-				w.Write(resp)
-				return
-			}
-			path := filepath.Join(s.NotesDir, n.Folder)
-			err = os.MkdirAll(path, 0755)
-			fmt.Println(n.Folder)
-			if err != nil {
-				resp, code := inout.ParseTemplate("templates/error.tmpl", err)
-				w.Header().Set("Content-Type", "text/html")
-				w.WriteHeader(code)
-				w.Write(resp)
-				return
-			}
-
-			fisier := fmt.Sprintf("notita-%s.json", time.Now().Format("2006-01-02-150405.999999"))
-			n.Path = filepath.Join("notes_folder", n.Folder, fisier)
-			//fmt.Println(fisier, r.Form)
-			//return
-
-			err = inout.ObjToFile(filepath.Join(s.NotesDir, n.Folder, fisier), n, true)
-			if err != nil {
-				resp, code := inout.ParseTemplate("templates/error.tmpl", err)
-				w.Header().Set("Content-Type", "text/html")
-				w.WriteHeader(code)
-				w.Write(resp)
-				return
-			}
-			referinta := r.Referer()
-			fmt.Println(referinta)
-			if referinta != "" {
-				http.Redirect(w, r, referinta, http.StatusSeeOther)
-			} else {
-				http.Redirect(w, r, "/index.html", http.StatusSeeOther)
-			}
-		case "update_note":
-			r.ParseForm()
-			n := types.Notita{}
-			n.Titlu = r.Form.Get("titlu")
-			n.Path = r.Form.Get("path")
-			n.Folder = r.Form.Get("director")
-			n.Continut = r.Form.Get("notita")
-			fmt.Println(n.Path)
-			err := inout.ObjToFile(n.Path, 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)
-			}
-
-		}
+	if endpoint == "" {
+		endpoint = "index.html"
 	}
 	}
-	resp, code := inout.ParseTemplate("templates/error.tmpl", fmt.Errorf("method %s not allowed", r.Method))
-	w.Header().Set("Content-Type", "text/html")
-	w.WriteHeader(code)
-	w.Write(resp)
-}
 
 
-func Notes(w http.ResponseWriter, r *http.Request, fisier string) {
-	var buf bytes.Buffer
-	notita := types.Notita{}
-	err := inout.FileToObj(fisier, &notita)
+	path := filepath.Join(baseDir, endpoint)
+	_, err := os.Stat(path)
 	if err != nil {
 	if err != nil {
-		w.WriteHeader(http.StatusInternalServerError)
-		w.Write([]byte(WEB_ERR))
-		template.Must(template.ParseFiles("templates/error.tmpl")).Execute(w, err)
-		return
-	}
-	if err := goldmark.Convert([]byte(notita.Continut), &buf); err != nil {
-		w.WriteHeader(http.StatusInternalServerError)
-		w.Write([]byte(WEB_ERR))
-		template.Must(template.ParseFiles("templates/error.tmpl")).Execute(w, err)
-		return
-	}
-	notita.HTML = buf.String()
-	resp, code := inout.ParseTemplate("templates/notita.tmpl", notita)
-	w.WriteHeader(code)
-	w.Write(resp)
-}
-
-func ListNotes(w http.ResponseWriter, r *http.Request, s *Server) {
-
-	if r.Method == "GET" {
-		f := inout.Folder{}
-		err := inout.GetFolderStructure(s.NotesDir, &f)
-		if err != nil {
-			resp, code := inout.ParseTemplate("templates/error.tmpl", err)
-			w.Header().Set("Content-Type", "text/html")
-			w.WriteHeader(code)
-			w.Write(resp)
-			return
-		}
-		resp, code := inout.ParseTemplate("templates/list_notes.tmpl", f)
 		w.Header().Set("Content-Type", "text/html")
 		w.Header().Set("Content-Type", "text/html")
+		resp, code := inout.ParseTemplate("templates/error.tmpl", err)
 		w.WriteHeader(code)
 		w.WriteHeader(code)
 		w.Write(resp)
 		w.Write(resp)
 		return
 		return
 	}
 	}
-	w.WriteHeader(http.StatusMethodNotAllowed)
-	w.Write([]byte("methoda nepermisa"))
+
+	w.Write(inout.FileToBytes(path))
 }
 }
+func findRoute(path string, routes map[string]string) (string, bool) {
+	// 1. exact match
+	if action, ok := routes[path]; ok {
+		return action, true
+	}
 
 
-func EditNotes(w http.ResponseWriter, r *http.Request, filename string) {
+	// 2. longest prefix match
+	longest := ""
+	action := ""
 
 
-	if r.Method == "GET" {
-		f := types.Notita{}
-		err := inout.FileToObj(filename, &f)
-		if err != nil {
-			resp, code := inout.ParseTemplate("templates/error.tmpl", err)
-			w.WriteHeader(code)
-			w.Write(resp)
+	for route, a := range routes {
+		if strings.HasPrefix(path, route) {
+			if len(route) > len(longest) {
+				longest = route
+				action = a
+			}
 		}
 		}
-		resp, code := inout.ParseTemplate("templates/edit_notes.tmpl", f)
-		w.WriteHeader(code)
-		w.Write(resp)
-		return
 	}
 	}
-	resp, code := inout.ParseTemplate("templates/error.tmpl", fmt.Errorf("method %s not allowed", r.Method))
-	w.WriteHeader(code)
-	w.Write(resp)
-}
 
 
-func MDToHTML(w http.ResponseWriter, r *http.Request) {
-	body, err := io.ReadAll(r.Body)
-	if err != nil {
-		fmt.Fprintf(w, "%+v", err)
+	if longest != "" {
+		return action, true
 	}
 	}
-	mp := make(map[string]any)
-	err = json.Unmarshal(body, &mp)
-	if err != nil {
-		fmt.Fprintf(w, "%+v", err)
-	}
-	var buff bytes.Buffer
-	md := goldmark.New(
-		goldmark.WithExtensions(extension.Table,
-			extension.DefinitionList,
-			extension.Footnote,
-			extension.Typographer))
-	err = md.Convert([]byte(mp["markdown"].(string)), &buff)
-	if err != nil {
-		fmt.Fprintf(w, "%+v", err)
+
+	return "", false
+}
+
+func makeFuncMap() map[string]hfunc {
+	handlers := map[string]hfunc{
+		"VIEW_NOTE": func(w http.ResponseWriter, r *http.Request) {
+			note := r.URL.Query().Get("note")
+			Notes(w, r, note)
+		},
+		"EDIT_NOTE": func(w http.ResponseWriter, r *http.Request) {
+			note := r.URL.Query().Get("note")
+			EditNotes(w, r, note)
+		},
+		"LIST_NOTES": func(w http.ResponseWriter, r *http.Request) {
+			srv := Server{}
+			err := inout.FileToObj("mynotes_cfg.json", &srv)
+			if err != nil {
+				srv.Err = err
+			}
+			ListNotes(w, r, &srv)
+		},
+		"API": func(w http.ResponseWriter, r *http.Request) {
+			srv := Server{}
+			err := inout.FileToObj("mynotes_cfg.json", &srv)
+			if err != nil {
+				srv.Err = err
+			}
+			API(w, r, &srv)
+		},
+		"MKDOWN_CONVERT": MDToHTML,
 	}
 	}
-	w.Write(buff.Bytes())
+	return handlers
 }
 }

+ 2 - 19
lib/server/srv.go

@@ -21,25 +21,8 @@ func NewServer(filename string) *Server {
 func (s *Server) Run() {
 func (s *Server) Run() {
 	fmt.Println(s.Port, s.Host)
 	fmt.Println(s.Port, s.Host)
 	// adding endpoints to the server
 	// adding endpoints to the server
-	http.HandleFunc("/", Wildcard)
-	http.HandleFunc("/api/", func(w http.ResponseWriter, r *http.Request) {
-		API(w, r, s)
-	})
-	http.HandleFunc("/notes/", func(w http.ResponseWriter, r *http.Request) {
-		note := r.URL.Query().Get("note")
-		Notes(w, r, note)
-	})
-	http.HandleFunc("/list_notes/", func(w http.ResponseWriter, r *http.Request) {
-		ListNotes(w, r, s)
-
-	})
-	http.HandleFunc("/edit/", func(w http.ResponseWriter, r *http.Request) {
-		note := r.URL.Query().Get("note")
-		EditNotes(w, r, note)
-	})
-
-	http.HandleFunc("/live_preview/", func(w http.ResponseWriter, r *http.Request) {
-		MDToHTML(w, r)
+	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
+		Wildcard(w, r, s)
 	})
 	})
 
 
 	//running server
 	//running server

+ 8 - 3
lib/server/types.go

@@ -1,8 +1,13 @@
 package server
 package server
 
 
+import "net/http"
+
 type Server struct {
 type Server struct {
-	Host     string `json:"srvhost"`
-	Port     string `json:"srvport"`
-	NotesDir string `json:"notes_directory"`
+	Host     string            `json:"srvhost"`
+	Port     string            `json:"srvport"`
+	NotesDir string            `json:"notes_directory"`
+	Routes   map[string]string `json:"routes"`
 	Err      error
 	Err      error
 }
 }
+
+type hfunc func(w http.ResponseWriter, r *http.Request)