diff --git a/Dockerfile b/Dockerfile index bc6cd5c..ef60bb7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,9 +1,13 @@ FROM mcr.microsoft.com/dotnet/sdk:7.0-alpine AS build +ENV ASPNETCORE_URLS=http://+:80 + WORKDIR /app COPY . . +EXPOSE 80 + RUN dotnet restore "Orion.Api/Orion.Api.csproj" RUN dotnet publish "Orion.Api/Orion.Api.csproj" -c Release -o /out diff --git a/Orion.Api/Startup.cs b/Orion.Api/Bootstraper.cs similarity index 72% rename from Orion.Api/Startup.cs rename to Orion.Api/Bootstraper.cs index 723bfc8..f945de8 100644 --- a/Orion.Api/Startup.cs +++ b/Orion.Api/Bootstraper.cs @@ -1,22 +1,11 @@ -using System; -using System.Collections.Generic; -using System.Globalization; -using System.Text; using AutoMapper; using FluentValidation; using FluentValidation.AspNetCore; using Microsoft.AspNetCore.Authentication.JwtBearer; -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Localization; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Razor; using Microsoft.AspNetCore.Mvc.Versioning; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Logging; using Microsoft.IdentityModel.Tokens; using Microsoft.OpenApi.Models; using Orion.Api.AutoMapper.Config; @@ -24,27 +13,18 @@ using Orion.Api.Middleware; using Orion.Api.Validators; using Orion.Ioc.Dependencies; +using System.Globalization; +using System.Text; namespace Orion.Api { - public class Startup + public static class Bootstraper { - private readonly ILoggerFactory _loggerFactory; - private readonly IWebHostEnvironment _env; - private IConfiguration Configuration { get; set; } - - public Startup(IConfiguration configuration, IWebHostEnvironment env, ILoggerFactory loggerFactory) - { - _loggerFactory = loggerFactory; - _env = env; - Configuration = configuration; - } - - public void ConfigureServices(IServiceCollection services) + public static void ConfigureServices(this IServiceCollection services, IConfiguration configuration) { services.AddCors(); - ConfigureAuthentication(services); + ConfigureAuthentication(services, configuration); services.AddControllers(); @@ -62,7 +42,7 @@ public void ConfigureServices(IServiceCollection services) ConfigureApiVersioning(services); services.AddDomainServices(); - services.AddAutoMapper(typeof(Startup)); + ConfigureMapper(services); } @@ -122,21 +102,10 @@ private static void ConfigureSwagger(IServiceCollection services) }); } - private void ConfigureAuthentication(IServiceCollection services) + private static void ConfigureAuthentication(IServiceCollection services, IConfiguration configuration) { - var jwtConfiguration = Configuration.GetSection("JwtConfiguration").Get(); - - services.AddIdentity( - option => - { - option.Password.RequireDigit = false; - option.Password.RequiredLength = 6; - option.Password.RequireNonAlphanumeric = false; - option.Password.RequireUppercase = false; - option.Password.RequireLowercase = false; - } - ).AddDefaultTokenProviders(); - + var jwtConfiguration = configuration.GetSection("JwtConfiguration").Get(); + services.AddAuthentication(option => { option.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; @@ -157,7 +126,7 @@ private void ConfigureAuthentication(IServiceCollection services) }); } - public void ConfigureMapper(IServiceCollection services) + public static void ConfigureMapper(IServiceCollection services) { var mappingConfig = new MapperConfiguration(mc => { @@ -168,20 +137,8 @@ public void ConfigureMapper(IServiceCollection services) services.AddScoped(_ => mappingConfig.CreateMapper()); } - public void Configure(IApplicationBuilder app, IHostEnvironment env) + public static void ConfigureApp(this IApplicationBuilder app) { - var logger = _loggerFactory.CreateLogger(); - - //ENVIRONMENT - if (env.IsDevelopment()) - { - logger.LogInformation("Development environment"); - } - else - { - logger.LogInformation("Environment: {Env}", _env.EnvironmentName); - app.UseHsts(); - } app.UseMiddleware(); //SWAGGER @@ -201,13 +158,6 @@ public void Configure(IApplicationBuilder app, IHostEnvironment env) app.UseHealthChecks("/health-check"); - var builder = new ConfigurationBuilder() - .SetBasePath(env.ContentRootPath) - .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) - .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) - .AddEnvironmentVariables(); - - Configuration = builder.Build(); app.UseRouting(); app.UseAuthentication(); diff --git a/Orion.Api/Orion.Api.csproj b/Orion.Api/Orion.Api.csproj index 2a04fef..709cdeb 100644 --- a/Orion.Api/Orion.Api.csproj +++ b/Orion.Api/Orion.Api.csproj @@ -1,9 +1,10 @@ - + net7.0 Orion.Api 7401dc57-e892-498d-a628-03091ec95c6a + enable Linux @@ -15,14 +16,7 @@ - - - - - - - all runtime; build; native; contentfiles; analyzers; buildtransitive @@ -30,7 +24,6 @@ - @@ -44,6 +37,10 @@ + + + + Always diff --git a/Orion.Api/Program.cs b/Orion.Api/Program.cs index 761db5e..3fe68bf 100644 --- a/Orion.Api/Program.cs +++ b/Orion.Api/Program.cs @@ -1,43 +1,11 @@ -using System; -using Microsoft.AspNetCore; -using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.Logging; -using NLog; -using NLog.Web; +using Orion.Api; -namespace Orion.Api -{ - public static class Program - { - public static void Main(string[] args) - { - var logger = NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger(); +var builder = WebApplication.CreateBuilder(args); - try - { - CreateWebHostBuilder(args).Build().Run(); - } - catch (Exception ex) - { - logger.Error(ex, "Stopped program because of exception"); - throw; - } - finally - { - LogManager.Shutdown(); - } - } +builder.Services.ConfigureServices(builder.Configuration); - private static IWebHostBuilder CreateWebHostBuilder(string[] args) => - WebHost.CreateDefaultBuilder(args) - .UseUrls("http://*:5000") - .UseStartup() - .UseIISIntegration() - .ConfigureLogging((context, logging) => - { - logging.ClearProviders(); - logging.AddConsole(); - }).UseNLog(); +var app = builder.Build(); - } -} +app.ConfigureApp(); + +app.Run(); diff --git a/Orion.Api/ValidateModelStateAttribute .cs b/Orion.Api/ValidateModelStateAttribute .cs deleted file mode 100644 index e69de29..0000000 diff --git a/Orion.Test/API/HealthCheckApiTest.cs b/Orion.Test/API/HealthCheckApiTest.cs index ff938a0..66e3ef4 100644 --- a/Orion.Test/API/HealthCheckApiTest.cs +++ b/Orion.Test/API/HealthCheckApiTest.cs @@ -11,7 +11,6 @@ public class HealthCheckApiTest: ApiTestInitializer public async Task GetAsync_HealthCheck_ReturnsHealthy() { //arrange - Setup(); var successMessageService = "Healthy"; //act diff --git a/Orion.Test/Configuration/ApiTestInitializer.cs b/Orion.Test/Configuration/ApiTestInitializer.cs index 7f8add7..bc9e3ff 100644 --- a/Orion.Test/Configuration/ApiTestInitializer.cs +++ b/Orion.Test/Configuration/ApiTestInitializer.cs @@ -1,40 +1,51 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc.Testing; +using Microsoft.Extensions.Configuration; using Newtonsoft.Json; +using Orion.Api.Models; +using System; using System.Net.Http; -using System.Net.Http.Headers; using System.Text; -using Orion.Api; -using Orion.Api.Models; namespace Orion.Test.Configuration { - public abstract class ApiTestInitializer : WebApplicationFactory + public abstract class ApiTestInitializer : IDisposable { - protected HttpClient Client; protected string AuthToken; - public ApiTestInitializer() - { + protected readonly HttpClient Client; + protected readonly HttpClient AuthenticatedClient; + protected IServiceProvider ServiceProvider { get; private set; } - } - public void Setup() + public ApiTestInitializer() { - var builder = new WebHostBuilder(); - - base.ConfigureWebHost(builder); - - Client = Server.CreateClient(); + var appFactory = new WebApplicationFactory() + .WithWebHostBuilder(builder => + { + var config = new ConfigurationBuilder() + .AddJsonFile("appsettings.Test.json", optional: false, reloadOnChange: true) + .Build(); - AuthUser(); - - Client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AuthToken); + builder + .UseConfiguration(config) + .ConfigureServices(services => + { + }); + }); + ServiceProvider = appFactory.Services; + Client = appFactory.CreateClient(); + AuthenticatedClient = appFactory.CreateClient(); } public void AuthUser() { - var result = Client.PostAsync("/api/Auth/Login", GetStringContent(new UserLoginModel { Email = "vanderlan.gs@gmail.com", Password = "123" })).GetAwaiter().GetResult(); + var result = Client.PostAsync("/api/Auth/Login", GetStringContent( + new UserLoginModel { + Email = "vanderlan.gs@gmail.com", + Password = "123" + })) + .GetAwaiter().GetResult(); var content = result.Content.ReadAsStringAsync().GetAwaiter().GetResult(); @@ -48,12 +59,25 @@ protected static StringContent GetStringContent(object obj) return new StringContent(JsonConvert.SerializeObject(obj), Encoding.Default, "application/json"); } - protected override void ConfigureWebHost(IWebHostBuilder builder) + private bool _disposedValue = false; + + protected virtual void Dispose(bool disposing) { - builder.UseEnvironment("Test") - .UseStartup(); + if (!_disposedValue) + { + _disposedValue = true; + } + } - base.ConfigureWebHost(builder); + ~ApiTestInitializer() + { + Dispose(false); + } + + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); } } } diff --git a/docker-compose.yaml b/docker-compose.yaml index bfa13b4..16187f5 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -6,8 +6,8 @@ services: context: . dockerfile: Dockerfile ports: - - "8080:5000" - container_name: orion-api + - "5010:80" + container_name: orion networks: - backend-network networks: