@casinelli/blog-server
v1.1.0
Published
A RESTful API server for managing multi-tenant blog sites with Express, Prisma, and PostgreSQL
Maintainers
Readme
@casinelli/blog-server
A RESTful API server for managing multi-tenant blog sites, built with Express, Prisma, and PostgreSQL.
Features
- Multi-tenant blog management (sites, posts, categories, tags)
- API key authentication with scopes and rate limiting
- IP and origin restrictions for API keys
- Usage tracking and statistics
- OpenAPI documentation with Swagger UI
- TypeDoc code documentation
Installation
npm install @casinelli/blog-serverOr install globally to use the CLI:
npm install -g @casinelli/blog-serverPrerequisites
- Node.js 18+
- PostgreSQL database
- Supabase project (for authentication)
Quick Start
1. Set environment variables
export DATABASE_URL=postgresql://user:password@localhost:5432/blog_db
export SUPABASE_URL=https://your-project.supabase.co
export SUPABASE_ANON_KEY=your-anon-key2. Run the server
blog-serverThe API will be available at http://localhost:3001
Setup (Development)
1. Clone and install dependencies
git clone https://github.com/casinelli/blog-server.git
cd blog-server
npm install2. Configure environment variables
Create a .env file in the project root:
# Database
DATABASE_URL=postgresql://user:password@localhost:5432/blog_db
# Supabase
SUPABASE_URL=https://your-project.supabase.co
SUPABASE_ANON_KEY=your-anon-key
# API Server
API_PORT=3001
CORS_ORIGINS=http://localhost:3000,http://localhost:5173
# Environment
NODE_ENV=development3. Initialize the database
Generate the Prisma client and run migrations:
npx prisma generate
npx prisma db pushOptionally seed initial data:
npm run db:init4. Start the server
Development mode (with hot reload):
npm run dev:apiProduction mode:
npm run start:apiAPI Documentation
Once the server is running:
- Swagger UI: http://localhost:3001/api/docs
- OpenAPI Spec: http://localhost:3001/api/openapi.json
- TypeDoc: http://localhost:3001/api/typedoc
- Health Check: http://localhost:3001/api/health
Authentication
All API routes (except health, docs, and info endpoints) require authentication via API key.
Providing API Keys
Include your API key in requests using one of these methods:
# Authorization header
curl -H "Authorization: Bearer sk_live_your_key" http://localhost:3001/api/sites
# X-API-Key header
curl -H "X-API-Key: sk_live_your_key" http://localhost:3001/api/sitesKey Types
| Type | Prefix | Description |
|------|--------|-------------|
| user | sk_* | User-scoped access to owned/member sites |
| site | ss_* | Access to a specific site only |
| admin | sa_* | Full system access |
Scopes
read- View resourceswrite- Create and update resourcesdelete- Remove resourcesadmin- Full access
Scripts
| Command | Description |
|---------|-------------|
| npm run dev | Start Vite dev server (frontend) |
| npm run dev:api. | Start API server with hot reload |
| npm run start:api | Start API server |
| npm run build | Build for production |
| npm run test | Run tests |
| npm run test:watch | Run tests in watch mode |
| npm run docs | Generate TypeDoc documentation |
| npm run db:init | Initialize database with seed data |
Project Structure
blog-server/
├── api/
│ ├── middleware/
│ │ └── auth.ts # Authentication middleware
│ ├── routes/
│ │ ├── sites.ts # Site management
│ │ ├── posts.ts # Post management
│ │ ├── users.ts # User management
│ │ ├── api-keys.ts # API key management
│ │ ├── categories.ts # Category management
│ │ └── tags.ts # Tag management
│ ├── openapi.yaml # OpenAPI specification
│ └── server.ts # Express server setup
├── src/
│ └── lib/
│ ├── api-keys.ts # API key functions
│ ├── prisma.ts # Database client
│ └── supabase.ts # Supabase client
├── prisma/
│ └── schema.prisma # Database schema
├── tests/ # Test files
├── docs/ # Generated TypeDoc docs
└── typedoc.json # TypeDoc configurationAPI Endpoints
Sites
GET /api/sites- List accessible sitesGET /api/sites/:siteId- Get site detailsPOST /api/sites- Create a sitePATCH /api/sites/:siteId- Update a siteDELETE /api/sites/:siteId- Delete a site (admin)GET /api/sites/:siteId/stats- Get site statistics
Posts
GET /api/sites/:siteId/posts- List postsGET /api/posts/:postId- Get post detailsPOST /api/sites/:siteId/posts- Create a postPATCH /api/posts/:postId- Update a postDELETE /api/posts/:postId- Delete a post
Users
GET /api/users- List users (admin)GET /api/users/me- Get current userGET /api/users/:userId- Get user profilePATCH /api/users/:userId- Update userDELETE /api/users/:userId- Delete user (admin)
API Keys
GET /api/api-keys- List API keysPOST /api/api-keys- Create API keyPATCH /api/api-keys/:keyId- Update API keyPOST /api/api-keys/:keyId/revoke- Revoke API keyGET /api/api-keys/:keyId/usage- Get usage stats
Categories & Tags
GET /api/sites/:siteId/categories- List categoriesPOST /api/sites/:siteId/categories- Create categoryGET /api/sites/:siteId/tags- List tagsPOST /api/sites/:siteId/tags- Create tag
Testing
Run the test suite:
npm run testRun tests in watch mode:
npm run test:watchLicense
MIT
