🏗️ **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
123 lines
3.3 KiB
Markdown
123 lines
3.3 KiB
Markdown
# 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 site
|
|
- `GET /api/content/{id}?site_id={site}` - Get single content item
|
|
- `GET /api/content/bulk?site_id={site}&ids[]={id1}&ids[]={id2}` - Get multiple content items
|
|
- `POST /api/content` - Create new content
|
|
- `PUT /api/content/{id}?site_id={site}` - Update existing content
|
|
- `DELETE /api/content/{id}?site_id={site}` - Delete content
|
|
|
|
### Version Control
|
|
- `GET /api/content/{id}/versions?site_id={site}` - Get version history
|
|
- `POST /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:
|
|
|
|
```bash
|
|
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
|
|
|
|
```bash
|
|
# 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
|
|
|
|
```bash
|
|
# 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 versions
|
|
- `content_versions` - Complete version history
|
|
|
|
### Example Version Control Workflow
|
|
|
|
```bash
|
|
# 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
|
|
```bash
|
|
./insertr-server --help
|
|
```
|
|
|
|
## Production Deployment
|
|
|
|
1. **Database**: Consider PostgreSQL for production scale
|
|
2. **Authentication**: Integrate with your auth system via middleware
|
|
3. **CORS**: Configure appropriate CORS policies
|
|
4. **SSL**: Serve over HTTPS
|
|
5. **Monitoring**: Add logging and metrics collection |