Added command line parsing for example, fixed non text responses, added request object so you can access parameters and base url
Also, various fixes and improvements
This commit is contained in:
@@ -5,6 +5,10 @@
|
||||
<TargetFramework>netcoreapp3.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.CommandLineUtils" Version="1.1.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Cuipod\Cuipod.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
@@ -1,39 +1,102 @@
|
||||
using Cuipod;
|
||||
using Microsoft.Extensions.CommandLineUtils;
|
||||
using System;
|
||||
|
||||
namespace CuipodExample
|
||||
{
|
||||
class Server
|
||||
{
|
||||
static void Main(string[] args)
|
||||
static int Main(string[] args)
|
||||
{
|
||||
CommandLineApplication commandLineApplication = new CommandLineApplication();
|
||||
commandLineApplication.HelpOption("-h | --help");
|
||||
CommandArgument directoryToServe = commandLineApplication.Argument(
|
||||
"directory",
|
||||
"Directory to server (required)"
|
||||
);
|
||||
CommandArgument certificateFile = commandLineApplication.Argument(
|
||||
"certificate",
|
||||
"Path to certificate (required)"
|
||||
);
|
||||
CommandArgument privateRSAKeyFilePath = commandLineApplication.Argument(
|
||||
"key",
|
||||
"Path to private Pkcs8 RSA key (required)"
|
||||
);
|
||||
commandLineApplication.OnExecute(() =>
|
||||
{
|
||||
if (directoryToServe.Value == null || certificateFile.Value == null || privateRSAKeyFilePath.Value == null)
|
||||
{
|
||||
commandLineApplication.ShowHelp();
|
||||
return 1;
|
||||
}
|
||||
return AppMain(directoryToServe.Value, certificateFile.Value, privateRSAKeyFilePath.Value);
|
||||
});
|
||||
|
||||
try
|
||||
{
|
||||
return commandLineApplication.Execute(args);
|
||||
} catch (Exception e)
|
||||
{
|
||||
Console.WriteLine("Error: {0}", e.Message);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
private static int AppMain(string directoryToServe, string certificateFile, string privateRSAKeyFilePath)
|
||||
{
|
||||
App app = new App(
|
||||
"<directory_to_serve>/", // directory to serve
|
||||
"<dir_with_cert>/certificate.crt", // path to certificate
|
||||
"<dir_with_cert>/privatekey.key" // path to private Pkcs8 RSA key
|
||||
directoryToServe,
|
||||
certificateFile,
|
||||
privateRSAKeyFilePath
|
||||
);
|
||||
|
||||
// Serve files
|
||||
app.OnRequest("/", response => {
|
||||
app.OnRequest("/", (request, response) => {
|
||||
response.RenderFileContent("index.gmi");
|
||||
});
|
||||
|
||||
app.OnRequest("/about/", response => {
|
||||
response.RenderFileContent("about_me.gmi");
|
||||
// Input example
|
||||
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
|
||||
app.OnRequest("/dynamic/content/", response => {
|
||||
app.OnRequest("/dynamic/content", (request, response) => {
|
||||
response.RenderPlainTextLine("# woah much content!");
|
||||
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
|
||||
// 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 :(");
|
||||
});
|
||||
|
||||
app.Run();
|
||||
return app.Run();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user