Skip to content

Commit

Permalink
Merge pull request #15 from lreb/releases/5.0.0-preview
Browse files Browse the repository at this point in the history
Releases/5.0.0 preview -api working in net 5
  • Loading branch information
lreb authored Feb 2, 2021
2 parents 93bbc8b + 49b8e28 commit ee92b14
Show file tree
Hide file tree
Showing 102 changed files with 3,577 additions and 66 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -435,4 +435,6 @@ $RECYCLE.BIN/

# Facware Base API
FacwareBase.API/AWSLambda/
FacwareBase.API/PublishAWSLambda/
FacwareBase.API/PublishAWSLambda/

src/.vscode
37 changes: 37 additions & 0 deletions Facware.Api/Extensions/CorsExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace Facware.Api.Extensions
{
/// <summary>
/// Extension to setup CORS configuration
/// </summary>
public static class CorsExtension
{
/// <summary>
/// Policy cors name
/// </summary>
public static readonly string AllowSpecificOrigins = "AllowSpecificOrigins";

/// <summary>
/// CORS configurations
/// </summary>
/// <param name="services">application service <see cref="IServiceCollection"/></param>
/// <param name="configuration">app settings configuration <see cref="IConfiguration"/></param>
public static void ConfigureCors(this IServiceCollection services, IConfiguration configuration)
{
services.AddCors(options =>
{
options.AddPolicy(AllowSpecificOrigins,
builder =>
{
builder.WithOrigins(configuration.GetSection("Cors:AllowedOrigin").Get<string[]>())
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials();
});
});
}
}
}
30 changes: 30 additions & 0 deletions Facware.Api/Extensions/DataBaseExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Facware.Data.Access;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace Facware.Api.Extensions
{
/// <summary>
/// The Service Collection Extensions
/// </summary>
/// <seealso cref="IServiceCollection"/>
public static class DatabaseExtension
{
/// <summary>
/// Set up the Service PostgreSQL DB Context
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/></param>
/// <param name="configuration">app settings configuration <see cref="IConfiguration"/></param>
public static void UsePostgreSqlServer(this IServiceCollection services, IConfiguration configuration)
{
// https://www.npgsql.org/efcore/api/Microsoft.Extensions.DependencyInjection.NpgsqlServiceCollectionExtensions.html#Microsoft_Extensions_DependencyInjection_NpgsqlServiceCollectionExtensions_AddEntityFrameworkNpgsql_IServiceCollection_
// https://www.npgsql.org/efcore/index.html#additional-configuration-for-aspnet-core-applications

services.AddDbContext<FacwareDbContext>(options =>
options.UseNpgsql(
configuration["DbContextSettings:ConnectionString"],
x => x.MigrationsAssembly("Facware.Data.Access")));
}
}
}
10 changes: 10 additions & 0 deletions Facware.Api/Extensions/DependencyInjectionExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
namespace Facware.Api.Extensions
{
public class DependencyInjectionExtension
{
public DependencyInjectionExtension()
{
}
}
}
41 changes: 41 additions & 0 deletions Facware.Api/Extensions/HostingEnvironmentExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;

namespace Facware.Api.Extensions
{
/// <summary>
/// Custom environments extension
/// </summary>
public static class HostingEnvironmentExtension
{
/// <summary>
/// Quality assurance environment
/// </summary>
private const string QualityAssurance = "QualityAssurance";
/// <summary>
/// Local environment
/// </summary>
private const string Local = "Local";

/// <summary>
/// Checks if the current host environment name is quality assurance.
/// </summary>
/// <param name="hostingEnvironment">An instance of <see cref="IWebHostEnvironment"/>.</param>
/// <returns>True if the environment name is quality assurance.</returns>
public static bool IsQA(this IWebHostEnvironment hostingEnvironment)
{
return hostingEnvironment.IsEnvironment(QualityAssurance);
}

/// <summary>
/// Checks if the current host environment name is Local.
/// </summary>
/// <param name="hostingEnvironment">An instance of <see cref="IWebHostEnvironment"/>.</param>
/// <returns>True if the environment name is local</returns>
public static bool IsLocal(this IWebHostEnvironment hostingEnvironment)
{
return hostingEnvironment.IsEnvironment(Local);
}
}
}
60 changes: 60 additions & 0 deletions Facware.Api/Extensions/SwaggerExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.IO;
using System.Reflection;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.OpenApi.Models;

namespace Facware.Api.Extensions
{
/// <summary>
/// Swagger extension
/// </summary>
public static class SwaggerExtension
{
#region Swagger Configuration
/// <summary>
/// Method to configure the Swagger Services within the Application services interface
/// </summary>
/// <param name="services">The Service Collection <see cref="IServiceCollection"/></param>
/// <param name="config">The Service Collection <see cref="IConfiguration"/></param>
public static void ConfigureSwaggerExtension(this IServiceCollection services, IConfiguration config)
{
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo
{
Title = config["SwaggerConfiguration:Title"],
Version = config["SwaggerConfiguration:Version"],
Description = config["SwaggerConfiguration:Description"]
});

// Set the comments path for the Swagger JSON and UI.
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);

c.IncludeXmlComments(xmlPath);
});
}
#endregion

/// <summary>
/// Enable Swagger pipeline
/// </summary>
/// <param name="app">application configuration <see cref="IApplicationBuilder"/></param>
/// <param name="config">application settings <see cref="IConfiguration"/></param>
public static void EnableSwaggerPipeline(this IApplicationBuilder app, IConfiguration config)
{
app.UseSwagger();
app.UseSwaggerUI(option =>
{
option.SwaggerEndpoint(
config["SwaggerConfiguration:SwaggerJSONEndpoints"],
$"{config["SwaggerConfiguration:Title"]} {config["SwaggerConfiguration:Version"]}");
// To serve the Swagger UI at the apps root
option.RoutePrefix = string.Empty;
});
}
}
}
8 changes: 8 additions & 0 deletions Facware.Api/Facware.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>$(NoWarn);1591</NoWarn>
</PropertyGroup>

<ItemGroup>
Expand All @@ -18,5 +20,11 @@
<ProjectReference Include="..\Facware.Data.Access\Facware.Data.Access.csproj">
<GlobalPropertiesToRemove></GlobalPropertiesToRemove>
</ProjectReference>
<ProjectReference Include="..\Facware.Extensions\Facware.Extensions.csproj">
<GlobalPropertiesToRemove></GlobalPropertiesToRemove>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Folder Include="Extensions\" />
</ItemGroup>
</Project>
4 changes: 2 additions & 2 deletions Facware.Api/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
"ASPNETCORE_ENVIRONMENT": "Local"
}
},
"Facware.Api": {
Expand All @@ -23,7 +23,7 @@
"launchUrl": "swagger",
"applicationUrl": "https://localhost:60832;http://localhost:56470",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
"ASPNETCORE_ENVIRONMENT": "Local"
}
}
}
Expand Down
65 changes: 45 additions & 20 deletions Facware.Api/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Facware.Data.Access;
using Facware.Api.Extensions;
using Facware.Data.Access.Base.Base;
using Facware.Data.Access.Repository.Implementation;
using Facware.Data.Access.Repository.Interface;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;

namespace Facware.Api
Expand All @@ -31,18 +23,36 @@ public Startup(IConfiguration configuration)
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
#region Swagger service
// enable swagger service
services.ConfigureSwaggerExtension(_configuration);
#endregion

services.AddControllers();
services.AddSwaggerGen(c =>
/*services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "Facware.Api", Version = "v1" });
});
});*/

var connectionString = _configuration["DbContextSettings:ConnectionString"];
services.AddDbContext<FacwareDbContext>(options =>
//var connectionString = _configuration["DbContextSettings:ConnectionString"];
/* services.AddDbContext<FacwareDbContext>(options =>
options.UseNpgsql(
connectionString,
x => x.MigrationsAssembly("Facware.Data.Access")));
x => x.MigrationsAssembly("Facware.Data.Access"))); */


#region Database context service
// TODO: before use this, create your own dbcontext
services.UsePostgreSqlServer(_configuration);

// in memory db
// services.UseInMemoryDatabase();
#endregion

#region Cors service
// enable policy cors service
services.ConfigureCors(_configuration);
#endregion

services.AddTransient(typeof(IGenericRepository<>), typeof(GenericRepository<>));
services.AddTransient<IItemRepository, ItemRepository>();
Expand All @@ -52,11 +62,26 @@ public void ConfigureServices(IServiceCollection services)
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Facware.Api v1"));
// Cors pipe
app.UseCors(CorsExtension.AllowSpecificOrigins);
// handle several environments
if (env.IsLocal())
{
app.UseDeveloperExceptionPage();
app.EnableSwaggerPipeline(_configuration);
}
else if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.EnableSwaggerPipeline(_configuration);
}
else if (env.IsStaging())
{
app.EnableSwaggerPipeline(_configuration);
}
else
{
app.EnableSwaggerPipeline(_configuration);
}

app.UseHttpsRedirection();
Expand Down
30 changes: 30 additions & 0 deletions Facware.Api/appsettings.Local.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"DbContextSettings": {
"ConnectionString": "Server=127.0.0.1;Port=5432;Database=facware;User Id=postgres;Password=postgres;"
},
"Cors": {
"AllowedOrigin": [
"http://localhost:4200",
"http://localhost:4201",
"http://localhost:4202"
]
},
"SwaggerConfiguration": {
"SwaggerJSONEndpoints": "swagger/v1/swagger.json",
"Title": "Facware Base",
"Version": "V1.0",
"Description": "Facware base project.",
"TermsOfService": "https://luisespinoza.facware.com/",
"ContactName": "luis.espinoza@afacware.com respinozabarboza@gmail.com",
"ContactEmail": "luis.espinoza@afacware.com respinozabarboza@gmail.com",
"LicenseName": "MIT",
"LicenseUrl": "https://opensource.org/licenses/MIT"
}
}
12 changes: 12 additions & 0 deletions Facware.Api/appsettings.Production.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"DbContextSettings": {
"ConnectionString": "Server=127.0.0.1;Port=5432;Database=facware;User Id=postgres;Password=postgres;"
}
}
12 changes: 12 additions & 0 deletions Facware.Api/appsettings.Staging.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"DbContextSettings": {
"ConnectionString": "Server=127.0.0.1;Port=5432;Database=facware;User Id=postgres;Password=postgres;"
}
}
Loading

0 comments on commit ee92b14

Please sign in to comment.