Skip to content

Commit ee92b14

Browse files
authored
Merge pull request #15 from lreb/releases/5.0.0-preview
Releases/5.0.0 preview -api working in net 5
2 parents 93bbc8b + 49b8e28 commit ee92b14

102 files changed

Lines changed: 3577 additions & 66 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,4 +435,6 @@ $RECYCLE.BIN/
435435

436436
# Facware Base API
437437
FacwareBase.API/AWSLambda/
438-
FacwareBase.API/PublishAWSLambda/
438+
FacwareBase.API/PublishAWSLambda/
439+
440+
src/.vscode
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using Microsoft.Extensions.Configuration;
3+
using Microsoft.Extensions.DependencyInjection;
4+
5+
namespace Facware.Api.Extensions
6+
{
7+
/// <summary>
8+
/// Extension to setup CORS configuration
9+
/// </summary>
10+
public static class CorsExtension
11+
{
12+
/// <summary>
13+
/// Policy cors name
14+
/// </summary>
15+
public static readonly string AllowSpecificOrigins = "AllowSpecificOrigins";
16+
17+
/// <summary>
18+
/// CORS configurations
19+
/// </summary>
20+
/// <param name="services">application service <see cref="IServiceCollection"/></param>
21+
/// <param name="configuration">app settings configuration <see cref="IConfiguration"/></param>
22+
public static void ConfigureCors(this IServiceCollection services, IConfiguration configuration)
23+
{
24+
services.AddCors(options =>
25+
{
26+
options.AddPolicy(AllowSpecificOrigins,
27+
builder =>
28+
{
29+
builder.WithOrigins(configuration.GetSection("Cors:AllowedOrigin").Get<string[]>())
30+
.AllowAnyHeader()
31+
.AllowAnyMethod()
32+
.AllowCredentials();
33+
});
34+
});
35+
}
36+
}
37+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using Facware.Data.Access;
2+
using Microsoft.EntityFrameworkCore;
3+
using Microsoft.Extensions.Configuration;
4+
using Microsoft.Extensions.DependencyInjection;
5+
6+
namespace Facware.Api.Extensions
7+
{
8+
/// <summary>
9+
/// The Service Collection Extensions
10+
/// </summary>
11+
/// <seealso cref="IServiceCollection"/>
12+
public static class DatabaseExtension
13+
{
14+
/// <summary>
15+
/// Set up the Service PostgreSQL DB Context
16+
/// </summary>
17+
/// <param name="services">The <see cref="IServiceCollection"/></param>
18+
/// <param name="configuration">app settings configuration <see cref="IConfiguration"/></param>
19+
public static void UsePostgreSqlServer(this IServiceCollection services, IConfiguration configuration)
20+
{
21+
// https://www.npgsql.org/efcore/api/Microsoft.Extensions.DependencyInjection.NpgsqlServiceCollectionExtensions.html#Microsoft_Extensions_DependencyInjection_NpgsqlServiceCollectionExtensions_AddEntityFrameworkNpgsql_IServiceCollection_
22+
// https://www.npgsql.org/efcore/index.html#additional-configuration-for-aspnet-core-applications
23+
24+
services.AddDbContext<FacwareDbContext>(options =>
25+
options.UseNpgsql(
26+
configuration["DbContextSettings:ConnectionString"],
27+
x => x.MigrationsAssembly("Facware.Data.Access")));
28+
}
29+
}
30+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
namespace Facware.Api.Extensions
3+
{
4+
public class DependencyInjectionExtension
5+
{
6+
public DependencyInjectionExtension()
7+
{
8+
}
9+
}
10+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
using Microsoft.AspNetCore.Hosting;
3+
using Microsoft.Extensions.Hosting;
4+
5+
namespace Facware.Api.Extensions
6+
{
7+
/// <summary>
8+
/// Custom environments extension
9+
/// </summary>
10+
public static class HostingEnvironmentExtension
11+
{
12+
/// <summary>
13+
/// Quality assurance environment
14+
/// </summary>
15+
private const string QualityAssurance = "QualityAssurance";
16+
/// <summary>
17+
/// Local environment
18+
/// </summary>
19+
private const string Local = "Local";
20+
21+
/// <summary>
22+
/// Checks if the current host environment name is quality assurance.
23+
/// </summary>
24+
/// <param name="hostingEnvironment">An instance of <see cref="IWebHostEnvironment"/>.</param>
25+
/// <returns>True if the environment name is quality assurance.</returns>
26+
public static bool IsQA(this IWebHostEnvironment hostingEnvironment)
27+
{
28+
return hostingEnvironment.IsEnvironment(QualityAssurance);
29+
}
30+
31+
/// <summary>
32+
/// Checks if the current host environment name is Local.
33+
/// </summary>
34+
/// <param name="hostingEnvironment">An instance of <see cref="IWebHostEnvironment"/>.</param>
35+
/// <returns>True if the environment name is local</returns>
36+
public static bool IsLocal(this IWebHostEnvironment hostingEnvironment)
37+
{
38+
return hostingEnvironment.IsEnvironment(Local);
39+
}
40+
}
41+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System;
2+
using System.IO;
3+
using System.Reflection;
4+
using Microsoft.AspNetCore.Builder;
5+
using Microsoft.Extensions.Configuration;
6+
using Microsoft.Extensions.DependencyInjection;
7+
using Microsoft.OpenApi.Models;
8+
9+
namespace Facware.Api.Extensions
10+
{
11+
/// <summary>
12+
/// Swagger extension
13+
/// </summary>
14+
public static class SwaggerExtension
15+
{
16+
#region Swagger Configuration
17+
/// <summary>
18+
/// Method to configure the Swagger Services within the Application services interface
19+
/// </summary>
20+
/// <param name="services">The Service Collection <see cref="IServiceCollection"/></param>
21+
/// <param name="config">The Service Collection <see cref="IConfiguration"/></param>
22+
public static void ConfigureSwaggerExtension(this IServiceCollection services, IConfiguration config)
23+
{
24+
services.AddSwaggerGen(c =>
25+
{
26+
c.SwaggerDoc("v1", new OpenApiInfo
27+
{
28+
Title = config["SwaggerConfiguration:Title"],
29+
Version = config["SwaggerConfiguration:Version"],
30+
Description = config["SwaggerConfiguration:Description"]
31+
});
32+
33+
// Set the comments path for the Swagger JSON and UI.
34+
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
35+
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
36+
37+
c.IncludeXmlComments(xmlPath);
38+
});
39+
}
40+
#endregion
41+
42+
/// <summary>
43+
/// Enable Swagger pipeline
44+
/// </summary>
45+
/// <param name="app">application configuration <see cref="IApplicationBuilder"/></param>
46+
/// <param name="config">application settings <see cref="IConfiguration"/></param>
47+
public static void EnableSwaggerPipeline(this IApplicationBuilder app, IConfiguration config)
48+
{
49+
app.UseSwagger();
50+
app.UseSwaggerUI(option =>
51+
{
52+
option.SwaggerEndpoint(
53+
config["SwaggerConfiguration:SwaggerJSONEndpoints"],
54+
$"{config["SwaggerConfiguration:Title"]} {config["SwaggerConfiguration:Version"]}");
55+
// To serve the Swagger UI at the apps root
56+
option.RoutePrefix = string.Empty;
57+
});
58+
}
59+
}
60+
}

Facware.Api/Facware.Api.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
<PropertyGroup>
44
<TargetFramework>net5.0</TargetFramework>
5+
<GenerateDocumentationFile>true</GenerateDocumentationFile>
6+
<NoWarn>$(NoWarn);1591</NoWarn>
57
</PropertyGroup>
68

79
<ItemGroup>
@@ -18,5 +20,11 @@
1820
<ProjectReference Include="..\Facware.Data.Access\Facware.Data.Access.csproj">
1921
<GlobalPropertiesToRemove></GlobalPropertiesToRemove>
2022
</ProjectReference>
23+
<ProjectReference Include="..\Facware.Extensions\Facware.Extensions.csproj">
24+
<GlobalPropertiesToRemove></GlobalPropertiesToRemove>
25+
</ProjectReference>
26+
</ItemGroup>
27+
<ItemGroup>
28+
<Folder Include="Extensions\" />
2129
</ItemGroup>
2230
</Project>

Facware.Api/Properties/launchSettings.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"launchBrowser": true,
1515
"launchUrl": "swagger",
1616
"environmentVariables": {
17-
"ASPNETCORE_ENVIRONMENT": "Development"
17+
"ASPNETCORE_ENVIRONMENT": "Local"
1818
}
1919
},
2020
"Facware.Api": {
@@ -23,7 +23,7 @@
2323
"launchUrl": "swagger",
2424
"applicationUrl": "https://localhost:60832;http://localhost:56470",
2525
"environmentVariables": {
26-
"ASPNETCORE_ENVIRONMENT": "Development"
26+
"ASPNETCORE_ENVIRONMENT": "Local"
2727
}
2828
}
2929
}

Facware.Api/Startup.cs

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Threading.Tasks;
5-
using Facware.Data.Access;
1+
using Facware.Api.Extensions;
62
using Facware.Data.Access.Base.Base;
73
using Facware.Data.Access.Repository.Implementation;
84
using Facware.Data.Access.Repository.Interface;
95
using Microsoft.AspNetCore.Builder;
10-
using Microsoft.AspNetCore.Hosting;
11-
using Microsoft.AspNetCore.HttpsPolicy;
12-
using Microsoft.AspNetCore.Mvc;
13-
using Microsoft.EntityFrameworkCore;
6+
using Microsoft.AspNetCore.Hosting;
147
using Microsoft.Extensions.Configuration;
158
using Microsoft.Extensions.DependencyInjection;
169
using Microsoft.Extensions.Hosting;
17-
using Microsoft.Extensions.Logging;
1810
using Microsoft.OpenApi.Models;
1911

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

3531
services.AddControllers();
36-
services.AddSwaggerGen(c =>
32+
/*services.AddSwaggerGen(c =>
3733
{
3834
c.SwaggerDoc("v1", new OpenApiInfo { Title = "Facware.Api", Version = "v1" });
39-
});
35+
});*/
4036

41-
var connectionString = _configuration["DbContextSettings:ConnectionString"];
42-
services.AddDbContext<FacwareDbContext>(options =>
37+
//var connectionString = _configuration["DbContextSettings:ConnectionString"];
38+
/* services.AddDbContext<FacwareDbContext>(options =>
4339
options.UseNpgsql(
4440
connectionString,
45-
x => x.MigrationsAssembly("Facware.Data.Access")));
41+
x => x.MigrationsAssembly("Facware.Data.Access"))); */
42+
43+
44+
#region Database context service
45+
// TODO: before use this, create your own dbcontext
46+
services.UsePostgreSqlServer(_configuration);
47+
48+
// in memory db
49+
// services.UseInMemoryDatabase();
50+
#endregion
51+
52+
#region Cors service
53+
// enable policy cors service
54+
services.ConfigureCors(_configuration);
55+
#endregion
4656

4757
services.AddTransient(typeof(IGenericRepository<>), typeof(GenericRepository<>));
4858
services.AddTransient<IItemRepository, ItemRepository>();
@@ -52,11 +62,26 @@ public void ConfigureServices(IServiceCollection services)
5262
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
5363
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
5464
{
55-
if (env.IsDevelopment())
56-
{
57-
app.UseDeveloperExceptionPage();
58-
app.UseSwagger();
59-
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Facware.Api v1"));
65+
// Cors pipe
66+
app.UseCors(CorsExtension.AllowSpecificOrigins);
67+
// handle several environments
68+
if (env.IsLocal())
69+
{
70+
app.UseDeveloperExceptionPage();
71+
app.EnableSwaggerPipeline(_configuration);
72+
}
73+
else if (env.IsDevelopment())
74+
{
75+
app.UseDeveloperExceptionPage();
76+
app.EnableSwaggerPipeline(_configuration);
77+
}
78+
else if (env.IsStaging())
79+
{
80+
app.EnableSwaggerPipeline(_configuration);
81+
}
82+
else
83+
{
84+
app.EnableSwaggerPipeline(_configuration);
6085
}
6186

6287
app.UseHttpsRedirection();

Facware.Api/appsettings.Local.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft": "Warning",
6+
"Microsoft.Hosting.Lifetime": "Information"
7+
}
8+
},
9+
"DbContextSettings": {
10+
"ConnectionString": "Server=127.0.0.1;Port=5432;Database=facware;User Id=postgres;Password=postgres;"
11+
},
12+
"Cors": {
13+
"AllowedOrigin": [
14+
"http://localhost:4200",
15+
"http://localhost:4201",
16+
"http://localhost:4202"
17+
]
18+
},
19+
"SwaggerConfiguration": {
20+
"SwaggerJSONEndpoints": "swagger/v1/swagger.json",
21+
"Title": "Facware Base",
22+
"Version": "V1.0",
23+
"Description": "Facware base project.",
24+
"TermsOfService": "https://luisespinoza.facware.com/",
25+
"ContactName": "luis.espinoza@afacware.com respinozabarboza@gmail.com",
26+
"ContactEmail": "luis.espinoza@afacware.com respinozabarboza@gmail.com",
27+
"LicenseName": "MIT",
28+
"LicenseUrl": "https://opensource.org/licenses/MIT"
29+
}
30+
}

0 commit comments

Comments
 (0)