Made all the templating work with gin instead

This commit is contained in:
2025-07-28 13:50:10 +01:00
parent 0d373d1990
commit 8b257a3e13
25 changed files with 140 additions and 270 deletions
+4
View File
@@ -0,0 +1,4 @@
# TODO
- [ ] On the categories page, add a column of how many times a given category is
used
- [ ] Make all the other items update/create page be like expenses
+3 -3
View File
@@ -30,7 +30,7 @@
opacity: 1; opacity: 1;
} }
20% { 20% {
opacity: 1.1; opacity: 1;
} }
40% { 40% {
opacity: 1; opacity: 1;
@@ -39,10 +39,10 @@
opacity: 1; opacity: 1;
} }
80% { 80% {
opacity: 1.1; opacity: 1;
} }
100% { 100% {
opacity: 0.9; opacity: 1;
} }
} }
@keyframes beam { @keyframes beam {
+2
View File
@@ -1,3 +1,4 @@
{{ define "categories" }}
<div id="operation-state" class="warning">&nbsp;</div><br> <div id="operation-state" class="warning">&nbsp;</div><br>
<table> <table>
<tr> <tr>
@@ -50,3 +51,4 @@
}); });
}) })
</script> </script>
{{ end }}
+2
View File
@@ -1,3 +1,4 @@
{{ define "category" }}
<table> <table>
<tr> <tr>
<td>ID:</td> <td>ID:</td>
@@ -8,3 +9,4 @@
<td>{{ .category.CategoryName }}</td> <td>{{ .category.CategoryName }}</td>
</tr> </tr>
</table> </table>
{{ end }}
+2
View File
@@ -1,3 +1,4 @@
{{ define "categoryNew" }}
<div> <div>
<form id="category-form" action="submit"> <form id="category-form" action="submit">
<fieldset id="form-box-boundaries"> <fieldset id="form-box-boundaries">
@@ -19,3 +20,4 @@
</fieldset> </fieldset>
</form> </form>
</div> </div>
{{ end }}
+2
View File
@@ -1,3 +1,4 @@
{{ define "overview" }}
<form> <form>
<p> <p>
<label for="range-start">Start date:</label> <label for="range-start">Start date:</label>
@@ -24,3 +25,4 @@
<div id="query-results"> <div id="query-results">
</div> </div>
{{ end }}
+2
View File
@@ -1,3 +1,4 @@
{{ define "overviewResults" }}
{{ range .expenses }} {{ range .expenses }}
<table> <table>
<tr> <tr>
@@ -42,3 +43,4 @@
</tr> </tr>
{{ end }} {{ end }}
</table> </table>
{{ end }}
+2
View File
@@ -1,3 +1,4 @@
{{ define "panic" }}
<code class="warning"> <code class="warning">
{{ .msg }} {{ .msg }}
<br> <br>
@@ -31,3 +32,4 @@
/usr/lib/go/src/runtime/asm_amd64.s:1695 (0x472d80) /usr/lib/go/src/runtime/asm_amd64.s:1695 (0x472d80)
goexit: BYTE $0x90 // NOP goexit: BYTE $0x90 // NOP
</code> </code>
{{ end }}
+12
View File
@@ -0,0 +1,12 @@
{{ define "store" }}
<table>
<tr>
<td>ID:</td>
<td>{{ .store.StoreID }}</td>
</tr>
<tr>
<td>Name:</td>
<td>{{ .store.StoreName }}</td>
</tr>
</table>
{{ end }}
+3 -1
View File
@@ -1,7 +1,8 @@
{{ define "storeNew" }}
<div> <div>
<form id="store-form" action="submit"> <form id="store-form" action="submit">
<fieldset id="form-box-boundaries"> <fieldset id="form-box-boundaries">
<legend>CATEGORY FORM</legend> <legend>STORE FORM</legend>
<div class="form-container form-left"> <div class="form-container form-left">
<div id="form-state" class="warning"></div><br> <div id="form-state" class="warning"></div><br>
@@ -24,3 +25,4 @@
</fieldset> </fieldset>
</form> </form>
</div> </div>
{{ end }}
+2
View File
@@ -1,3 +1,4 @@
{{ define "stores" }}
<div id="operation-state" class="warning">&nbsp;</div><br> <div id="operation-state" class="warning">&nbsp;</div><br>
<table> <table>
<tr> <tr>
@@ -50,3 +51,4 @@
}); });
}) })
</script> </script>
{{ end }}
+22
View File
@@ -1,3 +1,4 @@
{{ define "terminal" }}
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
@@ -53,6 +54,26 @@
{{ template "expenses" . }} {{ template "expenses" . }}
{{ else if eq .content "newExpense" }} {{ else if eq .content "newExpense" }}
{{ template "expense" . }} {{ template "expense" . }}
{{ else if eq .content "panic" }}
{{ template "panic" . }}
{{ else if eq .content "categories" }}
{{ template "categories" . }}
{{ else if eq .content "category" }}
{{ template "category" . }}
{{ else if eq .content "categoryNew" }}
{{ template "categoryNew" . }}
{{ else if eq .content "overview" }}
{{ template "overview" . }}
{{ else if eq .content "stores" }}
{{ template "stores" . }}
{{ else if eq .content "store" }}
{{ template "store" . }}
{{ else if eq .content "storeNew" }}
{{ template "storeNew" . }}
{{ else if eq .content "types" }}
{{ template "types" . }}
{{ else if eq .content "typeNew" }}
{{ template "typeNew" . }}
{{ else }} {{ else }}
<div>Unknown page</div> <div>Unknown page</div>
{{ end }} {{ end }}
@@ -65,3 +86,4 @@
</body> </body>
</html> </html>
{{ end }}
+2
View File
@@ -1,3 +1,4 @@
{{ define "type" }}
<div> <div>
<form id="type-form" action="submit"> <form id="type-form" action="submit">
<fieldset id="form-box-boundaries"> <fieldset id="form-box-boundaries">
@@ -33,3 +34,4 @@
</fieldset> </fieldset>
</form> </form>
</div> </div>
{{ end }}
+2
View File
@@ -1,3 +1,4 @@
{{ define "typeNew" }}
<div> <div>
<form id="type-form" action="submit"> <form id="type-form" action="submit">
<fieldset id="form-box-boundaries"> <fieldset id="form-box-boundaries">
@@ -19,3 +20,4 @@
</fieldset> </fieldset>
</form> </form>
</div> </div>
{{ end }}
+2
View File
@@ -1,3 +1,4 @@
{{ define "types" }}
<div id="operation-state" class="warning">&nbsp;</div><br> <div id="operation-state" class="warning">&nbsp;</div><br>
<table> <table>
<tr> <tr>
@@ -50,3 +51,4 @@
}); });
}) })
</script> </script>
{{ end }}
+13 -3
View File
@@ -3,12 +3,13 @@ package main
import ( import (
"expenses/config" "expenses/config"
"expenses/pages" "expenses/pages"
"expenses/templating"
"fmt"
"html/template" "html/template"
"log" "log"
"net/http" "net/http"
fp "path/filepath" fp "path/filepath"
"time"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
@@ -16,6 +17,15 @@ import (
// This can be set in the compilation command - in the Makefile // This can be set in the compilation command - in the Makefile
var ginMode = "debug" var ginMode = "debug"
var tmplFuncMap = template.FuncMap{
"formatDate": func(ts int64) string {
return time.Unix(ts, 0).Format("02-Jan-2006")
},
"formatPrice": func(v float32) string {
return fmt.Sprintf("%.2f", v)
},
}
func main() { func main() {
config.SetConfig("./config.yaml") config.SetConfig("./config.yaml")
cfg := config.GetInstance() cfg := config.GetInstance()
@@ -23,8 +33,8 @@ func main() {
gin.SetMode(ginMode) gin.SetMode(ginMode)
templates := template.Must( templates := template.Must(
template.New(""). template.New("").
Funcs(templating.TmplFuncMap). Funcs(tmplFuncMap).
ParseGlob(fp.Join(cfg.AssetsDir, "htmx/*.html")), ParseGlob(fp.Join(cfg.AssetsDir, "htmx/*.html")),
) )
router := gin.Default() router := gin.Default()
+3 -13
View File
@@ -1,27 +1,17 @@
package pages package pages
import ( import (
"expenses/config"
"expenses/templating"
"net/http" "net/http"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
func NotFoundView(c *gin.Context, msg string) { func NotFoundView(c *gin.Context, msg string) {
cfg := config.GetInstance() c.HTML(404, "terminal", gin.H{
errHtml := templating.HtmlTemplate(
cfg.AssetsDir+"htmx/panic.html", map[string]any{
"msg": msg,
},
)
c.HTML(404, "terminal.html", gin.H{
"warning": true, "warning": true,
"renderNavBar": false, "renderNavBar": false,
"content": errHtml, "content": "panic",
"msg": msg,
}) })
} }
+3 -13
View File
@@ -1,25 +1,15 @@
package pages package pages
import ( import (
"expenses/config"
"expenses/templating"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
func ServerErrorView(c *gin.Context, msg string) { func ServerErrorView(c *gin.Context, msg string) {
cfg := config.GetInstance() c.HTML(500, "terminal", gin.H{
errHtml := templating.HtmlTemplate(
cfg.AssetsDir+"htmx/panic.html", map[string]any{
"msg": msg,
},
)
c.HTML(500, "terminal.html", gin.H{
"warning": true, "warning": true,
"renderNavBar": false, "renderNavBar": false,
"content": errHtml, "content": "panic",
"msg": msg,
}) })
} }
+18 -48
View File
@@ -1,13 +1,10 @@
package pages package pages
import ( import (
"expenses/config"
"expenses/expenses" "expenses/expenses"
"expenses/templating"
"fmt" "fmt"
"net/http" "net/http"
fp "path/filepath"
"strconv" "strconv"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@@ -17,75 +14,47 @@ const (
CategoriesPath = "/categories" CategoriesPath = "/categories"
) )
func categoriesContent() (map[string]any, error) { func categoriesGlobalPage(c *gin.Context) {
categories, err := expenses.GetAllCategories() categories, err := expenses.GetAllCategories()
if err != nil { if err != nil {
return nil, err ServerErrorView(c, "The server too makes mistakes")
}
data := map[string]any{
"size": len(categories) - 1,
"categories": categories,
"resource": "categories",
}
return data, nil
}
func categoriesGlobalPage(c *gin.Context) {
cfg := config.GetInstance()
categories, err := categoriesContent()
if err != nil {
c.Header("HX-Redirect", "/500")
return return
} }
content := templating.HtmlTemplate(
fp.Join(cfg.AssetsDir, "htmx/categories.html"),
categories,
)
c.HTML(http.StatusOK, "terminal.html", gin.H{ c.HTML(http.StatusOK, "terminal", gin.H{
"page": "categories", "page": "categories",
"renderNavBar": true, "renderNavBar": true,
"content": content, "content": "categories",
"categories": categories,
}) })
} }
func categoryPage(c *gin.Context) { func categoryPage(c *gin.Context) {
cfg := config.GetInstance()
categoryID, err := strconv.Atoi(c.Param("id")) categoryID, err := strconv.Atoi(c.Param("id"))
if err != nil { if err != nil {
c.Redirect(404, "/404") NotFoundView(c, "No such category")
return
} }
category, err := expenses.GetCategory(categoryID) category, err := expenses.GetCategory(categoryID)
content := templating.HtmlTemplate( if err != nil {
fp.Join(cfg.AssetsDir, "/htmx/category.html"), ServerErrorView(c, "The server too makes mistakes")
map[string]any{ return
"category": category, }
},
)
c.HTML(http.StatusOK, "terminal.html", gin.H{ c.HTML(http.StatusOK, "terminal", gin.H{
"page": "category", "page": "category",
"renderNavBar": false, "renderNavBar": false,
"content": content, "content": "category",
"category": category,
}) })
} }
func newCategoryPage(c *gin.Context) { func newCategoryPage(c *gin.Context) {
cfg := config.GetInstance() c.HTML(http.StatusOK, "terminal", gin.H{
content := templating.HtmlTemplate(
fp.Join(cfg.AssetsDir, "/htmx/categoryNew.html"), map[string]any{},
)
c.HTML(http.StatusOK, "terminal.html", gin.H{
"page": "expenseNew", "page": "expenseNew",
"renderNavBar": false, "renderNavBar": false,
"content": content, "content": "categoryNew",
}) })
} }
@@ -107,7 +76,8 @@ func createCategory(c *gin.Context) {
func deleteCategory(c *gin.Context) { func deleteCategory(c *gin.Context) {
categoryID, err := strconv.Atoi(c.Param("id")) categoryID, err := strconv.Atoi(c.Param("id"))
if err != nil { if err != nil {
c.Redirect(404, "/404") NotFoundView(c, "No such category")
return
} }
category := expenses.Category{ category := expenses.Category{
+3 -3
View File
@@ -34,7 +34,7 @@ func ExpensesGlobalPage(c *gin.Context) {
return return
} }
c.HTML(http.StatusOK, "terminal.html", gin.H{ c.HTML(http.StatusOK, "terminal", gin.H{
"page": "expenses", "page": "expenses",
"renderNavBar": true, "renderNavBar": true,
"content": "expenses", "content": "expenses",
@@ -96,7 +96,7 @@ func expensePage(c *gin.Context) {
return return
} }
c.HTML(http.StatusOK, "terminal.html", gin.H{ c.HTML(http.StatusOK, "terminal", gin.H{
"page": "expense", "page": "expense",
"renderNavBar": false, "renderNavBar": false,
"content": "expense", "content": "expense",
@@ -135,7 +135,7 @@ func newExpensePage(c *gin.Context) {
return return
} }
c.HTML(http.StatusOK, "terminal.html", gin.H{ c.HTML(http.StatusOK, "terminal", gin.H{
"page": "expenseNew", "page": "expenseNew",
"renderNavBar": false, "renderNavBar": false,
"content": "newExpense", "content": "newExpense",
+7 -32
View File
@@ -1,15 +1,11 @@
package pages package pages
import ( import (
"expenses/config"
"expenses/expenses" "expenses/expenses"
"expenses/templating"
"log" "log"
"time" "time"
"html/template"
"net/http" "net/http"
fp "path/filepath"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
@@ -24,35 +20,19 @@ type UserDebtSummary struct {
Total float32 Total float32
} }
func overviewContent() template.HTML {
cfg := config.GetInstance()
content := templating.HtmlTemplate(
fp.Join(cfg.AssetsDir, "htmx/overview.html"),
map[string]any{},
)
return content
}
func overviewPartialPage(c *gin.Context) { func overviewPartialPage(c *gin.Context) {
content := overviewContent() c.HTML(http.StatusOK, "overview", gin.H{})
c.String(http.StatusOK, string(content))
} }
func overviewPage(c *gin.Context) { func overviewPage(c *gin.Context) {
content := overviewContent() c.HTML(http.StatusOK, "terminal", gin.H{
c.HTML(http.StatusOK, "terminal.html", gin.H{
"page": "overview", "page": "overview",
"renderNavBar": true, "renderNavBar": true,
"content": content, "content": "overview",
}) })
} }
func getResults(c *gin.Context) { func getResults(c *gin.Context) {
cfg := config.GetInstance()
startDateStr := c.PostForm("range-start") + " 00:00:00" startDateStr := c.PostForm("range-start") + " 00:00:00"
endDateStr := c.PostForm("range-end") + " 23:59:59" endDateStr := c.PostForm("range-end") + " 23:59:59"
@@ -115,15 +95,10 @@ func getResults(c *gin.Context) {
} }
} }
content := templating.HtmlTemplate( c.HTML(http.StatusOK, "overviewResults", gin.H{
fp.Join(cfg.AssetsDir, "htmx/overviewResults.html"), "expenses": expensesWithDebts,
map[string]any{ "summary": userDebtSummary,
"expenses": expensesWithDebts, })
"summary": userDebtSummary,
},
)
c.String(http.StatusOK, string(content))
} }
func RouteOverview(router *gin.Engine) { func RouteOverview(router *gin.Engine) {
+1 -1
View File
@@ -12,7 +12,7 @@ import (
func deletePayment(c *gin.Context) { func deletePayment(c *gin.Context) {
paymentID, err := strconv.Atoi(c.Param("id")) paymentID, err := strconv.Atoi(c.Param("id"))
if err != nil { if err != nil {
c.Header("HX-Redirect", "/404") NotFoundView(c, "failed to fetch payment")
return return
} }
+13 -48
View File
@@ -1,13 +1,10 @@
package pages package pages
import ( import (
"expenses/config"
"expenses/expenses" "expenses/expenses"
"expenses/templating"
"fmt" "fmt"
"net/http" "net/http"
fp "path/filepath"
"strconv" "strconv"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@@ -17,75 +14,43 @@ const (
StoresPath = "/stores" StoresPath = "/stores"
) )
func storesContent() (map[string]any, error) { func storesGlobalPage(c *gin.Context) {
stores, err := expenses.GetAllStores() stores, err := expenses.GetAllStores()
if err != nil { if err != nil {
return nil, err ServerErrorView(c, "Failed to fetch stores content")
}
data := map[string]any{
"size": len(stores) - 1,
"stores": stores,
"resource": "stores",
}
return data, nil
}
func storesGlobalPage(c *gin.Context) {
cfg := config.GetInstance()
stores, err := storesContent()
if err != nil {
c.Header("HX-Redirect", "/500")
return return
} }
content := templating.HtmlTemplate(
fp.Join(cfg.AssetsDir, "htmx/stores.html"),
stores,
)
c.HTML(http.StatusOK, "terminal.html", gin.H{ c.HTML(http.StatusOK, "terminal", gin.H{
"page": "stores", "page": "stores",
"renderNavBar": true, "renderNavBar": true,
"content": content, "content": "stores",
"stores": stores,
}) })
} }
func storePage(c *gin.Context) { func storePage(c *gin.Context) {
cfg := config.GetInstance()
storeID, err := strconv.Atoi(c.Param("id")) storeID, err := strconv.Atoi(c.Param("id"))
if err != nil { if err != nil {
c.Redirect(404, "/404") NotFoundView(c, "didn't find request store")
return
} }
store, err := expenses.GetCategory(storeID) store, err := expenses.GetStore(storeID)
content := templating.HtmlTemplate(
fp.Join(cfg.AssetsDir, "/htmx/category.html"),
map[string]any{
"store": store,
},
)
c.HTML(http.StatusOK, "terminal.html", gin.H{ c.HTML(http.StatusOK, "terminal", gin.H{
"page": "store", "page": "store",
"renderNavBar": false, "renderNavBar": false,
"content": content, "content": "store",
"store": store,
}) })
} }
func newStorePage(c *gin.Context) { func newStorePage(c *gin.Context) {
cfg := config.GetInstance() c.HTML(http.StatusOK, "terminal", gin.H{
content := templating.HtmlTemplate(
fp.Join(cfg.AssetsDir, "/htmx/storeNew.html"), map[string]any{},
)
c.HTML(http.StatusOK, "terminal.html", gin.H{
"page": "storeNew", "page": "storeNew",
"renderNavBar": false, "renderNavBar": false,
"content": content, "content": "storeNew",
}) })
} }
+15 -47
View File
@@ -1,13 +1,10 @@
package pages package pages
import ( import (
"expenses/config"
"expenses/expenses" "expenses/expenses"
"expenses/templating"
"fmt" "fmt"
"net/http" "net/http"
fp "path/filepath"
"strconv" "strconv"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@@ -17,75 +14,46 @@ const (
TypesPath = "/types" TypesPath = "/types"
) )
func typesContent() (map[string]any, error) { func typesGlobalPage(c *gin.Context) {
types, err := expenses.GetAllTypes() types, err := expenses.GetAllTypes()
if err != nil { if err != nil {
return nil, err ServerErrorView(c, "the server failed to get the types")
}
data := map[string]any{
"size": len(types) - 1,
"types": types,
"resource": "types",
}
return data, nil
}
func typesGlobalPage(c *gin.Context) {
cfg := config.GetInstance()
types, err := typesContent()
if err != nil {
c.Header("HX-Redirect", "/500")
return return
} }
content := templating.HtmlTemplate(
fp.Join(cfg.AssetsDir, "htmx/types.html"),
types,
)
c.HTML(http.StatusOK, "terminal.html", gin.H{ c.HTML(http.StatusOK, "terminal", gin.H{
"page": "types", "page": "types",
"renderNavBar": true, "renderNavBar": true,
"content": content, "content": "types",
"types": types,
}) })
} }
func typePage(c *gin.Context) { func typePage(c *gin.Context) {
cfg := config.GetInstance()
typeID, err := strconv.Atoi(c.Param("id")) typeID, err := strconv.Atoi(c.Param("id"))
if err != nil { if err != nil {
c.Redirect(404, "/404") NotFoundView(c, "requested type doesn't exist")
return
} }
typ, err := expenses.GetType(typeID) typ, err := expenses.GetType(typeID)
content := templating.HtmlTemplate( if err != nil {
fp.Join(cfg.AssetsDir, "/htmx/type.html"), ServerErrorView(c, "failed to retrieve requested type")
map[string]any{ }
"type": typ,
},
)
c.HTML(http.StatusOK, "terminal.html", gin.H{ c.HTML(http.StatusOK, "terminal", gin.H{
"page": "type", "page": "type",
"renderNavBar": false, "renderNavBar": false,
"content": content, "content": "type",
"type": typ,
}) })
} }
func newTypePage(c *gin.Context) { func newTypePage(c *gin.Context) {
cfg := config.GetInstance() c.HTML(http.StatusOK, "terminal", gin.H{
content := templating.HtmlTemplate(
fp.Join(cfg.AssetsDir, "/htmx/typeNew.html"), map[string]any{},
)
c.HTML(http.StatusOK, "terminal.html", gin.H{
"page": "typeNew", "page": "typeNew",
"renderNavBar": false, "renderNavBar": false,
"content": content, "content": "typeNew",
}) })
} }
-58
View File
@@ -1,58 +0,0 @@
package templating
import (
"bytes"
"expenses/config"
"fmt"
"html/template"
"log"
fp "path/filepath"
"time"
)
var (
TmplFuncMap = template.FuncMap{
"formatDate": func(ts int64) string {
return time.Unix(ts, 0).Format("02-Jan-2006")
},
"formatPrice": func(v float32) string {
return fmt.Sprintf("%.2f", v)
},
}
)
func createTemplateEngine(name string) *template.Template {
cfg := config.GetInstance()
tmpl := template.New(name).Funcs(TmplFuncMap)
tmpl, err := tmpl.ParseGlob(fp.Join(cfg.AssetsDir, "htmx/*.html"))
if err != nil {
panic(err)
}
return tmpl
}
func HtmlTemplate(file string, data map[string]any) template.HTML {
tmplEngName := fp.Base(file)
tmplEngine := createTemplateEngine(tmplEngName)
tmpl, err := tmplEngine.ParseFiles(file)
if err != nil {
log.Println("error in templating")
panic(err)
}
// Use a bytes.Buffer to store the rendered output
var renderedOutput bytes.Buffer
// Render the template
err = tmpl.ExecuteTemplate(&renderedOutput, tmplEngName, data)
if err != nil {
log.Fatalf("Template execution: %v", err)
}
// Now renderedOutput holds the rendered template
return template.HTML(renderedOutput.String())
}