frontend refactor

This commit is contained in:
0ceanSlim 2024-08-09 09:24:32 -04:00
parent 921b1adead
commit d81d78e25f
8 changed files with 82 additions and 81 deletions

View File

@ -1,76 +0,0 @@
package app
import (
"grain/server/db"
relay "grain/server/types"
"html/template"
"net/http"
)
type PageData struct {
Title string
Theme string
Events []relay.Event
}
func RootHandler(w http.ResponseWriter, r *http.Request) {
// Fetch the top ten most recent events
client := db.GetClient()
events, err := FetchTopTenRecentEvents(client)
if err != nil {
http.Error(w, "Unable to fetch events", http.StatusInternalServerError)
return
}
data := PageData{
Title: "GRAIN Dashboard",
Events: events,
}
RenderTemplate(w, data, "index.html")
}
// Define the base directories for views and templates
const (
viewsDir = "app/views/"
templatesDir = "app/views/templates/"
)
// Define the common layout templates filenames
var templateFiles = []string{
"layout.html",
"header.html",
"footer.html",
}
// Initialize the common templates with full paths
var layout = PrependDir(templatesDir, templateFiles)
func RenderTemplate(w http.ResponseWriter, data PageData, view string) {
// Append the specific template for the route
templates := append(layout, viewsDir+view)
// Parse all templates
tmpl, err := template.ParseFiles(templates...)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Execute the "layout" template
err = tmpl.ExecuteTemplate(w, "layout", data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
// Helper function to prepend a directory path to a list of filenames
func PrependDir(dir string, files []string) []string {
var fullPaths []string
for _, file := range files {
fullPaths = append(fullPaths, dir+file)
}
return fullPaths
}

View File

@ -1,7 +1,9 @@
package routes
import (
app "grain/app/src"
app "grain/app/src/types"
"grain/app/src/utils"
"net/http"
)
@ -11,5 +13,5 @@ func ImportEvents(w http.ResponseWriter, r *http.Request) {
}
// Call RenderTemplate with the specific template for this route
app.RenderTemplate(w, data, "importEvents.html")
utils.RenderTemplate(w, data, "importEvents.html")
}

17
app/src/routes/index.go Normal file
View File

@ -0,0 +1,17 @@
package routes
import (
app "grain/app/src/types"
"grain/app/src/utils"
"net/http"
)
func IndexHandler(w http.ResponseWriter, r *http.Request) {
data := app.PageData{
Title: "GRAIN Dashboard",
}
utils.RenderTemplate(w, data, "index.html")
}

View File

@ -0,0 +1,5 @@
package types
type PageData struct {
Title string
}

View File

@ -1,4 +1,4 @@
package app
package utils
import (
"context"

View File

@ -0,0 +1,10 @@
package utils
// Helper function to prepend a directory path to a list of filenames
func PrependDir(dir string, files []string) []string {
var fullPaths []string
for _, file := range files {
fullPaths = append(fullPaths, dir+file)
}
return fullPaths
}

View File

@ -0,0 +1,44 @@
package utils
import (
app "grain/app/src/types"
"html/template"
"net/http"
)
// Define the base directories for views and templates
const (
viewsDir = "app/views/"
templatesDir = "app/views/templates/"
)
// Define the common layout templates filenames
var templateFiles = []string{
"layout.html",
"header.html",
"footer.html",
}
// Initialize the common templates with full paths
var layout = PrependDir(templatesDir, templateFiles)
func RenderTemplate(w http.ResponseWriter, data app.PageData, view string) {
// Append the specific template for the route
templates := append(layout, viewsDir+view)
// Parse all templates
tmpl, err := template.ParseFiles(templates...)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Execute the "layout" template
err = tmpl.ExecuteTemplate(w, "layout", data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}

View File

@ -2,7 +2,6 @@ package main
import (
"fmt"
app "grain/app/src"
"grain/app/src/api"
"grain/app/src/routes"
"grain/config"
@ -86,6 +85,6 @@ func ListenAndServe(w http.ResponseWriter, r *http.Request) {
} else if r.Header.Get("Accept") == "application/nostr+json" {
utils.RelayInfoHandler(w, r)
} else {
app.RootHandler(w, r)
routes.IndexHandler(w, r)
}
}