migrate catalog api

This commit is contained in:
Borja García Rodríguez 2020-12-16 18:08:24 +01:00
parent 86e563f76e
commit 792629ae03
6 changed files with 110 additions and 118 deletions

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web"> <Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup> <PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework> <TargetFramework>net5.0</TargetFramework>
<DebugType>portable</DebugType> <DebugType>portable</DebugType>
<PreserveCompilationContext>true</PreserveCompilationContext> <PreserveCompilationContext>true</PreserveCompilationContext>
<AssemblyName>Catalog.API</AssemblyName> <AssemblyName>Catalog.API</AssemblyName>

View File

@ -1,9 +1,9 @@
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS base FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app WORKDIR /app
EXPOSE 80 EXPOSE 80
EXPOSE 443 EXPOSE 443
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src WORKDIR /src
# It's important to keep lines from here down to "COPY . ." identical in all Dockerfiles # It's important to keep lines from here down to "COPY . ." identical in all Dockerfiles

View File

@ -1,9 +1,9 @@
using Autofac.Extensions.DependencyInjection; using Catalog.API.Extensions;
using Catalog.API.Extensions;
using Microsoft.AspNetCore; using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Server.Kestrel.Core; using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF; using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF;
using Microsoft.eShopOnContainers.Services.Catalog.API;
using Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure; using Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
@ -12,30 +12,19 @@ using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Serilog; using Serilog;
using System; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using System.Net; using System.Net;
namespace Microsoft.eShopOnContainers.Services.Catalog.API
{
public class Program
{
public static readonly string Namespace = typeof(Program).Namespace;
public static readonly string AppName = Namespace.Substring(Namespace.LastIndexOf('.', Namespace.LastIndexOf('.') - 1) + 1);
public static int Main(string[] args)
{
var configuration = GetConfiguration(); var configuration = GetConfiguration();
Log.Logger = CreateSerilogLogger(configuration); Log.Logger = CreateSerilogLogger(configuration);
try try
{ {
Log.Information("Configuring web host ({ApplicationContext})...", AppName); Log.Information("Configuring web host ({ApplicationContext})...", Program.AppName);
var host = CreateHostBuilder(configuration, args); var host = CreateHostBuilder(configuration, args);
Log.Information("Applying migrations ({ApplicationContext})...", AppName); Log.Information("Applying migrations ({ApplicationContext})...", Program.AppName);
host.MigrateDbContext<CatalogContext>((context, services) => host.MigrateDbContext<CatalogContext>((context, services) =>
{ {
var env = services.GetService<IWebHostEnvironment>(); var env = services.GetService<IWebHostEnvironment>();
@ -48,23 +37,22 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API
}) })
.MigrateDbContext<IntegrationEventLogContext>((_, __) => { }); .MigrateDbContext<IntegrationEventLogContext>((_, __) => { });
Log.Information("Starting web host ({ApplicationContext})...", AppName); Log.Information("Starting web host ({ApplicationContext})...", Program.AppName);
host.Run(); host.Run();
return 0; return 0;
} }
catch (Exception ex) catch (Exception ex)
{ {
Log.Fatal(ex, "Program terminated unexpectedly ({ApplicationContext})!", AppName); Log.Fatal(ex, "Program terminated unexpectedly ({ApplicationContext})!", Program.AppName);
return 1; return 1;
} }
finally finally
{ {
Log.CloseAndFlush(); Log.CloseAndFlush();
} }
}
private static IWebHost CreateHostBuilder(IConfiguration configuration, string[] args) => IWebHost CreateHostBuilder(IConfiguration configuration, string[] args) =>
WebHost.CreateDefaultBuilder(args) WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration(x => x.AddConfiguration(configuration)) .ConfigureAppConfiguration(x => x.AddConfiguration(configuration))
.CaptureStartupErrors(false) .CaptureStartupErrors(false)
@ -87,13 +75,13 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API
.UseSerilog() .UseSerilog()
.Build(); .Build();
private static Serilog.ILogger CreateSerilogLogger(IConfiguration configuration) Serilog.ILogger CreateSerilogLogger(IConfiguration configuration)
{ {
var seqServerUrl = configuration["Serilog:SeqServerUrl"]; var seqServerUrl = configuration["Serilog:SeqServerUrl"];
var logstashUrl = configuration["Serilog:LogstashgUrl"]; var logstashUrl = configuration["Serilog:LogstashgUrl"];
return new LoggerConfiguration() return new LoggerConfiguration()
.MinimumLevel.Verbose() .MinimumLevel.Verbose()
.Enrich.WithProperty("ApplicationContext", AppName) .Enrich.WithProperty("ApplicationContext", Program.AppName)
.Enrich.FromLogContext() .Enrich.FromLogContext()
.WriteTo.Console() .WriteTo.Console()
.WriteTo.Seq(string.IsNullOrWhiteSpace(seqServerUrl) ? "http://seq" : seqServerUrl) .WriteTo.Seq(string.IsNullOrWhiteSpace(seqServerUrl) ? "http://seq" : seqServerUrl)
@ -102,14 +90,14 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API
.CreateLogger(); .CreateLogger();
} }
private static (int httpPort, int grpcPort) GetDefinedPorts(IConfiguration config) (int httpPort, int grpcPort) GetDefinedPorts(IConfiguration config)
{ {
var grpcPort = config.GetValue("GRPC_PORT", 81); var grpcPort = config.GetValue("GRPC_PORT", 81);
var port = config.GetValue("PORT", 80); var port = config.GetValue("PORT", 80);
return (port, grpcPort); return (port, grpcPort);
} }
private static IConfiguration GetConfiguration() IConfiguration GetConfiguration()
{ {
var builder = new ConfigurationBuilder() var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory()) .SetBasePath(Directory.GetCurrentDirectory())
@ -128,5 +116,9 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API
return builder.Build(); return builder.Build();
} }
}
public static class Program
{
public static string Namespace = typeof(Startup).Namespace;
public static string AppName = Namespace.Substring(Namespace.LastIndexOf('.', Namespace.LastIndexOf('.') - 1) + 1);
} }

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework> <TargetFramework>net5.0</TargetFramework>
<IsPackable>false</IsPackable> <IsPackable>false</IsPackable>
</PropertyGroup> </PropertyGroup>

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework> <TargetFramework>net5.0</TargetFramework>
<GenerateErrorForMissingTargetingPacks>false</GenerateErrorForMissingTargetingPacks> <GenerateErrorForMissingTargetingPacks>false</GenerateErrorForMissingTargetingPacks>
<IsPackable>false</IsPackable> <IsPackable>false</IsPackable>
</PropertyGroup> </PropertyGroup>