-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
73 lines (64 loc) · 1.88 KB
/
Copy pathProgram.cs
File metadata and controls
73 lines (64 loc) · 1.88 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
using System.Text.Json.Serialization;
using Configuration.Writable;
using Configuration.Writable.FormatProvider;
using Example.WebApi;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Mvc;
using NSwag.AspNetCore;
var builder = WebApplication.CreateSlimBuilder(args);
// Configuration.Writable
builder.Services.AddWritableOptions(services =>
{
// shared configuration for all options types
services.FormatProvider = new JsonAotFormatProvider(SampleSettingSerializerContext.Default);
// if you want to standard system configuration location, use services.UseStandardSaveDirectory("your-app-id");
// e.g. %APPDATA%\your-app-id\appdata-setting.json on Windows
// services.UseStandardSaveDirectory("your-app-id");
// add SampleSetting
services.Add<SampleSetting>(c =>
{
c.UseFile("appsettings.json");
c.SectionName = "MySetting";
c.WithValidator<SampleSettingValidator>();
});
});
// default Configuration
builder.Services.ConfigureHttpJsonOptions(options =>
{
options.SerializerOptions.TypeInfoResolverChain.Insert(
0,
SampleSettingSerializerContext.Default
);
});
builder.Services.AddOpenApiDocument();
var app = builder.Build();
app.UseOpenApi();
app.UseSwaggerUi(options =>
{
options.Path = "";
});
var configApi = app.MapGroup("/config");
configApi.MapGet(
"/get",
(IReadOnlyOptions<SampleSetting> options) =>
{
var settings = options.CurrentValue;
return settings;
}
);
configApi.MapGet(
"/set/{name}",
async ([FromServices] IWritableOptions<SampleSetting> options, string name) =>
{
try
{
await options.SaveAsync(setting => setting.Name = name);
return Results.Ok();
}
catch (Exception ex)
{
return Results.Text(content: ex.Message, statusCode: 500);
}
}
);
app.Run();