diff --git a/Docs/Mobile/.gitkeep b/Docs/Mobile/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/Docs/Server/.gitkeep b/Docs/Server/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/Docs/Web/.gitkeep b/Docs/Web/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/Mobile/.gitkeep b/Mobile/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/Server/Attributes/.gitkeep b/Server/Attributes/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/Server/Contexts/MongoDbContext.cs b/Server/Contexts/MongoDbContext.cs new file mode 100644 index 0000000..05248af --- /dev/null +++ b/Server/Contexts/MongoDbContext.cs @@ -0,0 +1,35 @@ +using MongoDB.Bson; +using MongoDB.Driver; + +namespace Server.Contexts; + +public static class MongoDBContext +{ + public static MongoClient Client { get; set; } + private static object _lock = new object(); + + static MongoDBContext() + { + if (Client != null) + return; + + lock (_lock) + { + if (Client != null) + return; + + Client = new MongoClient(Settings.MongoConnectionString); + Console.WriteLine("Settings.MongoConnectionString: " + Settings.MongoConnectionString); + } + } + + public static IMongoDatabase GetDatabase(string databaseName) + { + return Client.GetDatabase(databaseName); + } + + public static IMongoCollection GetCollection(string databaseName, string collectionName) + { + return GetDatabase(databaseName).GetCollection(collectionName); + } +} \ No newline at end of file diff --git a/Server/Controllers/BaseController.cs b/Server/Controllers/BaseController.cs new file mode 100644 index 0000000..0f0c059 --- /dev/null +++ b/Server/Controllers/BaseController.cs @@ -0,0 +1,12 @@ +using Microsoft.AspNetCore.Mvc; + +namespace Server.Controllers; + +public class BaseController : ControllerBase +{ + public BaseController() + { + } + + protected ActionResult Forbid(T data) => StatusCode(StatusCodes.Status403Forbidden, data); +} \ No newline at end of file diff --git a/Server/Enums/.gitkeep b/Server/Enums/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/Server/Models/Actions/.gitkeep b/Server/Models/Actions/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/Server/Models/Reactions/.gitkeep b/Server/Models/Reactions/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/Server/MongoServices/.gitkeep b/Server/MongoServices/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/Server/Program.cs b/Server/Program.cs new file mode 100644 index 0000000..e2748a0 --- /dev/null +++ b/Server/Program.cs @@ -0,0 +1,93 @@ +using System.Text.Json; +using Microsoft.AspNetCore.Authentication.JwtBearer; +using Microsoft.IdentityModel.Tokens; +using System.Text; + +var builder = WebApplication.CreateBuilder(args); + +// Add services to the container. +builder.Services.AddControllers() + .AddJsonOptions(options => + { + options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase; + options.JsonSerializerOptions.PropertyNameCaseInsensitive = true; + }); + +// Add HttpClient +builder.Services.AddHttpClient(); + +// Configure JWT Authentication +builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) + .AddJwtBearer(options => + { + var jwtKey = builder.Configuration["Jwt:Key"] ?? throw new Exception("JWT key not found"); + var jwtIssuer = builder.Configuration["Jwt:Issuer"] ?? throw new Exception("JWT issuer not found"); + var jwtAudience = builder.Configuration["Jwt:Audience"] ?? throw new Exception("JWT audience not found"); + + options.TokenValidationParameters = new TokenValidationParameters + { + ValidateIssuerSigningKey = true, + IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtKey)), + ValidateIssuer = true, + ValidateAudience = true, + ValidIssuer = jwtIssuer, + ValidAudience = jwtAudience, + ClockSkew = TimeSpan.Zero + }; + }); + +// Configure settings +Settings.MongoConnectionString = builder.Configuration.GetConnectionString("MongoDB") ?? throw new Exception("MongoDB connection string not found"); +Settings.JwtSettings = new JwtSettings +{ + Key = builder.Configuration["Jwt:Key"] ?? throw new Exception("JWT key not found"), + Issuer = builder.Configuration["Jwt:Issuer"] ?? throw new Exception("JWT issuer not found"), + Audience = builder.Configuration["Jwt:Audience"] ?? throw new Exception("JWT audience not found"), + ExpiryInDays = int.Parse(builder.Configuration["Jwt:ExpiryInDays"] ?? "7") +}; +// Add this line to listen on all interfaces +builder.WebHost.ConfigureKestrel(options => +{ + options.ListenAnyIP(5000); +}); + +var app = builder.Build(); + +// Configure the HTTP request pipeline. +if (!app.Environment.IsDevelopment()) +{ + app.UseExceptionHandler(new ExceptionHandlerOptions + { + AllowStatusCode404Response = true, + ExceptionHandlingPath = new PathString("/api/error") + }); + app.UseHsts(); +} + +app.UseHttpsRedirection(); + +app.UseRouting(); + +app.UseAuthentication(); +app.UseAuthorization(); + +app.MapControllers(); + +app.Run(); + + +return; + +public static class Settings +{ + public static string MongoConnectionString { get; set; } = ""; + public static JwtSettings JwtSettings { get; set; } = new(); +} + +public class JwtSettings +{ + public string Key { get; set; } = ""; + public string Issuer { get; set; } = ""; + public string Audience { get; set; } = ""; + public int ExpiryInDays { get; set; } = 7; +} \ No newline at end of file diff --git a/Server/Properties/launchSettings.json b/Server/Properties/launchSettings.json new file mode 100644 index 0000000..991ba6c --- /dev/null +++ b/Server/Properties/launchSettings.json @@ -0,0 +1,23 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": false, + "applicationUrl": "http://localhost:5280", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": false, + "applicationUrl": "https://localhost:7015;http://localhost:5280", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/Server/Requests/.gitkeep b/Server/Requests/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/Server/Responses/.gitkeep b/Server/Responses/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/Server/Server.csproj b/Server/Server.csproj new file mode 100644 index 0000000..e8c1d30 --- /dev/null +++ b/Server/Server.csproj @@ -0,0 +1,21 @@ + + + + net8.0 + enable + enable + linux-x64 + + + + + + + + + + + + + + diff --git a/Server/Services/.gitkeep b/Server/Services/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/Server/appsettings.Development.json b/Server/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/Server/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/Tests/Mobile/.gitkeep b/Tests/Mobile/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/Tests/Server/.gitkeep b/Tests/Server/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/Tests/Web/.gitkeep b/Tests/Web/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/Web/.gitkeep b/Web/.gitkeep new file mode 100644 index 0000000..e69de29