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()
|
||||
{
|
||||
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);
|
||||
|
||||
Router nested = new Router(NotFound);
|
||||
nested.Add(HttpMethod.GET, "example1", Nested1);
|
||||
nested.Add(HttpMethod.GET, "example2", Nested2);
|
||||
root.Use("nested", nested);
|
||||
|
||||
return root;
|
||||
//The order here matters
|
||||
Router router = new Router();
|
||||
router.Add(new CORS());
|
||||
router.Add("/variables/:param1/:param2", Variables);
|
||||
router.Add("/:file", Static.Create("www-static", "file"));
|
||||
router.Add("/:file", Static.Create("www-dynamic", "file"));
|
||||
router.Add("/", Root);
|
||||
router.SetDefault(NotFound);
|
||||
return router;
|
||||
}
|
||||
|
||||
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))
|
||||
return await Websocket(client);
|
||||
else
|
||||
return new OK(new HttpContent("text/html", await File.ReadAllBytesAsync("www-static/index.htm")));
|
||||
if (req.IsWebsocket)
|
||||
return await Websocket(req, route);
|
||||
|
||||
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)
|
||||
{
|
||||
return new Redirect("/");
|
||||
}
|
||||
|
||||
static NotFound NotFound(HttpRequest client, VariableCollection parameters)
|
||||
/// <summary>
|
||||
/// HTTP 404
|
||||
/// </summary>
|
||||
static NotFound NotFound(HttpRequest req, RouteMatch route)
|
||||
{
|
||||
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");
|
||||
if (ws == null)
|
||||
|
@ -75,10 +88,9 @@ static class Program
|
|||
|
||||
DataFrame payload = await ws.Read();
|
||||
|
||||
string result = payload.AsString();
|
||||
|
||||
if (payload.Opcode != WSOpcode.Close)
|
||||
{
|
||||
string result = payload.AsString();
|
||||
await ws.Write(true, $"Echoing message: \"{result}\"");
|
||||
}
|
||||
|
||||
|
@ -86,14 +98,4 @@ 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 bc8ce1a19af6f398fcb05d597f356adf3e3f2e00
|
||||
Subproject commit 2dacb80d492609d4a4407e85981bbe0447b3435f
|
Loading…
Reference in a new issue