pull out of older repos

This commit is contained in:
Risotto Bias 2025-01-12 03:47:45 -07:00
parent 2312588717
commit c74c6dd6d1
14 changed files with 235 additions and 1 deletions

21
LICENSE Normal file
View file

@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

17
err_404.go Normal file
View file

@ -0,0 +1,17 @@
package templates
import (
"fmt"
"net/http"
)
func (T Templates) Err404(w http.ResponseWriter, r *http.Request) {
fmt.Println("404: " + r.URL.Path)
e := T.internalNewSplain(
"File Not Found",
"Couldn't find that. Maybe it's us?",
"Let us know if it happens a lot.",
404,
)
T.internalTemplateErrorSplash(w, r, e)
}

17
errsplain.go Normal file
View file

@ -0,0 +1,17 @@
package templates
type ErrorSplain struct {
Title string
Explanation string
Remediation string
Status int
}
func (T Templates) internalNewSplain(title string, explanation string, remediation string, status int) ErrorSplain {
return ErrorSplain{
Title: title,
Explanation: explanation,
Remediation: remediation,
Status: status,
}
}

View file

@ -0,0 +1,19 @@
{{define "base"}}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{.Title}}</title>
<link rel="stylesheet" type="text/css" href="/static/style.css">
</head>
<body>
<header>
{{ template "nav.html" .}}
</header>
<h1>{{.Title}}</h1>
<main>
{{ template "content" .}}
</main>
</body>
</html>

View file

@ -0,0 +1,8 @@
{{template "base" .}}
{{define "content" }}
<p>It seems we encountered a problem...</p>
<h1>{{.Title}}</h1>
<p>{{.Explanation}}</p>
<h2>Possible Resolution:</h2>
<p>{{.Remediation}}</p>
{{end}}

View file

@ -0,0 +1,4 @@
{{template "base" .}}
{{define "content" }}
<p>Hello world</p>
{{end}}

View file

@ -0,0 +1,17 @@
<nav>
<h1 class="left">
<a href="/"><img class="site_logo" src="/static/favicon.png">Demo</a>
</h1>
<ul>
<li><a href="/home">Home</a></li>
{{if not .LoggedIn}}
<li><a href="/auth/login">Login</a></li>
<li><a href="/auth/register">Register</a></li>
<li><a href="/auth/recover">Recover</a></li>
{{ else }}
<li><a href="/settings/passwd">Change Password</a></li>
<li><a href="/settings/email">Change Email</a></li>
<li><a href="/auth/logout">Log Out</a></li>
{{end}}
</ul>
</nav>

29
example_test.go Normal file
View file

@ -0,0 +1,29 @@
package templates_test
import (
"embed"
"fmt"
"io"
"net/http"
"net/http/httptest"
"git.bivouac.wiki/use/templates"
)
//go:embed example_templates
var tpfs embed.FS
func ExampleTemplates() {
a := templates.NewTemplates(tpfs, "example_templates", "base.html", "nav.html")
v := struct{}{}
r := httptest.NewRequest(http.MethodGet, "home.html", nil)
w := httptest.NewRecorder()
a.TemplateEnd("home.html", w, r, v)
res := w.Result()
defer res.Body.Close()
data, err := io.ReadAll(res.Body)
if err != nil {
fmt.Errorf("expected error to be nil got %v", err)
}
fmt.Println(string(data))
}

28
init.go Normal file
View file

@ -0,0 +1,28 @@
package templates
import (
"embed"
"html/template"
)
type Templates map[string]*template.Template
// NewTemplates creates a new responder (embedded_dir should have a trailing slash)
func NewTemplates(given_fs embed.FS, embedded_dir string, variadics ...string) Templates {
// todo later: allow both io/fs.FS and embed.FS as called arguments
templates := make(map[string]*template.Template)
pages, _ := given_fs.ReadDir(embedded_dir)
for _, page := range pages {
file_path := embedded_dir + page.Name()
files := []string{file_path}
for _, v := range variadics {
files = append(files, embedded_dir+v) // add prefix
}
// files = append(files, variadics...)
tpl := template.Must(
template.New(page.Name()).ParseFS(given_fs, files...)) //+".html"))
templates[page.Name()] = tpl
// fmt.Println(page.Name())
}
return templates
}

30
internal_error_splash.go Normal file
View file

@ -0,0 +1,30 @@
package templates
import (
"fmt"
"net/http"
)
func (T Templates) internalTemplateErrorSplash(w http.ResponseWriter, r *http.Request, e ErrorSplain) {
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(e.Status)
// w.WriteHeader(http.StatusInternalServerError)
type v struct {
Title string
LoggedIn bool
Explanation string
Remediation string
}
s := v{
Title: "error",
LoggedIn: false,
Explanation: e.Explanation,
Remediation: e.Remediation,
}
err := T["error.html"].ExecuteTemplate(w, "error.html", s)
if err != nil {
fmt.Println(err)
}
return
}

View file

@ -1 +0,0 @@
package templates

17
new_repl.go Normal file
View file

@ -0,0 +1,17 @@
package templates
import (
"fmt"
"net/http"
)
func (T Templates) NewRepl(err error, w http.ResponseWriter, r *http.Request, title string, explanation string, remediation string) {
e := T.internalNewSplain(
title,
explanation,
remediation,
500,
)
T.internalTemplateErrorSplash(w, r, e)
fmt.Println(err)
}

11
template_end.go Normal file
View file

@ -0,0 +1,11 @@
package templates
import "net/http"
func (T Templates) TemplateEnd(template_name string, w http.ResponseWriter, r *http.Request, v any) {
w.Header().Set("Content-Type", "text/html")
err := T[template_name].ExecuteTemplate(w, template_name, v)
if err != nil {
T.ReplTemplateIssue(err, w, r)
}
}

17
template_issue.go Normal file
View file

@ -0,0 +1,17 @@
package templates
import (
"net/http"
"time"
)
func (T Templates) ReplTemplateIssue(err error, w http.ResponseWriter, r *http.Request) {
T.NewRepl(
err,
w,
r,
"Template problem",
"Something's wrong with our templates.",
"email us this timestamp: "+time.Now().String(),
)
}