miki

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

commit e5ecb47816ac841865a277164bd001330329c84e
parent 6e70eb2a5289e0e2d1a6b7e4bc7ad30d7c1279fe
Author: weetat <wtenison@protonmail.com>
Date:   Mon, 24 Feb 2025 17:56:11 +0000

Switch config from YAML to sys/config.json

Both packages now read their settings from a JSON file, so the go-yaml
dependency is gone.

Diffstat:
Mgo.mod | 2--
Mgo.sum | 2--
Msrc/filesystem/filesystem.go | 50++++++++++++++++++++++----------------------------
Msrc/network/network.go | 50++++++++++++++++++++++++++++++++------------------
Mweb/edit/index.html | 4++--
Mweb/home/index.html | 2+-
6 files changed, 57 insertions(+), 53 deletions(-)

diff --git a/go.mod b/go.mod @@ -1,5 +1,3 @@ module miki go 1.23.2 - -require github.com/goccy/go-yaml v1.15.17 diff --git a/go.sum b/go.sum @@ -1,2 +0,0 @@ -github.com/goccy/go-yaml v1.15.17 h1:dK4FbbTTEOZTLH/NW3/xBqg0JdC14YKVmYwS9GT3H60= -github.com/goccy/go-yaml v1.15.17/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA= diff --git a/src/filesystem/filesystem.go b/src/filesystem/filesystem.go @@ -1,21 +1,19 @@ package filesystem import ( - "bytes" + "encoding/json" "fmt" "log" "os" "path/filepath" "regexp" - - "github.com/goccy/go-yaml" ) var FilePatternHTML = regexp.MustCompile("^([a-zA-Z0-9.]+).html$") -var Paths struct { - RootDirectory string - WebDirectory string +var Dirs struct { + DataDirectory string `json:"data-directory"` + WebDirectory string `json:"web-directory"` } var GlobalPkg DirectoryPackage @@ -96,38 +94,34 @@ func MakeDirectoryPkg(resSig DirectorySignature) (DirectoryPackage, error) { return _resPkg, nil } -func GetYamlContent(yamlObjectId string, dataContainerReference any) { +func Init() { - _byteBuffer := bytes.NewBufferString(yamlObjectId) + //// PULL SYSTEM INFO FROM JSON - _decoder := yaml.NewDecoder(_byteBuffer, yaml.ReferenceDirs("sys")) - if _err := _decoder.Decode(dataContainerReference); _err != nil { - // NOTE: Not a sustainable solution for error handling long-term. - // TODO: Implement error handling to gracefully exit during runtime. - log.Panicln("__PANIC\tGetYamlContent:" + _err.Error()) + _fsData, _err := os.ReadFile("sys/config.json") + if _err != nil { + log.Panicln("__PANIC\tos.ReadFile:config.json:" + _err.Error()) + return } - log.Printf("DONE\tGetYamlContent:%v\n", dataContainerReference) -} - -func Init() { - - //// PULL SYSTEM INFO FROM YAML + var _fsDataStruct struct { + DataDirectory string `json:"data-directory"` + WebDirectory string `json:"web-directory"` + } - var _mfilesys struct { - FileSystemInfo struct { - RootDir string - WebDir string - } + if _err = json.Unmarshal(_fsData, &_fsDataStruct); _err != nil { + log.Panicln("__PANIC\tjson.Unmarshal:config.json:" + _err.Error()) + return } - GetYamlContent("filesysteminfo: *fs\n", &_mfilesys) - Paths.RootDirectory = _mfilesys.FileSystemInfo.RootDir - Paths.WebDirectory = _mfilesys.FileSystemInfo.WebDir + Dirs.DataDirectory = _fsDataStruct.DataDirectory + Dirs.WebDirectory = _fsDataStruct.WebDirectory + + log.Printf("INFO\tfilesystem:Init:Dirs:%v\n", Dirs) //// GET GLOBAL DIRECTORY RESOURCES - _globalSig, _err := MakeDirectorySig(filepath.Join(Paths.WebDirectory, "/global"), 0, FilePatternHTML) + _globalSig, _err := MakeDirectorySig(filepath.Join(Dirs.WebDirectory, "/global"), 0, FilePatternHTML) if _err != nil { log.Panicf("__PANIC\tnetwork:Init:MakeDirectorySig:%s", _err) } diff --git a/src/network/network.go b/src/network/network.go @@ -1,6 +1,7 @@ package network import ( + "encoding/json" "fmt" "log" "net/http" @@ -23,7 +24,7 @@ var mux *http.ServeMux func pageHandler(w http.ResponseWriter, r *http.Request) { // Produce URL path to directory relation. - _r, _err := mfs.MakeDirectorySig(filepath.Join(mfs.Paths.WebDirectory, r.URL.String()), 0, mfs.FilePatternHTML) + _r, _err := mfs.MakeDirectorySig(filepath.Join(mfs.Dirs.WebDirectory, r.URL.String()), 0, mfs.FilePatternHTML) if _err != nil { log.Printf("_ERROR\tHandleFunc(\"/\"):MakeResourceSig():%s\n", _err) http.NotFound(w, r) @@ -59,29 +60,42 @@ func pageHandler(w http.ResponseWriter, r *http.Request) { func Init() { //// GET SERVER INFO + _siData, _err := os.ReadFile("sys/config.json") + if _err != nil { + log.Panicln("__PANIC\tos.ReadFile:config.json:" + _err.Error()) + return + } - var _msrvinfo struct { - ServerInfo struct { - Port string - SslPath string - CertFile string - KeyFile string - } + type ServerInfo struct { + Port string `json:"port"` + SslPath string `json:"ssl-path"` + CertFile string `json:"cert-file"` + KeyFile string `json:"key-file"` + } + var _siDataStruct struct { + ServerInfo ServerInfo `json:"server-info"` } - mfs.GetYamlContent("serverinfo: *si\n", &_msrvinfo) - serverInfo.port = _msrvinfo.ServerInfo.Port - serverInfo.sslPath = _msrvinfo.ServerInfo.SslPath - serverInfo.certFile = filepath.Join(serverInfo.sslPath, _msrvinfo.ServerInfo.CertFile) - serverInfo.keyFile = filepath.Join(serverInfo.sslPath, _msrvinfo.ServerInfo.KeyFile) + + if _err = json.Unmarshal(_siData, &_siDataStruct); _err != nil { + log.Panicln("__PANIC\tjson.Unmarshal:config.json:" + _err.Error()) + return + } + + serverInfo.port = _siDataStruct.ServerInfo.Port + serverInfo.sslPath = _siDataStruct.ServerInfo.SslPath + serverInfo.certFile = filepath.Join(serverInfo.sslPath, _siDataStruct.ServerInfo.CertFile) + serverInfo.keyFile = filepath.Join(serverInfo.sslPath, _siDataStruct.ServerInfo.KeyFile) + + log.Printf("INFO\tnetwork:Init:ServerInfo:%v\n", serverInfo) //// CHECK FOR VALID CERT INFO - _tls_cert_path := filepath.Join(_msrvinfo.ServerInfo.SslPath, _msrvinfo.ServerInfo.CertFile) + _tls_cert_path := filepath.Join(_siDataStruct.ServerInfo.SslPath, _siDataStruct.ServerInfo.CertFile) if _, err := os.Stat(_tls_cert_path); err != nil { log.Panicf("__PANIC\tmain:%s", err) } - _tls_key_path := filepath.Join(_msrvinfo.ServerInfo.SslPath, _msrvinfo.ServerInfo.KeyFile) + _tls_key_path := filepath.Join(_siDataStruct.ServerInfo.SslPath, _siDataStruct.ServerInfo.KeyFile) if _, err := os.Stat(_tls_key_path); err != nil { log.Panicf("__PANIC\tmain:%s", err) } @@ -90,11 +104,11 @@ func Init() { mux = http.NewServeMux() - mux.Handle("/web/", http.StripPrefix("/web/", http.FileServer(http.Dir(mfs.Paths.WebDirectory)))) + mux.Handle("/web/", http.StripPrefix("/web/", http.FileServer(http.Dir(mfs.Dirs.WebDirectory)))) mux.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) { // log.Printf("CALL\tmain:mux.HandleFunc:%s\n", r.URL) - http.ServeFile(w, r, filepath.Join(mfs.Paths.WebDirectory, "/global/favicon.ico")) + http.ServeFile(w, r, filepath.Join(mfs.Dirs.WebDirectory, "/global/favicon.ico")) // log.Printf("DONE\tmain:mux.HandleFunc:%s\n", r.URL) }) @@ -118,6 +132,6 @@ func Exec() { //// RUN SERVER if _err := http.ListenAndServeTLS(":"+serverInfo.port, serverInfo.certFile, serverInfo.keyFile, mux); _err != nil { - fmt.Printf("_ERROR\tmain():ListenAndServeTLS:%s\n", _err) + fmt.Printf("_ERROR\tnetwork:Exec:ListenAndServeTLS:%s\n", _err) } } diff --git a/web/edit/index.html b/web/edit/index.html @@ -10,13 +10,13 @@ <body> - {{template "header" "EDIT"}} + {{template "header" "Edit"}} <section class="_content"> <section id="left"></section> - <section id=""> + <section id="main"> </section> diff --git a/web/home/index.html b/web/home/index.html @@ -10,7 +10,7 @@ <body> - {{template "header" "HOME"}} + {{template "header" "Home"}} <section class="_content">