added c# backend

This commit is contained in:
QkoSad
2024-11-24 19:45:56 +02:00
parent 3b572380bb
commit 4496672d19
83 changed files with 3792 additions and 43 deletions
+75
View File
@@ -0,0 +1,75 @@
using System;
using System.Net;
using System.Text;
using System.Threading;
using MySql.Data.MySqlClient;
using Newtonsoft.Json;
using System.Collections.Generic;
namespace Server
{
class Program
{
static void Main()
{
// create server
HttpListener listener = new HttpListener();
// routes need to be added first
listener.Prefixes.Add("http://localhost:5000/api/getall/");
listener.Prefixes.Add("http://localhost:5000/api/gettopten/");
listener.Prefixes.Add("http://localhost:5000/api/getuser/");
listener.Prefixes.Add("http://localhost:5000/api/reset/");
// Start listening for incoming requests
listener.Start();
Console.WriteLine("Server is listening on http://localhost:5000/");
// god knows what that is
Thread listenerThread = new Thread(() =>
{
while (true)
{
// wait for request
HttpListenerContext context = listener.GetContext();
// get response and request
HttpListenerRequest request = context.Request;
HttpListenerResponse response = context.Response;
// mysql connection
string connectionString = "server=127.0.0.1;uid=monty;pwd=some_pass;database=timelog";
MySqlConnection conn = new MySqlConnection(connectionString);
// url after localhost:5000/
var uri = request.Url.AbsolutePath;
switch (uri)
{
case "/api/reset":
Reset.run(conn, request, response);
break;
case "/api/getall":
Getall.run(conn, request, response);
break;
case "/api/gettopten":
Gettopten.run(conn, request, response);
break;
case "/api/getuser":
Getuser.run(conn, request, response);
break;
default:
response.StatusCode = 404;
string errorMessage = "Not Found";
byte[] buffer = Encoding.UTF8.GetBytes(errorMessage);
response.ContentType = "text/plain";
response.ContentLength64 = buffer.Length;
response.OutputStream.Write(buffer, 0, buffer.Length);
break;
}
// Close the response
response.OutputStream.Close();
}
});
// Start the listener thread
listenerThread.Start();
}
}
}