added buffer and some error handling
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
RakVhalate 2022-11-25 12:37:26 +10:00
parent f791625102
commit 50a3c94af8
8 changed files with 81 additions and 28 deletions

37
APIThreadBuffer.cs Normal file
View File

@ -0,0 +1,37 @@
namespace NeDvachAPI
{
public class APIThreadBuffer
{
private static List<Post> ThreadsPrewievs;
private static List<Post>[] Threads = new List<Post>[0];
public static void WriteThreadPreviewsBuffer(List<Post> OPPostsToWrite) //add list of OP-posts and last post to buffer
{
ThreadsPrewievs = OPPostsToWrite;
}
public static List<Post> GetOPPosts()
{
return ThreadsPrewievs;
}
public static void WriteThreadsBuffer(List<Post> ThreadToAdd) //add thread's posts from DB to buffer
{
Threads = Threads.Append(ThreadToAdd).ToArray();
}
public static List<Post> GetThread(int threadId)
{
if (threadId < Threads.Length + 1 & threadId != 0)
{
;
return Threads[threadId - 1];
}
else return null;
}
public static void RefreshThread(int threadId)
{
Threads[threadId - 1] = DBchat.PostsList("b", threadId);
}
}
}

20
BufferFill.cs Normal file
View File

@ -0,0 +1,20 @@
namespace NeDvachAPI
{
public class BufferFill
{
public static bool FillBuffer(string board) //method to get buffer of information, which is being ran while API is started
{
List<Post> posts = DBchat.ThreadsList(board);
APIThreadBuffer.WriteThreadPreviewsBuffer(posts);
Console.WriteLine("Заполняю буфер оп-постами");
for (int threadId = 1; threadId <= 4; threadId++)
{
APIThreadBuffer.WriteThreadsBuffer(DBchat.PostsList(board , threadId));
Console.WriteLine("Добавляю буферизую тред " + threadId);
}
return true;
}
}
}

View File

@ -13,10 +13,7 @@ namespace NeDvachAPI.Controllers
[HttpGet(Name = "GetPosts")] [HttpGet(Name = "GetPosts")]
public string Get([FromUri] string board, int thread) public string Get([FromUri] string board, int thread)
{ {
string ipAddress = Request.HttpContext.Connection.RemoteIpAddress.ToString(); string postsJson = JsonSerializer.Serialize(APIThreadBuffer.GetThread(thread));
List<Post> posts = DBchat.PostsList(board, thread);
string postsJson = JsonSerializer.Serialize(posts);
Console.WriteLine("С адреса " + ipAddress + " запрошен список постов из борды " + board + " и треда# " + thread);
return postsJson ; return postsJson ;
} }
} }

View File

@ -12,8 +12,16 @@ namespace NeDvachAPI.Controllers
{ {
string ipAddress = Request.HttpContext.Connection.RemoteIpAddress.ToString(); string ipAddress = Request.HttpContext.Connection.RemoteIpAddress.ToString();
ReceivedPost.Ip = ipAddress; ReceivedPost.Ip = ipAddress;
DBchat.SendPost(ReceivedPost); try
return new JsonResult("Posted Successfully!"); {
DBchat.SendPost(ReceivedPost);
APIThreadBuffer.RefreshThread(ReceivedPost.Thread_Id);
return new JsonResult("Сообщение успешно добавлено.");
}
catch
{
return new JsonResult("Произошла ошибка постинга");
}
} }
} }
} }

View File

@ -13,11 +13,8 @@ namespace NeDvachAPI.Controllers
[HttpGet(Name = "GetThreads")] [HttpGet(Name = "GetThreads")]
public string Get([FromUri] string board) public string Get([FromUri] string board)
{ {
string ipAddress = Request.HttpContext.Connection.RemoteIpAddress.ToString(); string postsJson = JsonSerializer.Serialize(APIThreadBuffer.GetOPPosts());
List<Post> posts = DBchat.ThreadsList(board); return postsJson;
string postsJson = JsonSerializer.Serialize(posts);
Console.WriteLine("С адреса " + ipAddress + " запрошен список тредов из борды " + board);
return postsJson;
} }
} }
} }

View File

@ -182,7 +182,7 @@ namespace NeDvachAPI
Console.WriteLine("Данные обновлены!"); Console.WriteLine("Данные обновлены!");
Console.ReadLine(); Console.ReadLine();
} }
public static void SendPost(Post postToSend) //добавлялка public static void SendPost(Post postToSend) //sending post to database
{ {
string connString = string connString =
String.Format( String.Format(
@ -201,7 +201,7 @@ namespace NeDvachAPI
int postNum = 14; int postNum = 14;
using (var command = new NpgsqlCommand(@" using (var command = new NpgsqlCommand(@"
INSERT INTO posts (post_id, post_text, post_number, content, thread_id, auth_ip, post_timestamp) INSERT INTO posts (post_id, post_text, post_number, content, thread_id, auth_ip, is_op, post_timestamp)
VALUES ( VALUES (
DEFAULT, DEFAULT,
@postText, @postText,
@ -209,6 +209,7 @@ namespace NeDvachAPI
@postImgUrl, @postImgUrl,
@threadId, @threadId,
@ip, @ip,
false,
( (
SELECT date_trunc( SELECT date_trunc(
'second', 'second',

View File

@ -14,7 +14,6 @@ namespace NeDvachAPI
.WithCredentials(AuthInfo.MinIo.username, AuthInfo.MinIo.password) .WithCredentials(AuthInfo.MinIo.username, AuthInfo.MinIo.password)
//.WithSSL() //.WithSSL()
.Build(); .Build();
//Console.WriteLine(filename);
Aes aesEncryption = Aes.Create(); Aes aesEncryption = Aes.Create();
aesEncryption.KeySize = 256; aesEncryption.KeySize = 256;
@ -26,9 +25,7 @@ namespace NeDvachAPI
.WithFileName(fileroute) .WithFileName(fileroute)
.WithContentType("image/png"); .WithContentType("image/png");
//.WithServerSideEncryption(ssec); //.WithServerSideEncryption(ssec);
//Console.WriteLine("Кидаю в minio...");
await DvachIo.PutObjectAsync(putObjectArgs); await DvachIo.PutObjectAsync(putObjectArgs);
//await Task.Delay(5000);
System.GC.Collect(); System.GC.Collect();
System.GC.WaitForPendingFinalizers(); System.GC.WaitForPendingFinalizers();
File.Delete(fileroute); File.Delete(fileroute);

View File

@ -1,12 +1,11 @@
var builder = WebApplication.CreateBuilder(args); using NeDvachAPI;
// Add services to the container. var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers(); builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer(); builder.Services.AddEndpointsApiExplorer();
builder.Services.AddCors(setup => builder.Services.AddCors(setup =>
{ {
setup.AddDefaultPolicy(policyBuilder => setup.AddDefaultPolicy(policyBuilder =>
@ -18,24 +17,21 @@ builder.Services.AddCors(setup =>
}); });
//builder.Services.AddSwaggerGen();
var app = builder.Build(); var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment()) if (app.Environment.IsDevelopment())
{ {
//app.UseSwagger();
//app.UseSwaggerUI();
} }
//app.UseHttpsRedirection();
app.UseCors(); app.UseCors();
app.UseAuthorization(); app.UseAuthorization();
app.MapControllers(); app.MapControllers();
app.Run(); if (BufferFill.FillBuffer("b"))
{
app.Run();
}