update example
This commit is contained in:
parent
ded802b1b3
commit
05916b9e1e
2 changed files with 28 additions and 13 deletions
|
@ -34,35 +34,40 @@ static class Program
|
|||
|
||||
static Router CreateRouter()
|
||||
{
|
||||
Router router = new Router(NotFound);
|
||||
router.Use("*", new CORS());
|
||||
router.Add(HttpMethod.GET, "test", Test);
|
||||
router.Static("www-static");
|
||||
router.Static("www-dynamic");
|
||||
router.Add(HttpMethod.GET, "", Root);
|
||||
Router root = new Router(NotFound);
|
||||
root.Use("*", new CORS());
|
||||
root.Add(HttpMethod.GET, "redirect", Redirect);
|
||||
root.Static("www-static");
|
||||
root.Static("www-dynamic");
|
||||
root.Add(HttpMethod.GET, "", Root);
|
||||
|
||||
return router;
|
||||
Router nested = new Router(NotFound);
|
||||
nested.Add(HttpMethod.GET, "example1", Nested1);
|
||||
nested.Add(HttpMethod.GET, "example2", Nested2);
|
||||
root.Use("nested", nested);
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
static async Task<HttpResponse?> Root(HttpRequest client, ParameterCollection parameters)
|
||||
static async Task<HttpResponse?> Root(HttpRequest client, VariableCollection parameters)
|
||||
{
|
||||
if (client.Headers.TryGetValue("Upgrade", out string? connection) && connection.Equals("websocket", StringComparison.OrdinalIgnoreCase))
|
||||
return await Websocket(client);
|
||||
else
|
||||
return new HttpContent("text/html", await File.ReadAllBytesAsync("www-static/index.htm"));
|
||||
return new OK(new HttpContent("text/html", await File.ReadAllBytesAsync("www-static/index.htm")));
|
||||
}
|
||||
|
||||
static HttpResponse Test(HttpRequest client, ParameterCollection parameters)
|
||||
static Redirect Redirect(HttpRequest client, VariableCollection parameters)
|
||||
{
|
||||
return new Redirect("/");
|
||||
}
|
||||
|
||||
static HttpResponse NotFound(HttpRequest client, ParameterCollection parameters)
|
||||
static NotFound NotFound(HttpRequest client, VariableCollection parameters)
|
||||
{
|
||||
return new NotFound("File not found");
|
||||
}
|
||||
|
||||
static async Task<HttpResponse> Websocket(HttpRequest client)
|
||||
static async Task<Empty> Websocket(HttpRequest client)
|
||||
{
|
||||
Websocket? ws = await client.UpgradeToWebsocket("test");
|
||||
if (ws == null)
|
||||
|
@ -81,4 +86,14 @@ static class Program
|
|||
|
||||
return new Empty();
|
||||
}
|
||||
|
||||
static OK Nested1(HttpRequest client, VariableCollection parameters)
|
||||
{
|
||||
return new OK("Foo");
|
||||
}
|
||||
|
||||
static OK Nested2(HttpRequest client, VariableCollection parameters)
|
||||
{
|
||||
return new OK("Bar");
|
||||
}
|
||||
}
|
||||
|
|
2
MiniHTTP
2
MiniHTTP
|
@ -1 +1 @@
|
|||
Subproject commit c302da3713a77336b724df395c2cd33e7f650c4a
|
||||
Subproject commit bc8ce1a19af6f398fcb05d597f356adf3e3f2e00
|
Loading…
Reference in a new issue