Download templates on boot

This commit is contained in:
Maximilian Friedersdorff 2025-02-04 20:27:32 +00:00
parent 30339c6b16
commit 3ad3666002
7 changed files with 59 additions and 11 deletions

View file

@ -9,7 +9,4 @@ It's not shared in the sense of simultaneous editing, don't do that!
## TODO
* handle static urls in the django url mapping style
* Write a delete view
* Style up the templates better
* Serve frontend dependencies from /static/
* Populate frontend dependencies automagically?

View file

@ -1,6 +1,7 @@
package main
import (
"fmt"
"log"
"net/http"
@ -24,7 +25,8 @@ func main() {
),
),
)
log.Fatal(http.ListenAndServe(":8080", router))
addr := fmt.Sprintf(":%d", conf.Conf.Port)
log.Fatal(http.ListenAndServe(addr, router))
}
func logger(next http.Handler) http.Handler {

View file

@ -1,5 +1,6 @@
extension = "md"
notesdir = "saved_notes"
port = 8080
[templates]
dir = "templates"
@ -8,3 +9,8 @@ base = "base.tmpl.html"
[static]
dir = "/home/max/src/gonotes/static"
root = "/static/"
assets = [
{ path = "css/bootstrap.min.css", url = "https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" },
{ path = "css/tiny-mde.min.css", url = "https://unpkg.com/tiny-markdown-editor/dist/tiny-mde.min.css" },
{ path = "js/tiny-mde.min.js", url = "https://unpkg.com/tiny-markdown-editor/dist/tiny-mde.min.js" },
]

View file

@ -1,13 +1,52 @@
package conf
import (
"errors"
"io"
"log"
"net/http"
"os"
"path/filepath"
"github.com/pelletier/go-toml/v2"
)
type Asset struct {
Path string
Url string
}
func (asset *Asset) FetchIfNotExists(staticPath string) {
destPath := filepath.Join(staticPath, asset.Path)
out, err := os.OpenFile(
destPath,
os.O_WRONLY|os.O_CREATE|os.O_EXCL,
0666,
)
if err != nil {
if errors.Is(err, os.ErrExist) {
log.Printf("%s already exists\n", destPath)
return
}
panic(err)
}
defer out.Close()
resp, err := http.Get(asset.Url)
if err != nil {
panic(err)
}
defer resp.Body.Close()
_, err = io.Copy(out, resp.Body)
if err != nil {
panic(err)
}
}
type Config struct {
Port int
Extension string
NotesDir string
Templates struct {
@ -17,6 +56,7 @@ type Config struct {
Static struct {
Dir string
Root string
Assets []Asset
}
}
@ -34,4 +74,8 @@ func LoadConfig(path string) {
if err != nil {
log.Fatal(err)
}
for _, asset := range Conf.Static.Assets {
asset.FetchIfNotExists(Conf.Static.Dir)
}
}

1
static/.gitignore vendored
View file

@ -1 +0,0 @@
*

View file

@ -5,7 +5,7 @@
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{{template "title" .}}</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css"
<link href="/static/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH"
crossorigin="anonymous">

View file

@ -1,7 +1,7 @@
{{define "title"}}Edit {{.note.Title}}{{end}}
{{define "main"}}
<form action="{{.urlEdit}}" method="POST">
<form action="{{.urlSave}}" method="POST">
<div class="mb-3">
<label for="noteBodyInput" class="form-label">Note</label>
<div class="border rounded rounded-1">
@ -13,11 +13,11 @@
<button class="btn btn-primary" type="submit">Save</button>
</form>
<script src="https://unpkg.com/tiny-markdown-editor/dist/tiny-mde.min.js"></script>
<script src="/static/js/tiny-mde.min.js"></script>
<link
rel="stylesheet"
type="text/css"
href="https://unpkg.com/tiny-markdown-editor/dist/tiny-mde.min.css"
href="/static/css/tiny-mde.min.css"
/>
<script type="text/javascript">
var tinyMDE = new TinyMDE.Editor({ textarea: "noteBodyInput" });