save_everything

This commit is contained in:
Dmitrii Kollerov 2023-03-22 12:02:46 +07:00
parent 388ebd964b
commit 5d280daf74
8 changed files with 69 additions and 39 deletions

View File

@ -7,10 +7,11 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.CommandLineUtils" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="6.0.0" />
<PackageReference Include="System.Data.SQLite" Version="1.0.115.5" />
<PackageReference Include="System.Data.SQLite.EF6" Version="1.0.115.5" />
<PackageReference Include="System.Data.SQLite.Linq" Version="1.0.115.5" />
<PackageReference Include="System.Net.Http" Version="4.3.4" />
</ItemGroup>
<ItemGroup>

View File

@ -4,19 +4,22 @@ using Cuipod;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
namespace Chatter.Logic.Actions
{
public static class ListActionManager
public class ListActionManager
{
public static Action<Request, Response, ILogger<App>> GetListAction() =>
(request, response, logger) => {
var service = new MessageService();
public readonly IMessageService _messageService;
IEnumerable<MessageModel> messages = service.GetMessages();
public ListActionManager(IMessageService messageService)
{
_messageService = messageService;
}
public Action<Request, Response, ILogger<App>> GetListAction() =>
(request, response, logger) => {
IEnumerable<MessageModel> messages = _messageService.GetMessages();
response.RenderPlainTextLine("# Chatter");
response.RenderPlainTextLine("");
@ -47,7 +50,7 @@ namespace Chatter.Logic.Actions
//todo: pagination here?
};
public static Action<Request, Response, ILogger<App>> PostAction() =>
public Action<Request, Response, ILogger<App>> PostAction() =>
(request, response, logger) => {
if (string.IsNullOrEmpty(request.Parameters))
{
@ -56,17 +59,23 @@ namespace Chatter.Logic.Actions
}
else
{
// redirect to show/ route with input parameters
var model = new PostModel()
{
Text = request.Parameters
Text = WebUtility.UrlDecode(request.Parameters)
};
var service = new MessageService();
service.AddMessage(model);
_messageService.AddMessage(model);
response.SetRedirectURL(request.BaseURL + "/list");
response.Status = StatusCode.RedirectTemp;
}
};
public Action<Request, Response, ILogger<App>> LoginAction() =>
(request, response, logger) => {
response.RenderPlainTextLine("Cert required");
response.Status = StatusCode.ClientCertRequired;
};
}
}

View File

@ -1,6 +1,10 @@
using Chatter.Logic.Actions;
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;
@ -45,6 +49,17 @@ namespace Chatter
}
}
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 =>
@ -57,6 +72,8 @@ namespace Chatter
.SetMinimumLevel(LogLevel.Debug)
);
using var serviceProvider = GetServiceProvider();
ILogger<App> logger = loggerFactory.CreateLogger<App>();
App app = new App(
@ -70,8 +87,9 @@ namespace Chatter
response.RenderFileContent("index.gmi");
});
app.OnRequest("/list", ListActionManager.GetListAction());
app.OnRequest("/post", ListActionManager.PostAction());
app.OnRequest("/list", serviceProvider.GetService<ListActionManager>().GetListAction());
app.OnRequest("/post", serviceProvider.GetService<ListActionManager>().PostAction());
app.OnRequest("/login", serviceProvider.GetService<ListActionManager>().LoginAction());
app.Run();

View File

@ -20,24 +20,19 @@ namespace Chatter.Repository
{
private readonly IDbConnector _connector;
public MessageRepository()
public MessageRepository(IDbConnector connector)
{
_connector = connector;
}
public void AddMessage(MessageDto msg)
{
using (var connector = new SqLiteConnector())
{
connector.AddMessage(msg);
}
_connector.AddMessage(msg);
}
public IEnumerable<MessageDto> GetMessages()
{
using (var connector = new SqLiteConnector())
{
return connector.GetMessages();
}
return _connector.GetMessages();
}
}
}

View File

@ -20,9 +20,9 @@ namespace Chatter.Service
{
public readonly IMessageRepository _repository;
public MessageService()
public MessageService(IMessageRepository repository)
{
_repository = new MessageRepository();
_repository = repository;
}
public IEnumerable<MessageModel> GetMessages()

View File

@ -1,9 +1,7 @@
# Chatter gemini app
This is chatter gemini app. An application can be used for chat with several different users. The idea of the project is to have the least amount of data stored permanently.
## Requirements:
* redis -- messages
* nosql database -- permanent userdata
This is chatter gemini app. An application can be used for chat with several different users. The idea of the project is to have the least amount of data stored permanently. This project uses sqlite in-memory database to store messages which means every time you reload it you lose all data.
=> /list Message list
=> /post Post message

View File

@ -5,9 +5,7 @@ VisualStudioVersion = 17.0.31919.166
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Cuipod", "Cuipod\Cuipod.csproj", "{2B6AD5A5-F10B-4FC3-BF12-5DD26FAF3796}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CuipodExample", "CuipodExample\CuipodExample.csproj", "{BD343B0B-29EB-4498-8CD2-D9483B6BDA98}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Chatter", "Chatter\Chatter.csproj", "{20C8B6F9-63FB-458D-921E-5DFD6EBE4435}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Chatter", "Chatter\Chatter.csproj", "{20C8B6F9-63FB-458D-921E-5DFD6EBE4435}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -19,10 +17,6 @@ Global
{2B6AD5A5-F10B-4FC3-BF12-5DD26FAF3796}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2B6AD5A5-F10B-4FC3-BF12-5DD26FAF3796}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2B6AD5A5-F10B-4FC3-BF12-5DD26FAF3796}.Release|Any CPU.Build.0 = Release|Any CPU
{BD343B0B-29EB-4498-8CD2-D9483B6BDA98}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BD343B0B-29EB-4498-8CD2-D9483B6BDA98}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BD343B0B-29EB-4498-8CD2-D9483B6BDA98}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BD343B0B-29EB-4498-8CD2-D9483B6BDA98}.Release|Any CPU.Build.0 = Release|Any CPU
{20C8B6F9-63FB-458D-921E-5DFD6EBE4435}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{20C8B6F9-63FB-458D-921E-5DFD6EBE4435}.Debug|Any CPU.Build.0 = Debug|Any CPU
{20C8B6F9-63FB-458D-921E-5DFD6EBE4435}.Release|Any CPU.ActiveCfg = Release|Any CPU

View File

@ -100,7 +100,22 @@ namespace Cuipod
private Response ProcessRequest(SslStream sslStream)
{
sslStream.ReadTimeout = 5000;
sslStream.AuthenticateAsServer(_serverCertificate, false, SslProtocols.Tls12 | SslProtocols.Tls13, false);
sslStream.AuthenticateAsServer(new SslServerAuthenticationOptions()
{
ServerCertificate = _serverCertificate,
EnabledSslProtocols = SslProtocols.Tls12 | SslProtocols.Tls13,
ClientCertificateRequired = false,
CertificateRevocationCheckMode = X509RevocationMode.NoCheck
});
//sslStream.AuthenticateAsServer(_serverCertificate, false, , false);
//var clientCertificate = sslStream.RemoteCertificate;
//var clientCertificateHash = Convert.ToBase64String(clientCertificate.GetCertHash());
//var username = clientCertificate.Issuer;
//if (sslStream.IsMutuallyAuthenticated)
//{
//}
// Read a message from the client.
string rawRequest = ReadRequest(sslStream);