Embed templates and static files
This commit is contained in:
parent
868d2a7bbd
commit
8edd857d22
11 changed files with 31 additions and 17 deletions
|
|
@ -1,6 +1,7 @@
|
|||
package conf
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"errors"
|
||||
"io"
|
||||
"log"
|
||||
|
|
@ -17,7 +18,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))
|
||||
|
|
@ -57,13 +58,9 @@ type Config struct {
|
|||
Protocol string
|
||||
Extension string
|
||||
NotesDir string
|
||||
Templates struct {
|
||||
Static struct {
|
||||
Dir string
|
||||
}
|
||||
Static struct {
|
||||
Dir string
|
||||
Root string
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
|
|
@ -75,6 +72,10 @@ 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/*
|
||||
Static embed.FS
|
||||
//go:embed templates/*
|
||||
Templates embed.FS
|
||||
)
|
||||
|
||||
func LoadConfig(path string) {
|
||||
|
|
@ -89,8 +90,10 @@ func LoadConfig(path string) {
|
|||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func FetchAssets() {
|
||||
for _, asset := range assets {
|
||||
asset.FetchIfNotExists(Conf.Static.Dir)
|
||||
asset.fetchIfNotExists("./internal/conf/static")
|
||||
}
|
||||
}
|
||||
|
|
|
|||
0
internal/conf/static/.gitignore
vendored
Normal file
0
internal/conf/static/.gitignore
vendored
Normal file
36
internal/conf/templates/base.tmpl.html
Normal file
36
internal/conf/templates/base.tmpl.html
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
{{define "base"}}
|
||||
<!doctype html>
|
||||
<html lang='en'>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>{{template "title" .}}</title>
|
||||
<link href="/static/css/bootstrap.min.css"
|
||||
rel="stylesheet"
|
||||
integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH"
|
||||
crossorigin="anonymous">
|
||||
</head>
|
||||
<body>
|
||||
<nav class="navbar navbar-expand-sm bg-body-tertiary mb-3">
|
||||
<div class="container-fluid">
|
||||
<a class="navbar-brand" href="#">GoNotes</a>
|
||||
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="/notes/">All Notes</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</nav>
|
||||
<div class="container">
|
||||
<div class="row justify-content-md-center">
|
||||
<div class="col-md-6">
|
||||
<h1>{{template "title" .}}</h1>
|
||||
<main>
|
||||
{{template "main" .}}
|
||||
</main>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
{{end}}
|
||||
32
internal/conf/templates/edit.tmpl.html
Normal file
32
internal/conf/templates/edit.tmpl.html
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
{{define "title"}}Edit {{.note.Title}}{{end}}
|
||||
|
||||
{{define "main"}}
|
||||
<form action="{{.urlSave}}" method="POST">
|
||||
<div class="mb-3">
|
||||
<input type="text" class="form-control" id="noteTitleInput" name="title" aria-described-by="titleHelp" value="{{.note.Title}}"/>
|
||||
<div id="titleHelp" class="form-text">Enter your note title</div>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<div class="border rounded rounded-1">
|
||||
<div id="toolbar"></div>
|
||||
<textarea class="form-control" id="noteBodyInput" name="body" aria-described-by="bodyHelp">{{.text}}</textarea>
|
||||
</div>
|
||||
<div id="bodyHelp" class="form-text">Enter your note content in markdown</div>
|
||||
</div>
|
||||
<button class="btn btn-primary" type="submit">Save</button>
|
||||
</form>
|
||||
|
||||
<script src="/static/js/tiny-mde.min.js"></script>
|
||||
<link
|
||||
rel="stylesheet"
|
||||
type="text/css"
|
||||
href="/static/css/tiny-mde.min.css"
|
||||
/>
|
||||
<script type="text/javascript">
|
||||
var tinyMDE = new TinyMDE.Editor({ textarea: "noteBodyInput" });
|
||||
var commandBar = new TinyMDE.CommandBar({
|
||||
element: "toolbar",
|
||||
editor: tinyMDE,
|
||||
});
|
||||
</script>
|
||||
{{end}}
|
||||
12
internal/conf/templates/list.tmpl.html
Normal file
12
internal/conf/templates/list.tmpl.html
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
{{define "title"}}All Notes{{end}}
|
||||
{{define "main"}}
|
||||
<div class="list-group list-group-flush">
|
||||
{{range $title := .titles}}
|
||||
<a class="list-group-item list-group-item-action d-flex justify-content-between"
|
||||
href="{{$title}}/">
|
||||
<span>{{$title}}</span>
|
||||
<img src="/static/icons/eye.svg" alt="Edit">
|
||||
</a>
|
||||
{{end}}
|
||||
</div>
|
||||
{{end}}
|
||||
11
internal/conf/templates/view.tmpl.html
Normal file
11
internal/conf/templates/view.tmpl.html
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
{{define "title"}}{{.note.Title}}{{end}}
|
||||
{{define "main"}}
|
||||
<div>
|
||||
{{.note.BodyRendered}}
|
||||
</div>
|
||||
<div class="d-flex justify-content-between">
|
||||
<a class="btn btn-primary" href="{{.urlEdit}}">Edit</a>
|
||||
<a class="btn btn-danger" href="{{.urlDelete}}">Delete</a>
|
||||
</div>
|
||||
{{end}}
|
||||
|
||||
|
|
@ -3,7 +3,6 @@ package templ
|
|||
import (
|
||||
"html/template"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"forgejo.gwairfelin.com/max/gonotes/internal/conf"
|
||||
|
|
@ -13,17 +12,19 @@ type Ctx map[string]any
|
|||
|
||||
func RenderTemplate(w http.ResponseWriter, tmpl string, context any) error {
|
||||
files := []string{
|
||||
filepath.Join(conf.Conf.Templates.Dir, conf.BaseTemplate),
|
||||
filepath.Join(conf.Conf.Templates.Dir, tmpl),
|
||||
filepath.Join("templates", "base.tmpl.html"),
|
||||
filepath.Join("templates", tmpl),
|
||||
}
|
||||
|
||||
for _, f := range files {
|
||||
_, err := os.Stat(f)
|
||||
file, err := conf.Templates.Open(f)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
file.Close()
|
||||
}
|
||||
t, err := template.ParseFiles(files...)
|
||||
|
||||
t, err := template.ParseFS(conf.Templates, files...)
|
||||
t.ExecuteTemplate(w, "base", context)
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue