🏗️ **Major Database Schema Refactoring** **Problem Solved**: Eliminated model duplication and multiple sources of truth by: - Removed duplicate models (`internal/models/content.go`) - Replaced inlined schema strings with sqlc-generated setup functions - Implemented database-specific schemas with proper NOT NULL constraints **Key Improvements**: ✅ **Single Source of Truth**: Database schemas define all types, no manual sync needed ✅ **Clean Generated Types**: sqlc generates `string` and `int64` instead of `sql.NullString/sql.NullTime` ✅ **Schema-as-Query Pattern**: Setup functions generated by sqlc for type safety ✅ **Database-Specific Optimization**: SQLite INTEGER timestamps, PostgreSQL BIGINT timestamps ✅ **Cross-Database Compatibility**: Single codebase supports both SQLite and PostgreSQL **Architecture Changes**: - `db/sqlite/` - SQLite-specific schema and setup queries - `db/postgresql/` - PostgreSQL-specific schema and setup queries - `db/queries/` - Cross-database CRUD queries using `sqlc.arg()` syntax - `internal/db/database.go` - Database abstraction with runtime selection - `internal/api/models.go` - Clean API models for requests/responses **Version Control System**: Complete element-level history with user attribution and rollback **Verification**: ✅ Full API workflow tested (create → update → rollback → versions) **Production Ready**: Supports SQLite (development) → PostgreSQL (production) migration
Insertr Content Server
REST API server for the Insertr CMS system. Provides content management with version control and user attribution.
Features
- Content Management: Full CRUD operations for content items
- Version Control: Complete edit history with rollback functionality
- User Attribution: Track who made each change
- Type-Safe Database: Uses sqlc for generated Go code from SQL
- SQLite & PostgreSQL: Database flexibility for development to production
API Endpoints
Content Operations
GET /api/content?site_id={site}- Get all content for a siteGET /api/content/{id}?site_id={site}- Get single content itemGET /api/content/bulk?site_id={site}&ids[]={id1}&ids[]={id2}- Get multiple content itemsPOST /api/content- Create new contentPUT /api/content/{id}?site_id={site}- Update existing contentDELETE /api/content/{id}?site_id={site}- Delete content
Version Control
GET /api/content/{id}/versions?site_id={site}- Get version historyPOST /api/content/{id}/rollback?site_id={site}- Rollback to specific version
Health & Status
GET /health- Server health check
User Attribution
All content operations support user attribution via the X-User-ID header:
curl -X PUT "http://localhost:8080/api/content/hero-title?site_id=demo" \
-H "Content-Type: application/json" \
-H "X-User-ID: john@example.com" \
-d '{"value": "Updated content"}'
Quick Start
# Build server
go build -o insertr-server ./cmd/server
# Start server
./insertr-server --port 8080
# Check health
curl http://localhost:8080/health
Development
Using sqlc
# Install sqlc
go install github.com/sqlc-dev/sqlc/cmd/sqlc@latest
# Generate Go code from SQL
sqlc generate
# Build with generated code
go build ./cmd/server
Database Schema
See db/schema/schema.sql for the complete schema. Key tables:
content- Current content versionscontent_versions- Complete version history
Example Version Control Workflow
# Create content
curl -X POST "http://localhost:8080/api/content" \
-H "Content-Type: application/json" \
-H "X-User-ID: alice@example.com" \
-d '{
"id": "hero-title",
"site_id": "demo",
"value": "Original Title",
"type": "text"
}'
# Update content (creates version)
curl -X PUT "http://localhost:8080/api/content/hero-title?site_id=demo" \
-H "Content-Type: application/json" \
-H "X-User-ID: bob@example.com" \
-d '{"value": "Updated Title"}'
# View version history
curl "http://localhost:8080/api/content/hero-title/versions?site_id=demo"
# Rollback to version 1
curl -X POST "http://localhost:8080/api/content/hero-title/rollback?site_id=demo" \
-H "Content-Type: application/json" \
-H "X-User-ID: admin@example.com" \
-d '{"version_id": 1}'
Configuration
Environment Variables
PORT- Server port (default: 8080)DB_PATH- SQLite database file path (default: ./insertr.db)
Command Line Flags
./insertr-server --help
Production Deployment
- Database: Consider PostgreSQL for production scale
- Authentication: Integrate with your auth system via middleware
- CORS: Configure appropriate CORS policies
- SSL: Serve over HTTPS
- Monitoring: Add logging and metrics collection