6.6 KiB
CLAUDE.md
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
Project Overview
Lenz-Web is a Go web application that serves digitized historical letters. It's built with:
- Go 1.24 using Fiber web framework
- Custom templating engine with hot reload for development
- Git-based content management (clones/updates from remote repository)
- XML parsing for letter metadata and content
- Static asset serving with compression and ETags
Development Commands
Build and Run
go build # Build the application
./lenz-web # Run the built binary
go run . # Build and run directly
Docker
docker-compose up # Run with Docker (exposes port 8085)
docker build . # Build Docker image
Testing
The project does not currently have automated tests. Check for *_test.go files before assuming test frameworks.
Architecture
Main Application Flow (lenz.go:30-69)
- Loads configuration from JSON files and environment variables
- Clones/opens Git repository containing letter data
- Parses XML models from the repository
- Sets up templating engine with custom functions
- Configures Fiber web server with routes and middleware
- In debug mode, enables file watchers for hot reload
Core Components
Configuration (config/)
config.go: Configuration management with JSON files, environment variables, and defaults- Uses
KGPZ_prefix for environment variables - Default config file:
config.dev.json
Templating (templating/)
- Custom template engine with layout and route registries
- WebSocket-based hot reload in debug mode
- Template functions for XML data processing
Controllers (controllers/)
- Route handlers for web endpoints
routes.go: Route registration and URL constants- Main routes:
/,/briefe,/brief/:letter, static pages
XML Models (xmlmodels/)
- Parses historical letter data from XML files
- Provides template functions for data access
- Core models: Library, Letter, Traditions, References
File Watching (watcher.go)
- Custom file watcher with debouncing (300ms)
- Two watch modes:
- REFRESH_CHANGES:
./views/assets(clears cache, sends refresh signal) - RESET_CHANGES:
./views/layouts,./views/routes(clears cache, reloads templates)
- REFRESH_CHANGES:
Key Directories
views/: Frontend templates, layouts, and static assetshelpers/: Utility functions and middlewareserver/: Fiber server configuration and cachinggit/: Git repository management
Configuration
Configuration is loaded in order: JSON files → environment variables → defaults
Key settings:
debug: Enables hot reload and verbose logginggit_url: Repository URL for letter contentbase_dir: Cache directory (default:_cache)port: Server port (default:8085)webhook_secret: GitHub webhook integration
Environment variables use KGPZ_ prefix (e.g., KGPZ_DEBUG=true).
Views Directory Structure
The views/ directory contains all frontend templates and assets:
views/
├── assets/ # Static files (CSS, JS, fonts, images)
├── layouts/ # Base layout templates
│ ├── components/ # Reusable layout components (_header, _footer, _menu, etc.)
│ └── default/ # Default layout (root.gohtml)
└── routes/ # Page-specific templates
├── brief/ # Individual letter view (head.gohtml, body.gohtml)
├── briefe/ # Letter listing view
├── components/ # Page components (_letterhead, _letterlist, _lettertrad)
├── datenschutz/ # Privacy page
├── edition/ # Edition info pages
└── kontakt/ # Contact page
Template System Architecture
Build-Time Embedding vs Development
- Production: Templates embedded via
//go:embeddirectives inviews/embed.go - Development: Direct filesystem access via
views/embed_dev.go(when built with-tags dev) - File watchers enable hot reload in debug mode
Template Structure Each route directory contains:
head.gohtml:<head>section content for the pagebody.gohtml: Main page content
Layout System (templating/engine.go)
- Base layout:
views/layouts/default/root.gohtml - Uses Go template blocks:
{{ block "head" . }},{{ block "body" . }}, etc. - Layout registry loads and caches layouts with template functions
- Template registry loads route-specific templates
Template Functions Available Core functions (templating/engine.go:118-131):
Safe: Render HTML without escapingToday: Current dateGetMonth: Month name from numberMinus: Subtraction for template math
XML Data Functions (added from xmlmodels/library.go):
Person: Get person data by reference IDPlace: Get location data by reference IDParseGeneric: Parse and render letter text with markup
Template Data Flow
- Controllers pass
fiber.Mapdata to templates - Engine merges with
GlobalData(debug flags, etc.) - Layout loaded and cloned from registry
- Route templates added to layout clone
- Combined template executed with merged data
Hot Reload System
- WebSocket server on port 9000 for live reload in debug mode
- File watchers trigger different actions:
./views/assetschanges: Clear cache + browser refresh signal./views/layouts,./views/routeschanges: Clear cache + reload templates
- 300ms debounce to prevent excessive reloads
Working with Templates
Template Syntax
- Standard Go template syntax with custom functions
- Variables:
{{ $model := . }}for cleaner references - Conditionals:
{{ if .isDev }}...{{ end }} - Loops:
{{ range $item := .items }}...{{ end }} - Components:
{{ template "_letterhead" .meta }}
Debugging Templates
- Enable debug mode in config:
"debug": true - Templates reload automatically on file changes
- Browser refreshes automatically via WebSocket
- Console shows reload notifications
Letter Data Templates Letter pages have access to rich XML data:
.meta: Letter metadata (dates, persons, places).text: Parsed letter content with markup.prev/.next: Navigation to adjacent letters- Navigation uses custom functions like
PersonandPlaceto resolve references
Development Notes
- File watchers only active in debug mode
- Templates are cached; use debug mode for development
- Static assets served with compression and ETags
- Git repository is cloned to
base_dir/git_pathon startup - WebSocket endpoint available for live reload in debug mode
- Template files use
.gohtmlextension for proper syntax highlighting