-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathProgram.cs
More file actions
77 lines (61 loc) · 2.02 KB
/
Program.cs
File metadata and controls
77 lines (61 loc) · 2.02 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using Scalar.AspNetCore;
using SurrealDb.Examples.MinimalApis.Models;
using SurrealDb.Net;
using Swashbuckle.AspNetCore.SwaggerUI;
// 💡 Be sure to set properties/columns to "camelCase" for PATCH methods to work as expected.
var builder = WebApplication.CreateBuilder(args);
var services = builder.Services;
var configuration = builder.Configuration;
services
.AddEndpointsApiExplorer()
.AddOpenApi(options =>
{
options.AddDocumentTransformer(
(document, _, _) =>
{
document.Info.Title = "MinimalApis Examples API";
document.Info.Version = "v1";
return Task.CompletedTask;
}
);
});
services.AddSurreal(configuration.GetConnectionString("SurrealDB")!);
var app = builder.Build();
app.UseHttpsRedirection();
app.MapGroup("/api")
.MapSurrealEndpoints<WeatherForecast>(
"/weatherForecast",
new() { Tags = ["WeatherForecast"], EnableMutations = false }
)
.MapSurrealEndpoints<Todo>("/todo", new() { Tags = ["Todo"] });
if (app.Environment.IsDevelopment())
{
// 💡 Enable OpenAPI document generation (e.g. "/openapi/v1.json")
app.MapOpenApi();
// 💡 Display OpenAPI User Interfaces (Swagger UI, Scalar)
app.UseSwaggerUI(options =>
{
options.ConfigObject.Urls =
[
new UrlDescriptor { Name = "MinimalApis Examples API v1", Url = "/openapi/v1.json" },
];
});
app.MapScalarApiReference();
}
await InitializeDbAsync();
app.Run();
async Task InitializeDbAsync()
{
const int initialCount = 5;
var weatherForecasts = new WeatherForecastFaker().Generate(initialCount);
var surrealDbClient = new SurrealDbClient(
SurrealDbOptions
.Create()
.FromConnectionString(configuration.GetConnectionString("SurrealDB")!)
.Build()
);
var tasks = weatherForecasts.Select(weatherForecast =>
surrealDbClient.Create(WeatherForecast.Table, weatherForecast)
);
await Task.WhenAll(tasks);
}