-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
120 lines (95 loc) · 3.38 KB
/
Program.cs
File metadata and controls
120 lines (95 loc) · 3.38 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using Microsoft.Extensions.Options;
using Platform;
using Platform.Services;
var builder = WebApplication.CreateBuilder(args);
//builder.Services.AddTransient<IResponseFormatter, GuidService>();
builder.Services.AddScoped<IResponseFormatter, GuidService>();
builder.Services.Configure<MessageOptions>(options =>
{
options.CityName = "Edmonton";
options.CountryName = "Canada";
});
var app = builder.Build();
//app.UseMiddleware<Population>();
//app.UseMiddleware<Capital>();
app.UseMiddleware<WeatherMiddleware>();
IResponseFormatter formatter = new TextResponseFormatter();
app.MapGet("middleware/function", async (HttpContext context, IResponseFormatter formatter) =>
{
await formatter.Format(context, "Middleware function: it is sunny in Jasper");
//await TextResponseFormatter.Singleton.Format(context, "Middleware function: it is sunny in Jasper");
//await formatter.Format(context, "Middleware function: it is sunny in Jasper");
});
//app.MapGet("endpoint/class", WeatherEndpoint.Endpoint);
//app.MapWeather("endpoint/class");
app.MapEndpoint<WeatherEndpoint>("endpoint/class");
app.MapGet("endpoint/function", async (HttpContext context) =>
{
IResponseFormatter formatter = context.RequestServices.GetRequiredService<IResponseFormatter>();
//await context.Response.WriteAsync("Endpoint function: It is sunny in Red Deer");
await formatter.Format(context, "Endpoint Function: it is sunny in Red Deer");
});
app.MapGet("{first}/{second}/{third}", async context =>
{
await context.Response.WriteAsync("Request was routed:\n");
foreach (var kvp in context.Request.RouteValues)
{
await context.Response.WriteAsync($"{kvp.Key}: {kvp.Value}\n");
}
});
app.MapGet("bruh/{item}", Population.NewEndpoint);
app.MapGet("capital/{country}", Capital.Endpoint);
app.MapGet("size/{city}", Population.Endpoint)
.WithMetadata(new RouteNameMetadata("population"));
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapGet("routing", async context =>
{
await context.Response.WriteAsync("Request was routed");
});
endpoints.MapGet("capital/uk", new Capital().Invoke);
endpoints.MapGet("population/paris", new Population().Invoke);
});
app.Run(async (context) =>
{
await context.Response.WriteAsync("Terminal Middleware reached");
});
//app.MapGet("/location", async (HttpContext context, IOptions<MessageOptions> msgOpts) =>
//{
// Platform.MessageOptions opts = msgOpts.Value;
// await context.Response.WriteAsync($"{opts.CityName}, {opts.CountryName}");
//});
app.UseMiddleware<LocationMiddleware>();
app.MapGet("/", () => "Hello World!");
app.Use(async (context, next) =>
{
await next();
await context.Response.WriteAsync($"\nStatus Code: {context.Response.StatusCode}");
});
app.Use(async (context, next) =>
{
if (context.Request.Path == "/short")
{
await context.Response.WriteAsync($"Request short circuited");
}
else
{
await next();
}
});
app.Use(async (ctx, req) =>
{
if (ctx.Request.Method == HttpMethods.Get && ctx.Request.Query["custom"] == "true")
{
ctx.Response.ContentType = "text/plain";
await ctx.Response.WriteAsync("Custom Middleware \n");
}
await req();
});
((IApplicationBuilder) app).Map("/branch", branch =>
{
branch.Run(new Platform.QueryStringMiddleware().Invoke);
});
app.UseMiddleware<Platform.QueryStringMiddleware>();
app.Run();