update
This commit is contained in:
parent
05916b9e1e
commit
d0979ebd5b
2 changed files with 40 additions and 38 deletions
|
@ -34,40 +34,53 @@ static class Program
|
||||||
|
|
||||||
static Router CreateRouter()
|
static Router CreateRouter()
|
||||||
{
|
{
|
||||||
Router root = new Router(NotFound);
|
//The order here matters
|
||||||
root.Use("*", new CORS());
|
Router router = new Router();
|
||||||
root.Add(HttpMethod.GET, "redirect", Redirect);
|
router.Add(new CORS());
|
||||||
root.Static("www-static");
|
router.Add("/variables/:param1/:param2", Variables);
|
||||||
root.Static("www-dynamic");
|
router.Add("/:file", Static.Create("www-static", "file"));
|
||||||
root.Add(HttpMethod.GET, "", Root);
|
router.Add("/:file", Static.Create("www-dynamic", "file"));
|
||||||
|
router.Add("/", Root);
|
||||||
Router nested = new Router(NotFound);
|
router.SetDefault(NotFound);
|
||||||
nested.Add(HttpMethod.GET, "example1", Nested1);
|
return router;
|
||||||
nested.Add(HttpMethod.GET, "example2", Nested2);
|
|
||||||
root.Use("nested", nested);
|
|
||||||
|
|
||||||
return root;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static async Task<HttpResponse?> Root(HttpRequest client, VariableCollection parameters)
|
/// <summary>
|
||||||
|
/// Root endpoint: /
|
||||||
|
/// </summary>
|
||||||
|
static async Task<HttpResponse?> Root(HttpRequest req, RouteMatch route)
|
||||||
{
|
{
|
||||||
if (client.Headers.TryGetValue("Upgrade", out string? connection) && connection.Equals("websocket", StringComparison.OrdinalIgnoreCase))
|
if (req.IsWebsocket)
|
||||||
return await Websocket(client);
|
return await Websocket(req, route);
|
||||||
else
|
|
||||||
return new OK(new HttpContent("text/html", await File.ReadAllBytesAsync("www-static/index.htm")));
|
byte[] indexFile = await File.ReadAllBytesAsync("www-static/index.htm");
|
||||||
|
HttpContent html = new HttpContent("text/html", indexFile);
|
||||||
|
return new OK(html);
|
||||||
}
|
}
|
||||||
|
|
||||||
static Redirect Redirect(HttpRequest client, VariableCollection parameters)
|
/// <summary>
|
||||||
{
|
/// HTTP 404
|
||||||
return new Redirect("/");
|
/// </summary>
|
||||||
}
|
static NotFound NotFound(HttpRequest req, RouteMatch route)
|
||||||
|
|
||||||
static NotFound NotFound(HttpRequest client, VariableCollection parameters)
|
|
||||||
{
|
{
|
||||||
return new NotFound("File not found");
|
return new NotFound("File not found");
|
||||||
}
|
}
|
||||||
|
|
||||||
static async Task<Empty> Websocket(HttpRequest client)
|
/// <summary>
|
||||||
|
/// Variables endpoint
|
||||||
|
/// </summary>
|
||||||
|
static HttpResponse? Variables(HttpRequest req, RouteMatch route)
|
||||||
|
{
|
||||||
|
string? param1 = route.GetVariable("param1");
|
||||||
|
string? param2 = route.GetVariable("param1");
|
||||||
|
|
||||||
|
return new OK($"Variable 1: {param1}\nVariable 2: {param2}");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Websocket endpoint
|
||||||
|
/// </summary>
|
||||||
|
static async Task<Empty> Websocket(HttpRequest client, RouteMatch route)
|
||||||
{
|
{
|
||||||
Websocket? ws = await client.UpgradeToWebsocket("test");
|
Websocket? ws = await client.UpgradeToWebsocket("test");
|
||||||
if (ws == null)
|
if (ws == null)
|
||||||
|
@ -75,10 +88,9 @@ static class Program
|
||||||
|
|
||||||
DataFrame payload = await ws.Read();
|
DataFrame payload = await ws.Read();
|
||||||
|
|
||||||
string result = payload.AsString();
|
|
||||||
|
|
||||||
if (payload.Opcode != WSOpcode.Close)
|
if (payload.Opcode != WSOpcode.Close)
|
||||||
{
|
{
|
||||||
|
string result = payload.AsString();
|
||||||
await ws.Write(true, $"Echoing message: \"{result}\"");
|
await ws.Write(true, $"Echoing message: \"{result}\"");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -86,14 +98,4 @@ static class Program
|
||||||
|
|
||||||
return new Empty();
|
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 bc8ce1a19af6f398fcb05d597f356adf3e3f2e00
|
Subproject commit 2dacb80d492609d4a4407e85981bbe0447b3435f
|
Loading…
Reference in a new issue