Browse Source

setup enviroment for linux OS

mahmoud 1 month ago
parent
commit
0f249b2246

+ 41 - 0
MTWorkHR.API/.env

@@ -0,0 +1,41 @@
+# Database Settings
+LOCAL_CONNECTION_STRING="Server=localhost;Database=MTWorkHRDB;User=sa;Password=Mahmoud001;MultipleActiveResultSets=true;Encrypt=False"
+
+
+# JWT Settings
+JWT_SECRET_KEY="dkajsdfhalksjdhfalksdjhfalksdjfhaslkdjfhasdlkfhjasdlkfhjasdfhh"
+JWT_AUDIENCE="http://localhost:28222"
+JWT_ISSUER="https://localhost:44325"
+JWT_DURATION_IN_MINUTES=60000
+
+# Cookie Options
+
+SESSION_COOKIE_EXPIRY_IN_SECONDS=3600
+SURVEY_COOKIE_EXPIRY_IN_SECONDS=1800
+
+# Database Config
+DB_RUN_MIGRATIONS=true
+
+# Attachment Settings
+TEMP_ATTACHMENT_PATH="/var/www/html/attachments/temp"
+ACTUAL_ATTACHMENT_PATH="/var/www/html/attachments/actual"
+
+# Mail Settings
+MAIL_API_KEY="your-sendgrid-api-key-here"
+MAIL_FROM_ADDRESS="eng_z@live.com"
+MAIL_FROM_NAME="Hr Management System"
+MAIL_PASSWORD="your-mail-password-here"
+MAIL_HOST="smtp-mail.outlook.com"
+MAIL_PORT=587
+TEMPLATE_PATH="/var/www/html/attachments/actual"
+
+# OTP Settings
+OTP_LENGTH=4
+OTP_EXPIRE_PERIOD_IN_MINUTES=3000
+OTP_SEND_EMAIL=true
+OTP_ALLOW_ZEROS=false
+OTP_MESSAGE_BODY="Dear User, Your OTP is {0}. Use this passcode to proceed."
+OTP_MESSAGE_SUBJECT="OTP"
+
+# General Settings
+ALLOWED_HOSTS="*"

+ 4 - 0
MTWorkHR.API/MTWorkHR.API.csproj

@@ -10,11 +10,15 @@
   </PropertyGroup>
   </PropertyGroup>
 
 
   <ItemGroup>
   <ItemGroup>
+    <PackageReference Include="Azure.Storage.Blobs" Version="12.20.0" />
+    <PackageReference Include="dotenv.net" Version="3.2.0" />
     <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.1">
     <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.1">
       <PrivateAssets>all</PrivateAssets>
       <PrivateAssets>all</PrivateAssets>
       <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
       <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
     </PackageReference>
     </PackageReference>
+    <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
     <PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.5" />
     <PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.5" />
+    <PackageReference Include="Moq" Version="4.20.72" />
     <PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
     <PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
   </ItemGroup>
   </ItemGroup>
 
 

+ 37 - 3
MTWorkHR.API/Program.cs

@@ -13,18 +13,31 @@ using Microsoft.AspNetCore.Mvc.Controllers;
 using Microsoft.OpenApi.Models;
 using Microsoft.OpenApi.Models;
 using MTWorkHR.API.Swagger;
 using MTWorkHR.API.Swagger;
 using Azure.Storage.Blobs;
 using Azure.Storage.Blobs;
+using Moq;
+
+
 using Microsoft.Extensions.DependencyInjection;
 using Microsoft.Extensions.DependencyInjection;
 
 
 var builder = WebApplication.CreateBuilder(args);
 var builder = WebApplication.CreateBuilder(args);
+
+if (OperatingSystem.IsLinux())
+{
+    builder.Configuration.AddJsonFile("appsettings.Linux.json", optional: true, reloadOnChange: true);
+}
+
+
+
 var config = new AppSettingsConfiguration();
 var config = new AppSettingsConfiguration();
 // Add services to the container.
 // Add services to the container.
 builder.Services.AddDbContext<HRDataContext>(options =>
 builder.Services.AddDbContext<HRDataContext>(options =>
 {
 {
     options.UseSqlServer(config.ConnectionStrings.LocalConnectionString);
     options.UseSqlServer(config.ConnectionStrings.LocalConnectionString);
-  //  options.UseSqlServer(builder.Configuration.GetSection("ConnectionStrings:MTWorkHRConnectionString").Value);
+    //  options.UseSqlServer(builder.Configuration.GetSection("ConnectionStrings:MTWorkHRConnectionString").Value);
 });
 });
 
 
 
 
+
+
 builder.Configuration.Bind(config);
 builder.Configuration.Bind(config);
 builder.Services.AddApplicationServices(config);
 builder.Services.AddApplicationServices(config);
 builder.Services.AddInfrastructureIdentityServices(config);
 builder.Services.AddInfrastructureIdentityServices(config);
@@ -32,7 +45,28 @@ builder.Services.AddInfrastructureIdentityServices(config);
 //builder.Services.AddIdentityServices(config);
 //builder.Services.AddIdentityServices(config);
 builder.Services.AddHostedService<DbMigrationService>();
 builder.Services.AddHostedService<DbMigrationService>();
 
 
-builder.Services.AddSingleton(x => new BlobServiceClient(config.ConnectionStrings.BlobConnectionString));
+
+
+
+if (builder.Environment.IsDevelopment())
+{
+    var mockBlobServiceClient = new Mock<BlobServiceClient>();
+
+    // Mock BlobContainerClient since this is what BlobServiceClient interacts with.
+    var mockBlobContainerClient = new Mock<BlobContainerClient>();
+
+    // Set up the mock to return a mock BlobContainerClient when requested.
+    mockBlobServiceClient
+        .Setup(x => x.GetBlobContainerClient(It.IsAny<string>()))
+        .Returns(mockBlobContainerClient.Object);
+
+    builder.Services.AddSingleton(mockBlobServiceClient.Object);
+}
+else
+{
+    // Use the actual connection string for production or other environments.
+    builder.Services.AddSingleton(x => new BlobServiceClient(config.ConnectionStrings.BlobConnectionString));
+}
 
 
 //builder.Services.AddControllers();
 //builder.Services.AddControllers();
 builder.Services.AddControllers(options =>
 builder.Services.AddControllers(options =>
@@ -112,7 +146,7 @@ app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "MTWorkHR.AP
 app.UseCors(x => x
 app.UseCors(x => x
     .AllowAnyMethod()
     .AllowAnyMethod()
     .AllowAnyHeader()
     .AllowAnyHeader()
-    .SetIsOriginAllowed(origin => 
+    .SetIsOriginAllowed(origin =>
     true) // allow any origin
     true) // allow any origin
     .AllowCredentials()); // allow credentials
     .AllowCredentials()); // allow credentials
 app.UseHttpsRedirection();
 app.UseHttpsRedirection();

+ 46 - 0
MTWorkHR.API/appsettings.Linux.json

@@ -0,0 +1,46 @@
+{
+    "Logging": {
+        "LogLevel": {
+            "Default": "Information",
+            "Microsoft.AspNetCore": "Warning"
+        }
+    },
+    "ConnectionStrings": {
+        "LocalConnectionString": "Server=localhost;Database=MTWorkHRDB;User=sa;Password=Mahmoud001;MultipleActiveResultSets=true;Encrypt=False"
+    },
+    "JwtSettings": {
+        "SecretKey": "dkajsdfhalksjdhfalksdjhfalksdjfhaslkdjfhasdlkfhjasdlkfhasdlkfhjasdfhh",
+        "Audience": "http://localhost:28222",
+        "Issuer": "https://localhost:44325",
+        "DurationInMinutes": 60000
+    },
+    "CookieOptions": {
+        "SessionCookieExpiryInSeconds": 3600,
+        "SurveyCookieExpiryInSeconds": 1800
+    },
+    "DbConfig": {
+        "RunMigrations": true
+    },
+    "AttachmentSettings": {
+        "TempAttachment": "/var/www/html/attachments/temp",
+        "ActualAttachment": "/var/www/html/attachments/actual"
+    },
+    "MailSettings": {
+        "ApiKey": "SendGrid-Key",
+        "FromAddress": "eng_z@live.com",
+        "FromName": "Hr Management System",
+        "Password": "111111111111",
+        "Host": "smtp-mail.outlook.com",
+        "Port": 587,
+        "TemplatePath": "/var/www/html/attachments/mailtemp"
+    },
+    "OTPSettings": {
+        "Length": 4,
+        "ExpirePeriodInMinutes": 3000,
+        "SendEmail": true,
+        "AllowZeros": false,
+        "MessageBody": "Dear User, Your OTP is {0}. Use this passcode to proceed.",
+        "MessageSubject": "OTP"
+    },
+    "AllowedHosts": "*"
+}

+ 9 - 1
MTWorkHR.Application/Middlewares/LoggingMiddleware.cs

@@ -15,6 +15,7 @@ using Microsoft.AspNetCore.Mvc;
 using MTWorkHR.Core.UnitOfWork;
 using MTWorkHR.Core.UnitOfWork;
 using MTWorkHR.Application.Services;
 using MTWorkHR.Application.Services;
 using MTWorkHR.Core.Global;
 using MTWorkHR.Core.Global;
+using Microsoft.Extensions.Logging;
 
 
 namespace MTWorkHR.Application.Middlewares
 namespace MTWorkHR.Application.Middlewares
 {
 {
@@ -22,9 +23,14 @@ namespace MTWorkHR.Application.Middlewares
     {
     {
         private readonly RequestDelegate _next;
         private readonly RequestDelegate _next;
 
 
-        public LoggingMiddleware(RequestDelegate next)
+        private readonly ILogger<LoggingMiddleware> _logger;
+
+
+        public LoggingMiddleware(RequestDelegate next, ILogger<LoggingMiddleware> logger)
         {
         {
             _next = next;
             _next = next;
+            _logger = logger;
+
         }
         }
 
 
         public async Task Invoke(HttpContext context, IUnitOfWorkLog unitOfWork)
         public async Task Invoke(HttpContext context, IUnitOfWorkLog unitOfWork)
@@ -36,6 +42,8 @@ namespace MTWorkHR.Application.Middlewares
             catch (Exception error)
             catch (Exception error)
             {
             {
                 //Get target controller info
                 //Get target controller info
+                _logger.LogError(error, "An error occurred during request processing");
+
                 var controllerActionDescriptor = context?
                 var controllerActionDescriptor = context?
                     .GetEndpoint()?
                     .GetEndpoint()?
                     .Metadata
                     .Metadata