Compare commits

..

No commits in common. "28438725c7e978f4fdb2072abd47c5a3443dcc98" and "868d2a7bbd9fe2daa4bee3fd40b4224cef93fef8" have entirely different histories.

12 changed files with 19 additions and 30 deletions

1
.gitignore vendored
View file

@ -2,4 +2,3 @@ main
saved_notes
static
conf.toml
gonotes

View file

@ -1,5 +0,0 @@
all: build
build:
go run cmd/fetch-static/main.go
go build -o gonotes cmd/server/main.go

View file

@ -1,7 +0,0 @@
package main
import "forgejo.gwairfelin.com/max/gonotes/internal/conf"
func main() {
conf.FetchAssets()
}

View file

@ -26,7 +26,10 @@ func main() {
router.Handle(
"/static/",
logger(
http.FileServer(http.FS(conf.Static)),
http.StripPrefix(
"/static",
http.FileServer(http.Dir(conf.Conf.Static.Dir)),
),
),
)

View file

@ -3,5 +3,8 @@ notesdir = "/var/lib/gonotes/saved_notes"
address = ":8080"
protocol = "tcp"
[templates]
dir = "/usr/share/gonotes/templates"
[static]
dir = "/var/www/gonotes/static"

View file

@ -1,7 +1,6 @@
package conf
import (
"embed"
"errors"
"io"
"log"
@ -18,7 +17,7 @@ type Asset struct {
Url string
}
func (asset *Asset) fetchIfNotExists(staticPath string) {
func (asset *Asset) FetchIfNotExists(staticPath string) {
destPath := filepath.Join(staticPath, asset.Path)
err := os.MkdirAll(path.Dir(destPath), os.FileMode(0750))
@ -58,9 +57,13 @@ type Config struct {
Protocol string
Extension string
NotesDir string
Static struct {
Templates struct {
Dir string
}
Static struct {
Dir string
Root string
}
}
var (
@ -72,10 +75,6 @@ var (
{Path: "icons/eye.svg", Url: "https://raw.githubusercontent.com/twbs/icons/refs/heads/main/icons/eye.svg"},
}
BaseTemplate string = "base.tmpl.html"
//go:embed static/css/* static/icons/* static/js/*
Static embed.FS
//go:embed templates/*
Templates embed.FS
)
func LoadConfig(path string) {
@ -90,10 +89,8 @@ func LoadConfig(path string) {
if err != nil {
log.Fatal(err)
}
}
func FetchAssets() {
for _, asset := range assets {
asset.fetchIfNotExists("./internal/conf/static")
asset.FetchIfNotExists(Conf.Static.Dir)
}
}

View file

View file

@ -3,6 +3,7 @@ package templ
import (
"html/template"
"net/http"
"os"
"path/filepath"
"forgejo.gwairfelin.com/max/gonotes/internal/conf"
@ -12,19 +13,17 @@ type Ctx map[string]any
func RenderTemplate(w http.ResponseWriter, tmpl string, context any) error {
files := []string{
filepath.Join("templates", "base.tmpl.html"),
filepath.Join("templates", tmpl),
filepath.Join(conf.Conf.Templates.Dir, conf.BaseTemplate),
filepath.Join(conf.Conf.Templates.Dir, tmpl),
}
for _, f := range files {
file, err := conf.Templates.Open(f)
_, err := os.Stat(f)
if err != nil {
return err
}
file.Close()
}
t, err := template.ParseFS(conf.Templates, files...)
t, err := template.ParseFiles(files...)
t.ExecuteTemplate(w, "base", context)
return err
}