update example
This commit is contained in:
parent
8238b9ff39
commit
46c1fbf804
2 changed files with 33 additions and 28 deletions
|
@ -32,55 +32,44 @@ static class Program
|
|||
Task.Delay(-1).Wait();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generates the URL router for the HTTP server.
|
||||
/// </summary>
|
||||
static Router CreateRouter()
|
||||
{
|
||||
//The order here matters
|
||||
Router subpath = new Router();
|
||||
subpath.Add("foo", new StaticEndpoint(new NotFound("A \"foo\" example sub page")));
|
||||
subpath.Add("bar", new StaticEndpoint(new NotFound("A \"bar\" example sub page")));
|
||||
|
||||
Router router = new Router();
|
||||
router.Add(new CORS());
|
||||
router.Add(HttpMethod.GET, "/variables/:param1/:param2", Variables);
|
||||
router.Add(HttpMethod.GET, "/:file", Static.Create("www-static", "file"));
|
||||
router.Add(HttpMethod.GET, "/:file", Static.Create("www-dynamic", "file"));
|
||||
router.Add(HttpMethod.GET, "/", Root);
|
||||
router.SetDefault(NotFound);
|
||||
router.Add("custom", new CustomRoute());
|
||||
router.Add("subpath", subpath);
|
||||
router.Add(new FileEndpoint("www-static"));
|
||||
router.Add(new FileEndpoint("www-dynamic"));
|
||||
router.Add("", Root);
|
||||
router.Add(new StaticEndpoint(new NotFound("File not found")));
|
||||
return router;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Root endpoint: /
|
||||
/// </summary>
|
||||
static async Task<HttpResponse?> Root(HttpRequest req, RouteMatch route)
|
||||
static async Task<HttpResponse?> Root(HttpRequest req)
|
||||
{
|
||||
if (req.IsWebsocket)
|
||||
return await Websocket(req, route);
|
||||
return await Websocket(req);
|
||||
|
||||
byte[] indexFile = await File.ReadAllBytesAsync("www-static/index.htm");
|
||||
HttpContent html = new HttpContent("text/html", indexFile);
|
||||
return new OK(html);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// HTTP 404
|
||||
/// </summary>
|
||||
static NotFound NotFound(HttpRequest req, RouteMatch route)
|
||||
{
|
||||
return new NotFound("File not found");
|
||||
}
|
||||
|
||||
/// <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)
|
||||
static async Task<Empty> Websocket(HttpRequest client)
|
||||
{
|
||||
Websocket? ws = await client.UpgradeToWebsocket("test");
|
||||
if (ws == null)
|
||||
|
@ -98,4 +87,20 @@ static class Program
|
|||
|
||||
return new Empty();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Custom route: /custom/{param1}/{param2}
|
||||
/// </summary>
|
||||
class CustomRoute : RouterBase
|
||||
{
|
||||
public override HttpMethod Method => HttpMethod.GET;
|
||||
public override int Arguments => 2;
|
||||
|
||||
protected override Task<HttpResponse?> GetResponseInner(HttpRequest req, ArraySegment<string> path)
|
||||
{
|
||||
string param1 = path[0];
|
||||
string param2 = path[1];
|
||||
return Task.FromResult<HttpResponse?>(new OK($"Variable 1: {param1}\nVariable 2: {param2}"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
2
MiniHTTP
2
MiniHTTP
|
@ -1 +1 @@
|
|||
Subproject commit 3dd2c79b2174fdc3cf4ff1b1e7bf935c20ecc898
|
||||
Subproject commit d948ab619114de8119b0380128ed25b794d97cbc
|
Loading…
Reference in a new issue