generated from gitpod-samples/template-dotnet-core-cli-csharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
48 lines (34 loc) · 1.24 KB
/
Program.cs
File metadata and controls
48 lines (34 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using Microsoft.EntityFrameworkCore;
using ExpressionCalculatorAPI.Data;
var builder = WebApplication.CreateBuilder(args);
// ```
// Add services
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddDbContext<AppDbContext>(options =>
options.UseSqlite(builder.Configuration.GetConnectionString("DefaultConnection")));
var app = builder.Build();
// Apply any pending migrations at startup so the database schema
// matches the code model. This is the recommended production approach.
using (var scope = app.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
db.Database.Migrate();
}
// Enable Swagger only in Development to avoid exposing docs in production.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
// Enable HTTPS redirection only outside of Development environments.
if (!app.Environment.IsDevelopment())
{
app.UseHttpsRedirection();
}
app.UseAuthorization();
// Root: redirect to Swagger UI so visiting the forwarded domain shows a useful page.
app.MapGet("/", () => Results.Redirect("/swagger"));
app.MapControllers();
app.Run();