Readability pass across all four areas of the stack. Comments explain why rather than what, concentrated at the points where behaviour is non-obvious: concurrency, caching, auth, scraping and nginx templating. Backend: doc comments on every exported declaration across handlers, services, models and resolvers. Resolver commentary lives inside function bodies so gqlgen does not strip it on regeneration; schema files use # comments rather than description blocks so introspection output is unchanged. Extracted the duplicated auth-cookie blocks into helpers. Frontend: header comments on every component, store and view, including STATUS: notes marking which of the parallel/legacy views are still live. Documented the non-reactive animation-state pattern, the homeData hub and the WebSocket stale-socket guard. Removed a few dead imports and named the reconnect-backoff and poll-interval constants. Python: module and function docstrings covering the Travelodge scraping contract, the deadline threading, the two-executor concurrency and the NDJSON streaming protocol. Infra: header blocks on every compose file, nginx template, Dockerfile and entrypoint, covering the config selection, envsubst allow-lists, rate-limit semantics and volume persistence. Hoisted the duplicated envsubst variable list in nginx/entrypoint.sh. New: backend/README.md, python/README.md, DEPLOYMENT.md, .env.example, backend/graph/doc.go. Refreshed vue/README.md. Corrected CLAUDE.md, which stated a 24h access token where main.go sets 7 days. No behaviour change. go build/vet, vite build, py_compile and docker compose config all pass. Co-Authored-By: Claude Opus 5 <[email protected]>
75 lines
2.4 KiB
Go
75 lines
2.4 KiB
Go
package services
|
|
|
|
import (
|
|
"log"
|
|
"time"
|
|
|
|
"adam-french.co.uk/backend/models"
|
|
"golang.org/x/crypto/bcrypt"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// SeedDatabase populates an empty database with demo content for local
|
|
// development. It is called from main only when SEED_DB=true, which
|
|
// docker-compose.dev.yml sets; it must never run in production because it
|
|
// creates an admin account with the password "password".
|
|
func SeedDatabase(db *gorm.DB) {
|
|
// "Any user exists" is the whole idempotency check — if the users table
|
|
// has a row, nothing is seeded, even if the other tables were emptied.
|
|
var user models.User
|
|
if db.First(&user).Error == nil {
|
|
log.Println("Database already has data, skipping seed")
|
|
return
|
|
}
|
|
|
|
log.Println("Seeding database with test data...")
|
|
|
|
hashedPassword, err := bcrypt.GenerateFromPassword([]byte("password"), bcrypt.DefaultCost)
|
|
if err != nil {
|
|
log.Fatal("Failed to hash seed password:", err)
|
|
}
|
|
|
|
// Dev-only credentials: testuser / password.
|
|
testUser := models.User{
|
|
Username: "testuser",
|
|
Password: hashedPassword,
|
|
Admin: true,
|
|
}
|
|
// GORM writes the generated primary key back into testUser, which the
|
|
// posts below then reference as their author.
|
|
db.Create(&testUser)
|
|
|
|
posts := []models.Post{
|
|
{Title: "Welcome to my blog", Content: "This is the first test post with some example content.", AuthorID: testUser.ID},
|
|
{Title: "Learning Go", Content: "Go is a great language for building web servers and APIs.", AuthorID: testUser.ID},
|
|
{Title: "Vue 3 Tips", Content: "The composition API makes Vue components much more flexible.", AuthorID: testUser.ID},
|
|
}
|
|
db.Create(&posts)
|
|
|
|
link1 := "https://example.com/project"
|
|
link2 := "https://example.com/book"
|
|
activities := []models.Activity{
|
|
{Type: "project", Name: "coding"},
|
|
{Type: "hobby", Name: "reading", Link: &link1},
|
|
{Type: "fitness", Name: "exercise"},
|
|
}
|
|
db.Create(&activities)
|
|
|
|
favorites := []models.Favorite{
|
|
{Type: "language", Name: "Go"},
|
|
{Type: "book", Name: "Designing Data-Intensive Applications", Link: &link2},
|
|
{Type: "framework", Name: "Vue"},
|
|
}
|
|
db.Create(&favorites)
|
|
|
|
now := time.Now()
|
|
rowingEntries := []models.Rowing{
|
|
{Date: now.AddDate(0, 0, -14), Time: 1800, Distance: 5000, TimePer500m: 120.0, Calories: 300},
|
|
{Date: now.AddDate(0, 0, -7), Time: 1750, Distance: 5200, TimePer500m: 118.5, Calories: 315},
|
|
{Date: now, Time: 1700, Distance: 5400, TimePer500m: 116.2, Calories: 330},
|
|
}
|
|
db.Create(&rowingEntries)
|
|
|
|
log.Println("Database seeded successfully")
|
|
}
|