grain/app/app.go

116 lines
2.7 KiB
Go
Raw Permalink Normal View History

package app
2024-07-23 20:40:39 +00:00
import (
"context"
2024-07-31 18:41:48 +00:00
"grain/server/db"
relay "grain/server/types"
2024-07-23 20:40:39 +00:00
"html/template"
"net/http"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
2024-07-23 20:40:39 +00:00
)
type PageData struct {
Title string
Theme string
Events []relay.Event
}
2024-07-23 20:40:39 +00:00
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
}
2024-07-23 20:40:39 +00:00
data := PageData{
Title: "GRAIN Relay",
Events: events,
2024-07-23 20:40:39 +00:00
}
2024-07-23 20:40:39 +00:00
RenderTemplate(w, data, "index.html")
}
// Define the base directories for views and templates
const (
2024-07-31 19:54:42 +00:00
viewsDir = "app/views/"
templatesDir = "app/views/templates/"
2024-07-23 20:40:39 +00:00
)
// Define the common layout templates filenames
var templateFiles = []string{
2024-07-27 20:46:25 +00:00
"layout.html",
2024-07-23 20:40:39 +00:00
"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
}
// FetchTopTenRecentEvents queries the database and returns the top ten most recent events.
func FetchTopTenRecentEvents(client *mongo.Client) ([]relay.Event, error) {
var results []relay.Event
2024-07-28 13:41:10 +00:00
collections, err := client.Database("grain").ListCollectionNames(context.TODO(), bson.M{})
if err != nil {
return nil, err
}
2024-07-28 13:41:10 +00:00
for _, collectionName := range collections {
collection := client.Database("grain").Collection(collectionName)
filter := bson.D{}
opts := options.Find().SetSort(bson.D{{Key: "createdat", Value: -1}}).SetLimit(10)
cursor, err := collection.Find(context.TODO(), filter, opts)
if err != nil {
return nil, err
}
2024-07-28 13:41:10 +00:00
defer cursor.Close(context.TODO())
for cursor.Next(context.TODO()) {
var event relay.Event
if err := cursor.Decode(&event); err != nil {
return nil, err
}
results = append(results, event)
}
2024-07-28 13:41:10 +00:00
if err := cursor.Err(); err != nil {
return nil, err
}
}
return results, nil
}