|
| 1 | +--- |
| 2 | +title: Configuration |
| 3 | +description: Configuration of .NET React template features |
| 4 | +--- |
| 5 | + |
| 6 | +## Configuration |
| 7 | + |
| 8 | +### Key Configuration Files |
| 9 | + |
| 10 | +- **MyApp/appsettings.json** - Application configuration |
| 11 | +- **MyApp.Client/next.config.mjs** - Next.js configuration (for Next.js templates) |
| 12 | +- **MyApp.Client/vite.config.ts** - Vite configuration (for Vite templates) |
| 13 | +- **MyApp.Client/styles/index.css** - Tailwind CSS configuration |
| 14 | +- **config/deploy.yml** - Kamal deployment settings |
| 15 | + |
| 16 | +## App Settings |
| 17 | + |
| 18 | +Configure in `appsettings.json` or environment: |
| 19 | + |
| 20 | +```json |
| 21 | +{ |
| 22 | + "ConnectionStrings": { |
| 23 | + "DefaultConnection": "DataSource=App_Data/app.db;Cache=Shared" |
| 24 | + }, |
| 25 | + "SmtpConfig": { |
| 26 | + "Host": "smtp.example.com", |
| 27 | + "Port": 587, |
| 28 | + |
| 29 | + "FromName": "MyApp" |
| 30 | + }, |
| 31 | + "AppConfig": { |
| 32 | + } |
| 33 | +} |
| 34 | +``` |
| 35 | + |
| 36 | +### Enable SMTP to Send Identity Auth Emails |
| 37 | + |
| 38 | +Update **SmtpConfig** in `appsettings.json` with your SMTP settings. |
| 39 | + |
| 40 | +In `Program.cs`, uncomment: |
| 41 | + |
| 42 | +```csharp |
| 43 | +services.AddSingleton<IEmailSender<ApplicationUser>, EmailSender>(); |
| 44 | +``` |
| 45 | + |
| 46 | +### App Settings Secrets |
| 47 | + |
| 48 | +Instead of polluting each GitHub Reposity with multiple App-specific GitHub Action Secrets, you can save all your secrets in a single `APPSETTINGS_PATCH` GitHub Action Secret to patch `appsettings.json` with environment-specific configuration using [JSON Patch](https://jsonpatch.com). E.g: |
| 49 | + |
| 50 | +```json |
| 51 | +[ |
| 52 | + { |
| 53 | + "op":"replace", |
| 54 | + "path":"/ConnectionStrings/DefaultConnection", |
| 55 | + "value":"Server=service-postgres;Port=5432;User Id=dbuser;Password=dbpass;Database=dbname;Pooling=true;" |
| 56 | + }, |
| 57 | + { "op":"add", "path":"/SmtpConfig", "value":{ |
| 58 | + "UserName": "SmptUser", |
| 59 | + "Password": "SmptPass", |
| 60 | + "Host": "email-smtp.us-east-1.amazonaws.com", |
| 61 | + "Port": 587, |
| 62 | + |
| 63 | + "FromName": "MyApp", |
| 64 | + |
| 65 | + } |
| 66 | + }, |
| 67 | + { "op": "add", "path": "/Admins", "value": [ "[email protected]", "[email protected]"] }, |
| 68 | + { "op":"add", "path":"/CorsFeature/allowOriginWhitelist/-", "value":"https://servicestack.net" } |
| 69 | +] |
| 70 | +``` |
| 71 | + |
| 72 | +### SMTP Email |
| 73 | + |
| 74 | +Enable email sending by uncommenting in `Program.cs`: |
| 75 | + |
| 76 | +```csharp |
| 77 | +services.AddSingleton<IEmailSender<ApplicationUser>, EmailSender>(); |
| 78 | +``` |
| 79 | + |
| 80 | +## Upgrading to Enterprise Database |
| 81 | + |
| 82 | +To switch from SQLite to PostgreSQL/SQL Server/MySQL: |
| 83 | + |
| 84 | +1. Install preferred RDBMS (ef-postgres, ef-mysql, ef-sqlserver), e.g: |
| 85 | + |
| 86 | +```bash |
| 87 | +npx add-in ef-postgres |
| 88 | +``` |
| 89 | + |
| 90 | +2. Install `db-identity` to use RDBMS `DatabaseJobsFeature` for background jobs and `DbRequestLogger` for Request Logs: |
| 91 | + |
| 92 | +```bash |
| 93 | +npx add-in db-identity |
| 94 | +``` |
| 95 | + |
| 96 | +### App Settings Secrets |
| 97 | + |
| 98 | +You could register any App-specifc secrets here, although our preference is instead of polluting each |
| 99 | +GitHub Reposity with multiple App-specific GitHub Action Secrets, you can save all your secrets in a single |
| 100 | +`APPSETTINGS_PATCH` GitHub Action Secret to patch `appsettings.json` with environment-specific configuration |
| 101 | +using [JSON Patch](https://jsonpatch.com). E.g: |
| 102 | + |
| 103 | +```bash |
| 104 | +# JSON Patch to apply to appsettings.json: |
| 105 | +gh secret set APPSETTINGS_PATCH [json-patch] |
| 106 | +``` |
| 107 | + |
| 108 | +JSON Patch example: |
| 109 | + |
| 110 | +```json |
| 111 | +[ |
| 112 | + { |
| 113 | + "op":"replace", |
| 114 | + "path":"/ConnectionStrings/DefaultConnection", |
| 115 | + "value":"Server=service-postgres;Port=5432;User Id=dbuser;Password=dbpass;Database=dbname" |
| 116 | + }, |
| 117 | + { "op":"add", "path":"/SmtpConfig", "value":{ |
| 118 | + "UserName": "SmptUser", |
| 119 | + "Password": "SmptPass", |
| 120 | + "Host": "email-smtp.us-east-1.amazonaws.com", |
| 121 | + "Port": 587, |
| 122 | + |
| 123 | + "FromName": "MyApp", |
| 124 | + |
| 125 | + } |
| 126 | + }, |
| 127 | + { "op": "add", "path": "/Admins", "value": [ "[email protected]", "[email protected]"] }, |
| 128 | + { "op":"add", "path":"/CorsFeature/allowOriginWhitelist/-", "value":"https://servicestack.net" } |
| 129 | +] |
| 130 | +``` |
| 131 | + |
| 132 | +### Kamal Accessories |
| 133 | + |
| 134 | +[Kamal Accessories](https://kamal-deploy.org/docs/commands/accessory/) allow you to run stateful services (like Databases, Redis, etc) on the same server as your application. This is a cost-effective way to host small-to-medium applications on a single server. |
| 135 | + |
| 136 | +You can define accessories in your `config/deploy.yml`. Here is an example of adding a local PostgreSQL database: |
| 137 | + |
| 138 | +```yaml |
| 139 | +service: acme |
| 140 | +# ... |
| 141 | + |
| 142 | +accessories: |
| 143 | + postgres: |
| 144 | + image: postgres |
| 145 | + # IP address of server to deploy accessory to. (e.g. 100.100.100.100) |
| 146 | + host: <%= ENV['KAMAL_DEPLOY_IP'] %> |
| 147 | + # Expose port 5432 to the host (optional, for external access) |
| 148 | + # port: "5432:5432" |
| 149 | + env: |
| 150 | + clear: |
| 151 | + # Database username and database name |
| 152 | + POSTGRES_USER: app |
| 153 | + POSTGRES_DB: app_db |
| 154 | + # Persist data to a volume |
| 155 | + PGDATA: /var/lib/postgresql/data |
| 156 | + # secrets from .env or GitHub Secrets |
| 157 | + secret: |
| 158 | + - POSTGRES_PASSWORD |
| 159 | + directories: |
| 160 | + # Mount host directories to container for persistence |
| 161 | + - /opt/docker/acme/postgres:/var/lib/postgresql/data |
| 162 | + # Optional: Init scripts |
| 163 | + - /opt/docker/acme/initdb.d:/docker-entrypoint-initdb.d |
| 164 | +``` |
| 165 | +
|
| 166 | +### Referencing Accessories |
| 167 | +
|
| 168 | +When your application runs in Docker on the same server, you can reference the accessory using the Docker service name. |
| 169 | +
|
| 170 | +For Kamal, the service name follows the pattern `<app-service>-<accessory-name>`. |
| 171 | +For example, if your service is named `acme` and accessory is `postgres`, the hostname will be `acme-postgres`. |
| 172 | + |
| 173 | +**Connection String Example:** |
| 174 | + |
| 175 | +``` |
| 176 | +Server=acme-postgres;Port=5432;User Id=app;Password=...;Database=app_db; |
| 177 | +``` |
0 commit comments