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

View File

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

View File

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

View File

@ -182,7 +182,7 @@ namespace NeDvachAPI
Console.WriteLine("Данные обновлены!");
Console.ReadLine();
}
public static void SendPost(Post postToSend) //добавлялка
public static void SendPost(Post postToSend) //sending post to database
{
string connString =
String.Format(
@ -201,7 +201,7 @@ namespace NeDvachAPI
int postNum = 14;
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 (
DEFAULT,
@postText,
@ -209,6 +209,7 @@ namespace NeDvachAPI
@postImgUrl,
@threadId,
@ip,
false,
(
SELECT date_trunc(
'second',

View File

@ -14,7 +14,6 @@ namespace NeDvachAPI
.WithCredentials(AuthInfo.MinIo.username, AuthInfo.MinIo.password)
//.WithSSL()
.Build();
//Console.WriteLine(filename);
Aes aesEncryption = Aes.Create();
aesEncryption.KeySize = 256;
@ -26,9 +25,7 @@ namespace NeDvachAPI
.WithFileName(fileroute)
.WithContentType("image/png");
//.WithServerSideEncryption(ssec);
//Console.WriteLine("Кидаю в minio...");
await DvachIo.PutObjectAsync(putObjectArgs);
//await Task.Delay(5000);
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
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.AddEndpointsApiExplorer();
builder.Services.AddCors(setup =>
{
setup.AddDefaultPolicy(policyBuilder =>
@ -18,24 +17,21 @@ builder.Services.AddCors(setup =>
});
//builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
//app.UseSwagger();
//app.UseSwaggerUI();
}
//app.UseHttpsRedirection();
app.UseCors();
app.UseAuthorization();
app.MapControllers();
app.Run();
if (BufferFill.FillBuffer("b"))
{
app.Run();
}