162 lines
3.0 KiB
Markdown
162 lines
3.0 KiB
Markdown
# DV-005: Variables de Entorno
|
|
|
|
**Proyecto:** Sistema de Registro de Estudiantes
|
|
**Fecha:** 2026-01-07
|
|
|
|
---
|
|
|
|
## 1. Backend (.NET 10)
|
|
|
|
### appsettings.json (Base)
|
|
|
|
```json
|
|
{
|
|
"Logging": {
|
|
"LogLevel": {
|
|
"Default": "Information",
|
|
"Microsoft.AspNetCore": "Warning",
|
|
"HotChocolate": "Warning"
|
|
}
|
|
},
|
|
"AllowedHosts": "*",
|
|
"GraphQL": {
|
|
"MaxExecutionDepth": 5,
|
|
"MaxComplexity": 100
|
|
}
|
|
}
|
|
```
|
|
|
|
### appsettings.Development.json
|
|
|
|
```json
|
|
{
|
|
"ConnectionStrings": {
|
|
"DefaultConnection": "Server=localhost;Database=StudentEnrollment;User Id=sa;Password=YourStrong@Passw0rd;TrustServerCertificate=True"
|
|
},
|
|
"GraphQL": {
|
|
"EnableIntrospection": true
|
|
}
|
|
}
|
|
```
|
|
|
|
### appsettings.Production.json.example
|
|
|
|
```json
|
|
{
|
|
"ConnectionStrings": {
|
|
"DefaultConnection": "Server=PROD_SERVER;Database=StudentEnrollment;User Id=app_user;Password=CHANGE_ME;Encrypt=True"
|
|
},
|
|
"GraphQL": {
|
|
"EnableIntrospection": false
|
|
}
|
|
}
|
|
```
|
|
|
|
---
|
|
|
|
## 2. User Secrets (Desarrollo Local)
|
|
|
|
```bash
|
|
# Inicializar secrets
|
|
cd src/backend/Host
|
|
dotnet user-secrets init
|
|
|
|
# Guardar connection string
|
|
dotnet user-secrets set "ConnectionStrings:DefaultConnection" \
|
|
"Server=localhost;Database=StudentEnrollment;User Id=sa;Password=YourStrong@Passw0rd;TrustServerCertificate=True"
|
|
|
|
# Listar secrets
|
|
dotnet user-secrets list
|
|
|
|
# Remover
|
|
dotnet user-secrets remove "ConnectionStrings:DefaultConnection"
|
|
```
|
|
|
|
---
|
|
|
|
## 3. Frontend (Angular 21)
|
|
|
|
### environment.ts (Desarrollo)
|
|
|
|
```typescript
|
|
export const environment = {
|
|
production: false,
|
|
graphqlUrl: 'https://localhost:5001/graphql',
|
|
apiTimeout: 30000,
|
|
};
|
|
```
|
|
|
|
### environment.prod.ts (Producción)
|
|
|
|
```typescript
|
|
export const environment = {
|
|
production: true,
|
|
graphqlUrl: '/graphql',
|
|
apiTimeout: 15000,
|
|
};
|
|
```
|
|
|
|
---
|
|
|
|
## 4. Docker (.env)
|
|
|
|
### .env.example
|
|
|
|
```env
|
|
# Database
|
|
DB_PASSWORD=YourStrong@Passw0rd
|
|
DB_NAME=StudentEnrollment
|
|
|
|
# API
|
|
ASPNETCORE_ENVIRONMENT=Development
|
|
ASPNETCORE_URLS=http://+:5000
|
|
|
|
# Frontend
|
|
FRONTEND_URL=http://localhost:4200
|
|
```
|
|
|
|
### docker-compose.yml (usando .env)
|
|
|
|
```yaml
|
|
services:
|
|
sqlserver:
|
|
environment:
|
|
- SA_PASSWORD=${DB_PASSWORD}
|
|
|
|
api:
|
|
environment:
|
|
- ASPNETCORE_ENVIRONMENT=${ASPNETCORE_ENVIRONMENT}
|
|
- ConnectionStrings__DefaultConnection=Server=sqlserver;Database=${DB_NAME};User Id=sa;Password=${DB_PASSWORD};TrustServerCertificate=True
|
|
```
|
|
|
|
---
|
|
|
|
## 5. Variables por Ambiente
|
|
|
|
| Variable | Desarrollo | Producción |
|
|
|----------|------------|------------|
|
|
| ConnectionString | localhost | Servidor prod |
|
|
| EnableIntrospection | true | false |
|
|
| LogLevel | Debug | Warning |
|
|
| CORS Origins | localhost:4200 | dominio.com |
|
|
| MaxExecutionDepth | 10 | 5 |
|
|
|
|
---
|
|
|
|
## 6. Cargar Configuración
|
|
|
|
```csharp
|
|
// Program.cs
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
// Orden de carga (último gana):
|
|
// 1. appsettings.json
|
|
// 2. appsettings.{Environment}.json
|
|
// 3. User Secrets (solo Development)
|
|
// 4. Variables de entorno
|
|
// 5. Command line args
|
|
|
|
var connectionString = builder.Configuration
|
|
.GetConnectionString("DefaultConnection");
|
|
```
|