This repository has been archived on 2023-03-22. You can view files and clone it, but cannot push or open issues or pull requests.
cuipod/Chatter/Program.cs
Dmitrii Kollerov 5d280daf74 save_everything
2023-03-22 12:02:46 +07:00

100 lines
3.3 KiB
C#

using Chatter.Connector;
using Chatter.Logic.Actions;
using Chatter.Repository;
using Chatter.Service;
using Cuipod;
using Microsoft.Extensions.CommandLineUtils;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Security.Cryptography.X509Certificates;
namespace Chatter
{
internal class Program
{
static int Main(string[] args)
{
CommandLineApplication commandLineApplication = new CommandLineApplication();
commandLineApplication.HelpOption("-h | --help");
CommandArgument certificateFile = commandLineApplication.Argument(
"certificate",
"Path to certificate (required)"
);
CommandArgument privateRSAKeyFilePath = commandLineApplication.Argument(
"key",
"Path to private Pkcs8 RSA key (required)"
);
commandLineApplication.OnExecute(() =>
{
if (certificateFile.Value == null || privateRSAKeyFilePath.Value == null)
{
commandLineApplication.ShowHelp();
return 1;
}
X509Certificate2 cert = CertificateUtils.LoadCertificate(certificateFile.Value, privateRSAKeyFilePath.Value);
return AppMain("static/", cert);
});
try
{
return commandLineApplication.Execute(args);
}
catch (Exception e)
{
Console.WriteLine("Error: {0}", e.Message);
return 1;
}
}
private static ServiceProvider GetServiceProvider()
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddSingleton<IDbConnector, SqLiteConnector>();
serviceCollection.AddScoped<IMessageService, MessageService>();
serviceCollection.AddScoped<IMessageRepository, MessageRepository>();
serviceCollection.AddScoped<ListActionManager>();
return serviceCollection.BuildServiceProvider();
}
private static int AppMain(string directoryToServe, X509Certificate2 certificate)
{
using ILoggerFactory loggerFactory = LoggerFactory.Create(builder =>
builder
.AddSimpleConsole(options =>
{
options.SingleLine = true;
options.TimestampFormat = "hh:mm:ss ";
})
.SetMinimumLevel(LogLevel.Debug)
);
using var serviceProvider = GetServiceProvider();
ILogger<App> logger = loggerFactory.CreateLogger<App>();
App app = new App(
directoryToServe,
certificate,
logger
);
// Serve files
app.OnRequest("/", (request, response, logger) => {
response.RenderFileContent("index.gmi");
});
app.OnRequest("/list", serviceProvider.GetService<ListActionManager>().GetListAction());
app.OnRequest("/post", serviceProvider.GetService<ListActionManager>().PostAction());
app.OnRequest("/login", serviceProvider.GetService<ListActionManager>().LoginAction());
app.Run();
return 0;
}
}
}