Compare commits
No commits in common. "28438725c7e978f4fdb2072abd47c5a3443dcc98" and "868d2a7bbd9fe2daa4bee3fd40b4224cef93fef8" have entirely different histories.
28438725c7
...
868d2a7bbd
12 changed files with 19 additions and 30 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -2,4 +2,3 @@ main
|
||||||
saved_notes
|
saved_notes
|
||||||
static
|
static
|
||||||
conf.toml
|
conf.toml
|
||||||
gonotes
|
|
||||||
|
|
|
||||||
5
Makefile
5
Makefile
|
|
@ -1,5 +0,0 @@
|
||||||
all: build
|
|
||||||
|
|
||||||
build:
|
|
||||||
go run cmd/fetch-static/main.go
|
|
||||||
go build -o gonotes cmd/server/main.go
|
|
||||||
|
|
@ -1,7 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import "forgejo.gwairfelin.com/max/gonotes/internal/conf"
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
conf.FetchAssets()
|
|
||||||
}
|
|
||||||
|
|
@ -26,7 +26,10 @@ func main() {
|
||||||
router.Handle(
|
router.Handle(
|
||||||
"/static/",
|
"/static/",
|
||||||
logger(
|
logger(
|
||||||
http.FileServer(http.FS(conf.Static)),
|
http.StripPrefix(
|
||||||
|
"/static",
|
||||||
|
http.FileServer(http.Dir(conf.Conf.Static.Dir)),
|
||||||
|
),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,5 +3,8 @@ notesdir = "/var/lib/gonotes/saved_notes"
|
||||||
address = ":8080"
|
address = ":8080"
|
||||||
protocol = "tcp"
|
protocol = "tcp"
|
||||||
|
|
||||||
|
[templates]
|
||||||
|
dir = "/usr/share/gonotes/templates"
|
||||||
|
|
||||||
[static]
|
[static]
|
||||||
dir = "/var/www/gonotes/static"
|
dir = "/var/www/gonotes/static"
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
package conf
|
package conf
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"embed"
|
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
|
|
@ -18,7 +17,7 @@ type Asset struct {
|
||||||
Url string
|
Url string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (asset *Asset) fetchIfNotExists(staticPath string) {
|
func (asset *Asset) FetchIfNotExists(staticPath string) {
|
||||||
destPath := filepath.Join(staticPath, asset.Path)
|
destPath := filepath.Join(staticPath, asset.Path)
|
||||||
|
|
||||||
err := os.MkdirAll(path.Dir(destPath), os.FileMode(0750))
|
err := os.MkdirAll(path.Dir(destPath), os.FileMode(0750))
|
||||||
|
|
@ -58,9 +57,13 @@ type Config struct {
|
||||||
Protocol string
|
Protocol string
|
||||||
Extension string
|
Extension string
|
||||||
NotesDir string
|
NotesDir string
|
||||||
Static struct {
|
Templates struct {
|
||||||
Dir string
|
Dir string
|
||||||
}
|
}
|
||||||
|
Static struct {
|
||||||
|
Dir string
|
||||||
|
Root string
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|
@ -72,10 +75,6 @@ var (
|
||||||
{Path: "icons/eye.svg", Url: "https://raw.githubusercontent.com/twbs/icons/refs/heads/main/icons/eye.svg"},
|
{Path: "icons/eye.svg", Url: "https://raw.githubusercontent.com/twbs/icons/refs/heads/main/icons/eye.svg"},
|
||||||
}
|
}
|
||||||
BaseTemplate string = "base.tmpl.html"
|
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) {
|
func LoadConfig(path string) {
|
||||||
|
|
@ -90,10 +89,8 @@ func LoadConfig(path string) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
func FetchAssets() {
|
|
||||||
for _, asset := range assets {
|
for _, asset := range assets {
|
||||||
asset.fetchIfNotExists("./internal/conf/static")
|
asset.FetchIfNotExists(Conf.Static.Dir)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
0
internal/conf/static/.gitignore
vendored
0
internal/conf/static/.gitignore
vendored
|
|
@ -3,6 +3,7 @@ package templ
|
||||||
import (
|
import (
|
||||||
"html/template"
|
"html/template"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"forgejo.gwairfelin.com/max/gonotes/internal/conf"
|
"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 {
|
func RenderTemplate(w http.ResponseWriter, tmpl string, context any) error {
|
||||||
files := []string{
|
files := []string{
|
||||||
filepath.Join("templates", "base.tmpl.html"),
|
filepath.Join(conf.Conf.Templates.Dir, conf.BaseTemplate),
|
||||||
filepath.Join("templates", tmpl),
|
filepath.Join(conf.Conf.Templates.Dir, tmpl),
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, f := range files {
|
for _, f := range files {
|
||||||
file, err := conf.Templates.Open(f)
|
_, err := os.Stat(f)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
file.Close()
|
|
||||||
}
|
}
|
||||||
|
t, err := template.ParseFiles(files...)
|
||||||
t, err := template.ParseFS(conf.Templates, files...)
|
|
||||||
t.ExecuteTemplate(w, "base", context)
|
t.ExecuteTemplate(w, "base", context)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue