miki

Miki: personal wiki with the fmd markup format
git clone https://wtenison.com/repo/miki.git
Log | Files | Refs | README | LICENSE

commit b1ab631f06a65218fe491e32db0ace97c278871b
parent 0c15acee707fc373831f635e081d472737d271be
Author: weetat <wtenison@protonmail.com>
Date:   Tue, 15 Apr 2025 21:44:52 +0000

Merge page handlers into a single handleView

/home, /subject and /edit all just render their template now. Drop the
/data/ route and data directory config, serve /sw.js from the web root,
and move the favicon to web/icons.

Diffstat:
Msrc/filesystem/files.go | 15---------------
Msrc/filesystem/filesystem.go | 2--
Msrc/miki/miki.go | 1-
Msrc/network/routes.go | 95++++++++++++++-----------------------------------------------------------------
Rweb/global/favicon.ico -> web/icons/favicon.ico | 0
5 files changed, 16 insertions(+), 97 deletions(-)

diff --git a/src/filesystem/files.go b/src/filesystem/files.go @@ -7,21 +7,6 @@ import ( "path/filepath" ) -func ReadFileFMD(inputFilePath string) (string, error) { - - _fileName := filepath.Base(inputFilePath) - if filepath.Ext(_fileName) != ".fmd" { - return "", fmt.Errorf("ReadFileFMD:filepath.Ext:%s:invalid file extension", _fileName) - } - - _fsData, _err := os.ReadFile(inputFilePath) - if _err != nil { - return "", fmt.Errorf("ReadFileFMD:os.ReadFile:%s:%s", inputFilePath, _err.Error()) - } - - return string(_fsData), nil -} - func ReadFileJSON(inputFilePath string, outputDataContainer any) error { _fileName := filepath.Base(inputFilePath) diff --git a/src/filesystem/filesystem.go b/src/filesystem/filesystem.go @@ -12,7 +12,6 @@ var WebGlobalPkg DirectoryPackage func Init() { var _fsDataStruct struct { - DataDirectory string `json:"data-directory"` WebDirectory string `json:"web-directory"` } @@ -20,7 +19,6 @@ func Init() { log.Panicf("__PANIC:filesystem:Init:%s", _err) } - Dirs["data"] = _fsDataStruct.DataDirectory Dirs["web"] = _fsDataStruct.WebDirectory log.Printf("INFO:filesystem:Init:Dirs:%v", Dirs) diff --git a/src/miki/miki.go b/src/miki/miki.go @@ -8,6 +8,5 @@ import ( func main() { mfs.Init() mnw.Init() - mnw.Exec() } diff --git a/src/network/routes.go b/src/network/routes.go @@ -1,6 +1,7 @@ package network import ( + "fmt" "log" "net/http" "path/filepath" @@ -9,8 +10,6 @@ import ( mfs "codeberg.org/wtenison/miki/src/filesystem" ) -// TODO: There is a lot of duplicate functionality here. Simplify/consolidate. - var mux *http.ServeMux func makeServerRoutes() { @@ -18,18 +17,19 @@ func makeServerRoutes() { mux = http.NewServeMux() mux.Handle("/web/", http.StripPrefix("/web/", http.FileServer(http.Dir(mfs.Dirs["web"])))) - mux.Handle("/data/", http.StripPrefix("/data/", http.FileServer(http.Dir(mfs.Dirs["data"])))) mux.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) { - http.ServeFile(w, r, filepath.Join(mfs.Dirs["web"], "/global/favicon.ico")) + http.ServeFile(w, r, filepath.Join(mfs.Dirs["web"], "/icons/favicon.ico")) }) - mux.HandleFunc("/home", handlePageHome) - mux.HandleFunc("/subject", handlePageSubject) - mux.HandleFunc("/edit", handlePageEdit) + mux.HandleFunc("/home", handleView) + mux.HandleFunc("/subject", handleView) + mux.HandleFunc("/edit", handleView) mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - if r.URL.Path == "/" { + if r.URL.Path == "/sw.js" { + http.ServeFile(w, r, filepath.Join(mfs.Dirs["web"], "/sw.js")) + } else if r.URL.Path == "/" { http.Redirect(w, r, "/home", http.StatusMovedPermanently) return } else { @@ -38,43 +38,11 @@ func makeServerRoutes() { }) } -func handlePageHome(w http.ResponseWriter, r *http.Request) { +func handleView(w http.ResponseWriter, r *http.Request) { - // Collect HTML template files from directory. _rPkg, _err := mfs.MakeDirectoryPkg(filepath.Join(mfs.Dirs["web"], r.URL.String()), mfs.SEARCH_DEPTH_ZERO, "*.html") if _err != nil { - log.Printf("_ERROR:network:handlePage(\"/home\"):%s", _err) - http.NotFound(w, r) - return - } - - // Add 'global' file dependencies. - _rPkg.DependencyFiles = append(_rPkg.DependencyFiles, mfs.WebGlobalPkg.DependencyFiles...) - - // Create template from package. - _tmpl, _err := template.ParseFiles(_rPkg.DependencyFiles...) - if _err != nil { - log.Printf("_ERROR:network:handlePage(\"/home\"):%s", _err) - http.Error(w, "_ERROR:network:handlePage(\"/home\"):template.ParseFiles", http.StatusInternalServerError) - return - } - - // Render template. - if _err = _tmpl.ExecuteTemplate(w, "index.html", nil); _err != nil { - log.Printf("_ERROR:network:handlePage(\"/home\"):%s", _err) - http.Error(w, "_ERROR:network:handlePage(\"/home\"):template.ExecuteTemplate", http.StatusInternalServerError) - return - } - - log.Printf("DONE:handlePage:%s", r.URL) -} - -func handlePageSubject(w http.ResponseWriter, r *http.Request) { - - // Collect HTML template files from directory. - _rPkg, _err := mfs.MakeDirectoryPkg(filepath.Join(mfs.Dirs["web"], r.URL.Path), mfs.SEARCH_DEPTH_ZERO, "*.html") - if _err != nil { - log.Printf("_ERROR:network:handlePage(\"/subject\"):%s", _err) + log.Printf("_ERROR: network: handleView(\"/view\"): %s", _err) http.NotFound(w, r) return } @@ -85,48 +53,17 @@ func handlePageSubject(w http.ResponseWriter, r *http.Request) { // Create template from package. _tmpl, _err := template.ParseFiles(_rPkg.DependencyFiles...) if _err != nil { - log.Printf("_ERROR:network:handlePage(\"/subject\"):%s", _err) - http.Error(w, "_ERROR:network:handlePage(\"/subject\"):template.ParseFiles", http.StatusInternalServerError) + _errStr := fmt.Sprintf("_ERROR: network: handleView(\"%s\"): template.ParseFiles: %s", r.URL.String(), _err) + log.Println(_errStr) + http.Error(w, _errStr, http.StatusInternalServerError) return } // Render template. if _err = _tmpl.ExecuteTemplate(w, "index.html", nil); _err != nil { - log.Printf("_ERROR:network:handlePage(\"/subject\"):%s", _err) - http.Error(w, "_ERROR:network:handlePage(\"/subject\"):template.ExecuteTemplate", http.StatusInternalServerError) + _errStr := fmt.Sprintf("_ERROR: network: handleView(\"%s\"): template.ExecuteTemplate: %s", r.URL.String(), _err) + log.Println(_errStr) + http.Error(w, _errStr, http.StatusInternalServerError) return } - - log.Printf("DONE:handlePage:%s", r.URL) -} - -func handlePageEdit(w http.ResponseWriter, r *http.Request) { - - // Collect HTML template files from directory. - _rPkg, _err := mfs.MakeDirectoryPkg(filepath.Join(mfs.Dirs["web"], r.URL.Path), mfs.SEARCH_DEPTH_ZERO, "*.html") - if _err != nil { - log.Printf("_ERROR:network:handlePage(\"/edit\"):%s", _err) - http.NotFound(w, r) - return - } - - // Add 'global' file dependencies. - _rPkg.DependencyFiles = append(_rPkg.DependencyFiles, mfs.WebGlobalPkg.DependencyFiles...) - - // Create template from package. - _tmpl, _err := template.ParseFiles(_rPkg.DependencyFiles...) - if _err != nil { - log.Printf("_ERROR:network:handlePage(\"/edit\"):%s", _err) - http.Error(w, "_ERROR:network:handlePage(\"/edit\"):template.ParseFiles", http.StatusInternalServerError) - return - } - - // Render template. - if _err = _tmpl.ExecuteTemplate(w, "index.html", nil); _err != nil { - log.Printf("_ERROR:network:handlePage(\"/edit\"):%s", _err) - http.Error(w, "_ERROR:network:handlePage(\"/edit\"):template.ExecuteTemplate", http.StatusInternalServerError) - return - } - - log.Printf("DONE:handlePage:%s", r.URL) } diff --git a/web/global/favicon.ico b/web/icons/favicon.ico Binary files differ.