Recount-Server/web/router.go

129 lines
3.6 KiB
Go
Raw Normal View History

2024-01-11 20:07:47 -08:00
package web
import (
2024-02-10 08:51:27 -08:00
"nickiel.net/recount_server/web/templates"
"html/template"
2024-01-11 20:07:47 -08:00
"net/http"
2024-01-20 21:51:10 -08:00
"strconv"
2024-01-11 20:07:47 -08:00
"bytes"
"context"
2024-01-12 15:48:31 -08:00
"github.com/a-h/templ"
2024-01-11 20:07:47 -08:00
"github.com/go-chi/chi/v5"
2024-01-12 15:48:31 -08:00
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
2024-01-11 20:07:47 -08:00
)
2024-02-10 08:51:27 -08:00
type IndexTemplateModel struct {
InnerHtml template.HTML
ActivePage string
2024-02-10 08:51:27 -08:00
NewTransactionPane template.HTML
QuickAccessPane template.HTML
}
const TemplateDir = "./web/templates/"
2024-01-12 15:48:31 -08:00
func SetLogLevel(level zerolog.Level) {
zerolog.SetGlobalLevel(level)
}
2024-01-11 20:07:47 -08:00
func WebRouter() http.Handler {
r := chi.NewRouter()
2024-01-20 15:05:06 -08:00
r.Get("/", getDashboard)
r.Get("/transactions", getTransactionsPage)
2024-01-24 18:58:29 -08:00
2024-02-10 08:51:27 -08:00
r.Get("/components/account_summaries", GetAccountSummaries)
2024-01-24 18:58:29 -08:00
2024-02-10 08:51:27 -08:00
r.Get("/components/data/transaction_table_rows", GetTransactionsRows)
r.Get("/components/data/expenditure_chart", GetExpenditureChart)
r.Get("/components/data/account_summary/{accountID}", GetAccountSummaryChart)
r.Get("/components/data/transaction_quick_access", GetTransactionQuickAccessEntries)
2024-01-24 18:58:29 -08:00
2024-01-12 20:50:41 -08:00
r.Handle("/static/*", http.StripPrefix("/static/", http.FileServer(http.Dir("web/static/"))))
2024-01-11 20:07:47 -08:00
return r
}
//for name, values := range req.Header {
// Loop over all values for the name.
// for _, value := range values {
// log.Debug().Msg(name + " " + value);
// }
// }
func renderFullPage(w http.ResponseWriter, c templ.Component, pageName string) {
2024-02-10 08:51:27 -08:00
var main_component bytes.Buffer
// Render the provided templ component
2024-02-10 08:51:27 -08:00
err := c.Render(context.Background(), &main_component)
if err != nil {
log.Fatal().Err(err).Msg("Fatal error reading hello template");
}
// get the index template
index, err := template.ParseFiles(TemplateDir + "index.html")
if err != nil {
log.Fatal().
Err(err).
Msg("Fatal error reading index template")
}
2024-02-10 08:51:27 -08:00
var new_tp bytes.Buffer;
err = templates.NewTransactionPane().Render(context.Background(), &new_tp);
if err != nil {
log.Fatal().Err(err).Msg("Could not render new transaction pane for index");
}
var quick_ap bytes.Buffer;
err = templates.QuickAccessPane().Render(context.Background(), &quick_ap);
if err != nil {
log.Fatal().Err(err).Msg("Could not render quick access pane for index");
}
// Inject the templ component html into the index template
err = index.Execute(w,
2024-02-10 08:51:27 -08:00
IndexTemplateModel{
InnerHtml: template.HTML(main_component.String()),
ActivePage: pageName,
2024-02-10 08:51:27 -08:00
NewTransactionPane: template.HTML(new_tp.String()),
QuickAccessPane: template.HTML(quick_ap.String()),
});
if err != nil {
log.Fatal().Err(err).Msg("Fatal error reading hello template");
}
}
2024-01-20 15:05:06 -08:00
func getDashboard(w http.ResponseWriter, req *http.Request) {
2024-02-10 08:51:27 -08:00
component := templates.Dashboard();
_, ok := req.Header["Hx-Request"]
if ok {
err := component.Render(context.Background(), w);
if err != nil {
log.Fatal().Err(err).Msg("Couldn't render dashboard templ template");
}
} else {
2024-01-20 15:05:06 -08:00
renderFullPage(w, component, "Dashboard");
}
}
2024-01-20 15:05:06 -08:00
func getTransactionsPage(w http.ResponseWriter, req *http.Request) {
2024-01-20 21:51:10 -08:00
accounts := make([]string, 10);
for i := 0; i < 10; i++ {
accounts[i] = strconv.Itoa(i) + " Account";
}
2024-01-20 15:05:06 -08:00
2024-02-10 08:51:27 -08:00
component := templates.TransactionsPage(1, &accounts);
_, ok := req.Header["Hx-Request"]
if ok {
2024-01-20 15:05:06 -08:00
err := component.Render(context.Background(), w);
if err != nil {
2024-01-20 15:05:06 -08:00
log.Fatal().Err(err).Msg("Couldn't render transactions templ template");
}
2024-01-20 15:05:06 -08:00
} else {
renderFullPage(w, component, "Transactions")
}
2024-01-20 15:05:06 -08:00
}