Configuration Guide
This guide covers all configuration aspects of the Happy Habitat backend, including database setup, JWT configuration, CORS, environment settings, and deployment considerations.Configuration Files
The main configuration file isappsettings.json located in the API project:
Path: HappyHabitat.API/appsettings.json
Development Configuration
Production Configuration
Createappsettings.Production.json for production-specific settings:
Database Configuration
Connection String
The connection string format depends on your authentication method: Windows Authentication (Development):Database Recreation (Development Only)
- Rapid development and schema changes
- Testing fresh database states
- Local development environments only
Program.cs:248-252):
Database Initialization
On startup, the application automatically:- Applies Migrations - Creates or updates database schema
- Seeds Initial Data - Creates roles, vehicle types, default admin
- Seeds Dummy Data - (Development only) Creates test users and communities
Program.cs:240-276
Manual Seeding
Run database seeding without starting the server:Database Migrations
Creating Migrations
When you modify entity models, create a new migration:Applying Migrations
Automatic (on startup): Migrations are applied automatically when the application starts. Manual:Viewing Migration History
Rolling Back Migrations
Removing Last Migration
Only remove migrations that haven’t been applied to production databases.
JWT Configuration
Settings
Key Requirements
Security Requirements
- Minimum length: 32 characters
- Complexity: Use random, cryptographically secure strings
- Uniqueness: Different key for each environment
- Rotation: Change periodically (invalidates existing tokens)
Generating a Secure Key
PowerShell:Environment Variables
Recommended for production:Double underscores (
__) in environment variables map to nested JSON configuration.Token Lifetime
Token lifetime is configured in the JWT service. Default is typically 24 hours. To customize, modifyJwtService in HappyHabitat.Infrastructure/Services/JwtService.cs.
CORS Configuration
Development
Default development origins (Program.cs:56):
Production
Configuration:;)
Safety Checks:
The application enforces CORS configuration in production (Program.cs:54-56):
CORS Policy
Current policy (Program.cs:60-66):
File Upload Configuration
Request Size Limits
Global Limit (Program.cs:18-21):Upload Directory
Uploaded files are stored in: Path:{ContentRoot}/uploads/documents/{communityId}/{category}/{residentId}/{fileName}
Configuration (Program.cs:226-233):
Ensure the application has write permissions to the uploads directory.
Logging Configuration
Log Levels
Trace- Most detailedDebug- Debugging informationInformation- General informationWarning- Warning messagesError- Error messagesCritical- Critical failuresNone- Disable logging
Production Logging
Recommended production configuration:Logging Providers
Add additional logging providers for production: Application Insights:Seed Data
Initial Seeder
Location:HappyHabitat.Infrastructure/Seeders/InitialSeeder.cs
Creates:
- 6 roles (SYSTEM_ADMIN, ADMIN_COMPANY, etc.)
- 5 vehicle types
- Default admin user (elgrandeahc)
- 10 ticket categories
- 6 ticket statuses
Dummy Seeder (Development Only)
Location:HappyHabitat.Infrastructure/Seeders/DummySeeder.cs
Creates:
- 10 test users with different roles
- 13 communities with realistic data
- 10 residents per community
- 3 company administrators
- Vehicles, pets, and visits for test users
- 11 seasonal banners
- Community announcements
Dummy data is only seeded in Development environment (
Program.cs:270-275).Authorization Policies
Authorization policies are configured inProgram.cs:113-119:
Service Registration
All application services are registered inProgram.cs:121-150:
Services use Scoped lifetime by default, meaning one instance per HTTP request.
Swagger Configuration
Swagger is enabled in Development for API documentation and testing. Configuration (Program.cs:156-198):- URL:
http://localhost:5080/(Development only) - JSON:
http://localhost:5080/swagger/v1/swagger.json
Environment-Specific Configuration
Detecting Environment
Setting Environment
Command Line:Error Handling
Global exception handling middleware is configured (Program.cs:214):
HTTPS Configuration
Development: HTTPS redirection is disabled to avoid CORS issues (Program.cs:220-223).
Production:
HTTPS is enforced:
Health Checks
Implement health checks for monitoring:Performance Configuration
Response Compression
Response Caching
Deployment Checklist
Security
- Change JWT secret key
- Configure CORS with production origins
- Use SQL authentication instead of Windows auth
- Enable HTTPS and SSL certificates
- Set
Database:RecreateOnStartuptofalse
Database
- Create production database
- Apply migrations
- Configure connection string
- Test database connectivity
- Set up backup schedule
Configuration
- Set environment to Production
- Configure logging to appropriate level
- Set up application insights or monitoring
- Configure allowed hosts
- Review all configuration settings
Performance
- Enable response compression
- Configure response caching
- Set appropriate request size limits
- Optimize database indexes
Troubleshooting
Database Connection Issues
Symptoms:- “Cannot open database” errors
- “Login failed for user” errors
- Verify connection string format
- Check SQL Server is running
- Verify firewall allows connections
- Test credentials with SQL Server Management Studio
- Check TrustServerCertificate setting
CORS Errors
Symptoms:- “Access to fetch has been blocked by CORS policy”
- Browser console shows CORS errors
- Verify frontend URL is in
Cors:Origins - Check CORS policy includes required methods and headers
- Ensure CORS middleware is before authentication
- Verify credentials are allowed if needed
JWT Validation Errors
Symptoms:- “Signature validation failed” errors
- “Token expired” errors
- Verify JWT key matches between environments
- Check issuer and audience settings
- Verify token hasn’t expired
- Check system clock synchronization
Additional Resources
- Authentication Guide - For JWT and security details
- Administrator Guide - For managing the system
- ASP.NET Core Configuration
- Entity Framework Core Migrations