Merge remote-tracking branch 'origin/main' into main

This commit is contained in:
Egidijus Lileika 2021-02-10 18:34:40 +02:00
commit 8399eacabf

View File

@ -13,7 +13,7 @@ namespace CuipodExample
{ {
class Server class Server
{ {
static void Main(string[] args) static int Main(string[] args)
{ {
App app = new App( App app = new App(
"<directory_to_serve>/", // directory to serve "<directory_to_serve>/", // directory to serve
@ -22,28 +22,53 @@ namespace CuipodExample
); );
// Serve files // Serve files
app.OnRequest("/", response => { app.OnRequest("/", (request, response) => {
response.RenderFileContent("index.gmi"); response.RenderFileContent("index.gmi");
}); });
app.OnRequest("/about/", response => { // Input example
response.RenderFileContent("about_me.gmi"); app.OnRequest("/input", (request, response) => {
if (request.Parameters == null)
{
response.SetInputHint("Please enter something: ");
response.Status = StatusCode.Input;
}
else
{
// redirect to show/ route with input parameters
response.SetRedirectURL(request.BaseURL + "/show?" + request.Parameters);
response.Status = StatusCode.RedirectTemp;
}
});
app.OnRequest("/show", (request, response) => {
if (request.Parameters == null)
{
// redirect to input
response.SetRedirectURL(request.BaseURL + "/input");
response.Status = StatusCode.RedirectTemp;
}
else
{
// show what has been entered
response.RenderPlainTextLine("# " + request.Parameters);
}
}); });
// Or dynamically render content // Or dynamically render content
app.OnRequest("/dynamic/content/", response => { app.OnRequest("/dynamic/content", (request, response) => {
response.RenderPlainTextLine("# woah much content!"); response.RenderPlainTextLine("# woah much content!");
response.RenderPlainTextLine("More utilities to render content will come soon!"); response.RenderPlainTextLine("More utilities to render content will come soon!");
}); });
// Optional but nice. In case it is specified and client will do a bad route // Optional but nice. In case it is specified and client will do a bad route
// request we will respond with Success status and render result from this lambda // request we will respond with Success status and render result from this lambda
app.OnBadRequest(response => { app.OnBadRequest((request, response) => {
response.RenderPlainTextLine("# Ohh No!!! Request is bad :("); response.RenderPlainTextLine("# Ohh No!!! Request is bad :(");
}); });
app.Run(); return app.Run();
} }
} }
} }
``` ```