From d81d78e25fc368ca3d2be413f564390cb90e1c29 Mon Sep 17 00:00:00 2001 From: 0ceanSlim Date: Fri, 9 Aug 2024 09:24:32 -0400 Subject: [PATCH] frontend refactor --- app/src/app.go | 76 ------------------------ app/src/routes/importEvents.go | 6 +- app/src/routes/index.go | 17 ++++++ app/src/types/pageData.go | 5 ++ app/src/{ => utils}/fetchRecentEvents.go | 2 +- app/src/utils/prependDir.go | 10 ++++ app/src/utils/renderTemplate.go | 44 ++++++++++++++ main.go | 3 +- 8 files changed, 82 insertions(+), 81 deletions(-) delete mode 100644 app/src/app.go create mode 100644 app/src/routes/index.go create mode 100644 app/src/types/pageData.go rename app/src/{ => utils}/fetchRecentEvents.go (98%) create mode 100644 app/src/utils/prependDir.go create mode 100644 app/src/utils/renderTemplate.go diff --git a/app/src/app.go b/app/src/app.go deleted file mode 100644 index 8ad9e26..0000000 --- a/app/src/app.go +++ /dev/null @@ -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 -} - - diff --git a/app/src/routes/importEvents.go b/app/src/routes/importEvents.go index 3d62c1c..58a02ba 100644 --- a/app/src/routes/importEvents.go +++ b/app/src/routes/importEvents.go @@ -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") } diff --git a/app/src/routes/index.go b/app/src/routes/index.go new file mode 100644 index 0000000..a6b3a03 --- /dev/null +++ b/app/src/routes/index.go @@ -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") +} \ No newline at end of file diff --git a/app/src/types/pageData.go b/app/src/types/pageData.go new file mode 100644 index 0000000..2c9f25f --- /dev/null +++ b/app/src/types/pageData.go @@ -0,0 +1,5 @@ +package types + +type PageData struct { + Title string +} \ No newline at end of file diff --git a/app/src/fetchRecentEvents.go b/app/src/utils/fetchRecentEvents.go similarity index 98% rename from app/src/fetchRecentEvents.go rename to app/src/utils/fetchRecentEvents.go index cac3a6d..4a551b8 100644 --- a/app/src/fetchRecentEvents.go +++ b/app/src/utils/fetchRecentEvents.go @@ -1,4 +1,4 @@ -package app +package utils import ( "context" diff --git a/app/src/utils/prependDir.go b/app/src/utils/prependDir.go new file mode 100644 index 0000000..193a6d9 --- /dev/null +++ b/app/src/utils/prependDir.go @@ -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 +} diff --git a/app/src/utils/renderTemplate.go b/app/src/utils/renderTemplate.go new file mode 100644 index 0000000..d234ccb --- /dev/null +++ b/app/src/utils/renderTemplate.go @@ -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) + } +} + + + diff --git a/main.go b/main.go index 2c8918f..fb97739 100644 --- a/main.go +++ b/main.go @@ -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) } }