Skip to content

Commit ae95521

Browse files
authored
Merge pull request #1 from lreb/development
First version to work with basic api
2 parents 230ec26 + 9472ac8 commit ae95521

File tree

13 files changed

+457
-3
lines changed

13 files changed

+457
-3
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using Microsoft.AspNetCore.Hosting;
2+
using Microsoft.Extensions.Hosting;
3+
4+
namespace FacwareBase.API
5+
{
6+
public class LambdaEntryPoint :
7+
8+
// The base class must be set to match the AWS service invoking the Lambda function. If not Amazon.Lambda.AspNetCoreServer
9+
// will fail to convert the incoming request correctly into a valid ASP.NET Core request.
10+
//
11+
// API Gateway REST API -> Amazon.Lambda.AspNetCoreServer.APIGatewayProxyFunction
12+
// API Gateway HTTP API payload version 1.0 -> Amazon.Lambda.AspNetCoreServer.APIGatewayProxyFunction
13+
// API Gateway HTTP API payload version 2.0 -> Amazon.Lambda.AspNetCoreServer.APIGatewayHttpApiV2ProxyFunction
14+
// Application Load Balancer -> Amazon.Lambda.AspNetCoreServer.ApplicationLoadBalancerFunction
15+
//
16+
// Note: When using the AWS::Serverless::Function resource with an event type of "HttpApi" then payload version 2.0
17+
// will be the default and you must make Amazon.Lambda.AspNetCoreServer.APIGatewayHttpApiV2ProxyFunction the base class.
18+
Amazon.Lambda.AspNetCoreServer.APIGatewayProxyFunction
19+
{
20+
/// <summary>
21+
/// The builder has configuration, logging and Amazon API Gateway already configured. The startup class
22+
/// needs to be configured in this method using the UseStartup() method.
23+
/// </summary>
24+
/// <param name="builder"></param>
25+
protected override void Init(IWebHostBuilder builder)
26+
{
27+
builder
28+
.UseStartup<Startup>();
29+
}
30+
31+
/// <summary>
32+
/// Use this override to customize the services registered with the IHostBuilder.
33+
///
34+
/// It is recommended not to call ConfigureWebHostDefaults to configure the IWebHostBuilder inside this method.
35+
/// Instead customize the IWebHostBuilder in the Init(IWebHostBuilder) overload.
36+
/// </summary>
37+
/// <param name="builder"></param>
38+
protected override void Init(IHostBuilder builder)
39+
{
40+
}
41+
}
42+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using Microsoft.Extensions.Configuration;
2+
using Microsoft.Extensions.DependencyInjection;
3+
4+
namespace FacwareBase.Api.Extensions.Cors
5+
{
6+
/// <summary>
7+
/// Extension to setup CORS configuration
8+
/// </summary>
9+
public static class CorsExtension
10+
{
11+
/// <summary>
12+
/// Policy cors name
13+
/// </summary>
14+
public static readonly string QSSAllowSpecificOrigins = "QSSAllowSpecificOrigins";
15+
16+
/// <summary>
17+
/// CORS configurations
18+
/// </summary>
19+
/// <param name="services">application service <see cref="IServiceCollection"/></param>
20+
/// <param name="configuration">app settings configuration <see cref="IConfiguration"/></param>
21+
public static void ConfigureCors(this IServiceCollection services, IConfiguration configuration)
22+
{
23+
services.AddCors(options =>
24+
{
25+
options.AddPolicy(QSSAllowSpecificOrigins,
26+
builder =>
27+
{
28+
builder.WithOrigins(configuration.GetSection("Cors:AllowedOrigin").Get<string[]>())
29+
.AllowAnyHeader()
30+
.AllowAnyMethod()
31+
.AllowCredentials();
32+
});
33+
});
34+
}
35+
}
36+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System.Data;
2+
using Microsoft.EntityFrameworkCore;
3+
using Microsoft.Extensions.DependencyInjection;
4+
// using QSS.DataAccess.DataContext;
5+
6+
namespace FacwareBase.Api.Extensions
7+
{
8+
/// <summary>
9+
/// The Service Collection Extensions
10+
/// </summary>
11+
/// <seealso cref="IServiceCollection"/>
12+
public static class ServiceCollectionExtensions
13+
{
14+
/// <summary>
15+
/// Set up the Service SQL DB Context
16+
/// </summary>
17+
/// <param name="serviceCollection">The <see cref="IServiceCollection"/></param>
18+
/// <param name="applicationConfigurationConnectionString">The data migration connection string</param>
19+
public static void UseSqlServer(this IServiceCollection serviceCollection, string applicationConfigurationConnectionString)
20+
{
21+
// TODO: use your context
22+
//serviceCollection.AddDbContext<QSSContext>(o => o.UseSqlServer(applicationConfigurationConnectionString));
23+
}
24+
25+
/// <summary>
26+
/// Set up the Service PostgreSQL DB Context
27+
/// </summary>
28+
/// <param name="serviceCollection">The <see cref="IServiceCollection"/></param>
29+
/// <param name="applicationConfigurationConnectionString">The data migration connection string</param>
30+
public static void UsePostgreSqlServer(this IServiceCollection serviceCollection, string applicationConfigurationConnectionString)
31+
{
32+
// https://www.npgsql.org/efcore/api/Microsoft.Extensions.DependencyInjection.NpgsqlServiceCollectionExtensions.html#Microsoft_Extensions_DependencyInjection_NpgsqlServiceCollectionExtensions_AddEntityFrameworkNpgsql_IServiceCollection_
33+
// https://www.npgsql.org/efcore/index.html#additional-configuration-for-aspnet-core-applications
34+
35+
// TODO: use your context
36+
//serviceCollection.AddDbContext<QSSContext>(options => options.UseNpgsql(applicationConfigurationConnectionString));
37+
}
38+
39+
}
40+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using Microsoft.AspNetCore.Hosting;
2+
using Microsoft.Extensions.Hosting;
3+
4+
namespace FacwareBase.Api.Extensions.Environment
5+
{
6+
/// <summary>
7+
/// Custom environments extension
8+
/// </summary>
9+
public static class HostingEnvironmentExtension
10+
{
11+
/// <summary>
12+
/// Quality assurance environment
13+
/// </summary>
14+
private const string QualityAssurance = "QualityAssurance";
15+
/// <summary>
16+
/// Local environment
17+
/// </summary>
18+
private const string Local = "Local";
19+
20+
/// <summary>
21+
/// Checks if the current host environment name is quality assurance.
22+
/// </summary>
23+
/// <param name="hostingEnvironment">An instance of <see cref="IWebHostEnvironment"/>.</param>
24+
/// <returns>True if the environment name is quality assurance.</returns>
25+
public static bool IsQA(this IWebHostEnvironment hostingEnvironment)
26+
{
27+
return hostingEnvironment.IsEnvironment(QualityAssurance);
28+
}
29+
30+
/// <summary>
31+
/// Checks if the current host environment name is Local.
32+
/// </summary>
33+
/// <param name="hostingEnvironment">An instance of <see cref="IWebHostEnvironment"/>.</param>
34+
/// <returns>True if the environment name is local</returns>
35+
public static bool IsLocal(this IWebHostEnvironment hostingEnvironment)
36+
{
37+
return hostingEnvironment.IsEnvironment(Local);
38+
}
39+
}
40+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using Microsoft.Extensions.Configuration;
2+
using Microsoft.Extensions.Diagnostics.HealthChecks;
3+
using System.Collections.Generic;
4+
using System.Threading;
5+
using System.Threading.Tasks;
6+
7+
namespace FacwareBase.Api.Extensions.HealthCheck
8+
{
9+
/// <summary>
10+
/// Custom health check
11+
/// </summary>
12+
public class CustomHealthCheckExtension: IHealthCheck
13+
{
14+
/// <summary>
15+
/// App settings data DI
16+
/// </summary>
17+
private readonly IConfiguration _config;
18+
19+
public CustomHealthCheckExtension(IConfiguration config)
20+
{
21+
_config = config;
22+
}
23+
24+
/// <summary>
25+
/// custom health check
26+
/// </summary>
27+
/// <param name="context">Health check context <see cref="HealthCheckContext"/></param>
28+
/// <param name="cancellationToken">cancellation token <see cref="CancellationToken"/></param>
29+
/// <returns></returns>
30+
public Task<HealthCheckResult> CheckHealthAsync(
31+
HealthCheckContext context,
32+
CancellationToken cancellationToken = default(CancellationToken))
33+
{
34+
Dictionary<string, object> data = new Dictionary<string, object>();
35+
data["environment"] = _config["HealthChecks:Environment"];
36+
data["corsAllowedOrigin"] = _config.GetSection("Cors:AllowedOrigin").Get<string[]>();
37+
// data["connection"] = _config["ConnectionStrings:ApplicationConfigurationConnectionString"];
38+
return Task.FromResult(
39+
HealthCheckResult.Healthy("Test health check data", data));
40+
}
41+
}
42+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using Microsoft.Extensions.Configuration;
2+
using Microsoft.Extensions.DependencyInjection;
3+
using System;
4+
using System.IO;
5+
using System.Reflection;
6+
using Microsoft.AspNetCore.Builder;
7+
8+
namespace FacwareBase.Api.Extensions.Swagger
9+
{
10+
/// <summary>
11+
/// Swagger extension
12+
/// </summary>
13+
public static class SwaggerExtension
14+
{
15+
#region Swagger Configuration
16+
/// <summary>
17+
/// Method to configure the Swagger Services within the Application services interface
18+
/// </summary>
19+
/// <param name="services">The Service Collection <see cref="IServiceCollection"/></param>
20+
/// <param name="config">The Service Collection <see cref="IConfiguration"/></param>
21+
public static void ConfigureSwaggerExtension(this IServiceCollection services, IConfiguration config)
22+
{
23+
services.AddSwaggerGen(c =>
24+
{
25+
c.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo
26+
{
27+
Title = config["SwaggerConfiguration:Title"],
28+
Version = config["SwaggerConfiguration:Version"],
29+
Description = config["SwaggerConfiguration:Description"]
30+
});
31+
32+
// Set the comments path for the Swagger JSON and UI.
33+
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
34+
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
35+
36+
c.IncludeXmlComments(xmlPath);
37+
});
38+
}
39+
#endregion
40+
41+
/// <summary>
42+
/// Enable Swagger pipeline
43+
/// </summary>
44+
/// <param name="app">application configuration <see cref="IApplicationBuilder"/></param>
45+
/// <param name="config">application settings <see cref="IConfiguration"/></param>
46+
public static void EnableSwaggerPipeline(this IApplicationBuilder app, IConfiguration config)
47+
{
48+
app.UseSwagger();
49+
app.UseSwaggerUI(option =>
50+
{
51+
option.SwaggerEndpoint(
52+
config["SwaggerConfiguration:SwaggerJSONEndpoints"],
53+
$"{config["SwaggerConfiguration:Title"]} {config["SwaggerConfiguration:Version"]}");
54+
// To serve the Swagger UI at the apps root
55+
option.RoutePrefix = string.Empty;
56+
});
57+
}
58+
}
59+
}

FacwareBase.API/FacwareBase.API.csproj

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,23 @@
22

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

9+
<ItemGroup>
10+
<PackageReference Include="Amazon.Lambda.AspNetCoreServer" Version="5.1.4" />
11+
<PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="3.1.1" />
12+
<PackageReference Include="EntityFramework6.Npgsql" Version="6.4.1" />
13+
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.8" />
14+
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.8">
15+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
16+
<PrivateAssets>all</PrivateAssets>
17+
</PackageReference>
18+
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
19+
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="3.1.4" />
20+
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.5.0" />
21+
</ItemGroup>
22+
723

824
</Project>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace FacwareBase.API.Helpers.Domain
2+
{
3+
public class ConnectionString
4+
{
5+
public string ApplicationConfigurationConnectionString { get; set; }
6+
}
7+
}

0 commit comments

Comments
 (0)