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 ## TODO
* handle static urls in the django url mapping style * handle static urls in the django url mapping style
* Write a delete view
* Style up the templates better * Style up the templates better
* Serve frontend dependencies from /static/
* Populate frontend dependencies automagically?

View file

@ -1,6 +1,7 @@
package main package main
import ( import (
"fmt"
"log" "log"
"net/http" "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 { func logger(next http.Handler) http.Handler {

View file

@ -1,5 +1,6 @@
extension = "md" extension = "md"
notesdir = "saved_notes" notesdir = "saved_notes"
port = 8080
[templates] [templates]
dir = "templates" dir = "templates"
@ -8,3 +9,8 @@ base = "base.tmpl.html"
[static] [static]
dir = "/home/max/src/gonotes/static" dir = "/home/max/src/gonotes/static"
root = "/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 package conf
import ( import (
"errors"
"io"
"log" "log"
"net/http"
"os" "os"
"path/filepath"
"github.com/pelletier/go-toml/v2" "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 { type Config struct {
Port int
Extension string Extension string
NotesDir string NotesDir string
Templates struct { Templates struct {
@ -15,8 +54,9 @@ type Config struct {
Base string Base string
} }
Static struct { Static struct {
Dir string Dir string
Root string Root string
Assets []Asset
} }
} }
@ -34,4 +74,8 @@ func LoadConfig(path string) {
if err != nil { if err != nil {
log.Fatal(err) 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 charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<title>{{template "title" .}}</title> <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" rel="stylesheet"
integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH"
crossorigin="anonymous"> crossorigin="anonymous">

View file

@ -1,7 +1,7 @@
{{define "title"}}Edit {{.note.Title}}{{end}} {{define "title"}}Edit {{.note.Title}}{{end}}
{{define "main"}} {{define "main"}}
<form action="{{.urlEdit}}" method="POST"> <form action="{{.urlSave}}" method="POST">
<div class="mb-3"> <div class="mb-3">
<label for="noteBodyInput" class="form-label">Note</label> <label for="noteBodyInput" class="form-label">Note</label>
<div class="border rounded rounded-1"> <div class="border rounded rounded-1">
@ -13,11 +13,11 @@
<button class="btn btn-primary" type="submit">Save</button> <button class="btn btn-primary" type="submit">Save</button>
</form> </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 <link
rel="stylesheet" rel="stylesheet"
type="text/css" 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"> <script type="text/javascript">
var tinyMDE = new TinyMDE.Editor({ textarea: "noteBodyInput" }); var tinyMDE = new TinyMDE.Editor({ textarea: "noteBodyInput" });