sophuwu.site > myweb
got it back in working order
sophuwu sophie@skisiel.com
Sat, 16 Nov 2024 14:18:54 +0100
commit

9b9c8c38c23da81575f60a083939aee549a3e096

parent

d1c28596805603653e4a4eb511ffec910a1bb529

4 files changed, 66 insertions(+), 28 deletions(-)

jump to
M .gitignore.gitignore

@@ -1,1 +1,3 @@

build/ +webhome/*.sqlite +webhome/media/*
M config/config.goconfig/config.go

@@ -1,35 +1,65 @@

package config import ( - yaml "gopkg.in/yaml.v3" + "bytes" "log" "os" + "path/filepath" ) var ( - ListenAddr string - TemplateDir string - StaticDir string - MediaDir string - DBPath string - Email string - Name string - URL string + ListenAddr string + WebRoot string + DbPath string + StaticPath string + MediaPath string + Templates string + Email string + Name string + URL string ) -var m map[string]string +func path(p string) string { + return filepath.Join(WebRoot, p) +} func init() { - var path string = "config.yaml" - if len(os.Args) > 1 { - path = os.Args[1] + if len(os.Args) < 2 { + log.Fatalf("Usage: %s <config file>", os.Args[0]) } - file, err := os.ReadFile(path) + file, err := os.ReadFile(os.Args[1]) if err != nil { log.Fatalf("Error opening config: %v", err) } - err = yaml.Unmarshal(file, &cfg) - if err != nil { - log.Fatalf("Error parsing config: %v", err) + + var mm = make(map[string]string) + var pos int + + for _, v := range bytes.Split(file, []byte{'\n'}) { + if len(v) == 0 || (len(v) > 0 && v[0] == '#') || func() bool { + pos = bytes.IndexByte(v, '=') + return pos == -1 + }() { + continue + } + mm[string(bytes.TrimSpace(v[:pos]))] = string(bytes.TrimSpace(v[pos+1:])) } + ListenAddr = mm["address"] + ":" + mm["port"] + if ListenAddr[len(ListenAddr)-1] == ':' { + ListenAddr += "8085" + } + + if len(mm["webroot"]) > 0 && mm["webroot"][0] == '/' { + WebRoot = mm["webroot"] + } else { + WebRoot = filepath.Join(filepath.Dir(os.Args[1]), mm["webroot"]) + } + DbPath = path("stuff.sqlite") + StaticPath = path("static") + MediaPath = path("media") + Templates = path("templates/*") + Email = mm["email"] + Name = mm["name"] + URL = mm["url"] + }
M main.gomain.go

@@ -4,23 +4,22 @@ import (

"html/template" "log" "net/http" - "path/filepath" "sophuwu.site/myweb/config" ) var Tplt *template.Template func ParseTemplates() { - Tplt = template.Must(template.ParseGlob(filepath.Join(config.Cfg.Paths.Templates, "*"))) + Tplt = template.Must(template.ParseGlob(config.Templates)) } func HttpIndex(w http.ResponseWriter, r *http.Request) { data := make(map[string]string) data["Title"] = config.Name data["Description"] = "Blogs and projects by " + config.Name + "." - data["Url"] = config.Cfg.Website.Url + r.URL.Path - data["Email"] = config.Cfg.Contact.Email - data["Name"] = config.Cfg.Contact.Name + data["Url"] = config.URL + r.URL.Path + data["Email"] = config.Email + data["Name"] = config.Name if err := Tplt.ExecuteTemplate(w, "index", data); err != nil { log.Println(err)

@@ -28,14 +27,17 @@ return

} } +func HttpFS(path, fspath string) { + http.Handle(path, http.StripPrefix(path, http.FileServer(http.Dir(fspath)))) +} + func main() { ParseTemplates() http.HandleFunc("/", HttpIndex) - - http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir(config.Cfg.Paths.Static)))) - http.Handle("/media/", http.StripPrefix("/media/", http.FileServer(http.Dir(config.Cfg.Paths.Media)))) + HttpFS("/static/", config.StaticPath) + HttpFS("/media/", config.MediaPath) - http.ListenAndServe(config.ListenAddr(), nil) + http.ListenAndServe(config.ListenAddr, nil) }
M webhome/myweb.confwebhome/myweb.conf

@@ -4,11 +4,15 @@

## The address to listen for http requests. address=127.0.0.1 +## The path to the directory containing the website files. +## Absolute or relative to the directory containing this file. +webroot=./ + ## The url for the root of the website. ## This is used to generate URLs for sharing. url=https://skisiel.com ## The name of the person who maintains the website. -Name=Sophie Kisiel +name=Sophie Kisiel ## Their email address. -Email=sophie@skisiel.com +email=sophie@skisiel.com