mirror of
https://github.com/0ceanSlim/grain.git
synced 2024-11-24 09:23:23 +00:00
frontend refactor
This commit is contained in:
parent
921b1adead
commit
d81d78e25f
@ -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
|
||||
}
|
||||
|
||||
|
@ -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
17
app/src/routes/index.go
Normal 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")
|
||||
}
|
5
app/src/types/pageData.go
Normal file
5
app/src/types/pageData.go
Normal file
@ -0,0 +1,5 @@
|
||||
package types
|
||||
|
||||
type PageData struct {
|
||||
Title string
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package app
|
||||
package utils
|
||||
|
||||
import (
|
||||
"context"
|
10
app/src/utils/prependDir.go
Normal file
10
app/src/utils/prependDir.go
Normal 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
|
||||
}
|
44
app/src/utils/renderTemplate.go
Normal file
44
app/src/utils/renderTemplate.go
Normal 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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
3
main.go
3
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)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user