Skip to content

Commit

Permalink
feat: multidatabase & pfx
Browse files Browse the repository at this point in the history
  • Loading branch information
brunobritodev committed Apr 18, 2022
1 parent d6f0c9a commit 33361c4
Show file tree
Hide file tree
Showing 20 changed files with 212 additions and 129 deletions.
1 change: 0 additions & 1 deletion docker/.env

This file was deleted.

46 changes: 26 additions & 20 deletions docker/docker-compose-local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@ version: "3"
services:

devstore-rabbit:
image: rabbitmq:management
image: rabbitmq:3-management
container_name: devstore-rabbit
ports:
- 5672:5672
- 15672:15672
environment:
RABBITMQ_DEFAULT_USER: devstore
RABBITMQ_DEFAULT_PASS: devstore
RABBITMQ_DEFAULT_USER: "devstore"
RABBITMQ_DEFAULT_PASS: "devstore"

sql-server:
image: mcr.microsoft.com/mssql/server:2017-latest
container_name: devstore-sql-server
ports:
- 1433:1433
environment:
SA_PASSWORD: "MyDB@123"
ACCEPT_EULA: "Y"
image: mcr.microsoft.com/mssql/server:2017-latest
container_name: devstore-sql-server
expose:
- 1433
environment:
SA_PASSWORD: "MyDB@123"
ACCEPT_EULA: "Y"

web-mvc:
image: desenvolvedorio/devstore-web-mvc-ecommerce:latest
Expand All @@ -41,7 +41,7 @@ services:
- api-identity
- api-customers
- api-bff-checkout

api-identity:
image: desenvolvedorio/devstore-api-identity:latest
container_name: devstore-api-identity
Expand All @@ -59,7 +59,7 @@ services:
depends_on:
- devstore-rabbit
- sql-server

api-cart:
image: desenvolvedorio/devstore-api-cart:latest
container_name: devstore-api-cart
Expand Down Expand Up @@ -136,7 +136,7 @@ services:
- api-cart
- api-billing
- api-order

api-billing:
image: desenvolvedorio/devstore-api-billing:latest
container_name: devstore-api-billing
Expand All @@ -156,7 +156,7 @@ services:
- api-identity
- api-order
- sql-server

api-order:
image: desenvolvedorio/devstore-api-order:latest
container_name: devstore-api-order
Expand All @@ -183,9 +183,11 @@ services:
context: ./
dockerfile: ./nginx/Dockerfile
restart: always
volumes:
- ./nginx/conf.d:/etc/nginx/conf.d:ro
ports:
- "80:80"
- "443:443"
- "7500:80"
- "7501:443"
depends_on:
- web-mvc

Expand All @@ -195,10 +197,14 @@ services:
volumes:
- ./certs:/https:rw
command: >
sh -c "[ -e "./https/devstore.academy-localhost.pfx" ] && echo File Already exist || (openssl genrsa -out devstore.rsa 2048 &&
sh -c "[ -e "./https/devstore.academy-localhost.pfx" ] && echo File Already exist || (
rm /https/devstore.academy-localhost.pfx &&
openssl genrsa -out devstore.rsa 2048 &&
openssl req -sha256 -new -key devstore.rsa -out devstore.csr -subj '/CN=localhost' &&
openssl x509 -req -sha256 -days 1825 -in devstore.csr -signkey devstore.rsa -out devstore.crt &&
openssl pkcs12 -export -out /https/devstore.academy-localhost.pfx -inkey devstore.rsa -in devstore.crt -password pass:9HoGMnb7Lu8NFdHBz4Vq2rtKivzMhmMXhtvuB4TZcLMmbWfFmDQCjJeLURAJ4GYe)"
openssl x509 -req -sha256 -days 365 -in devstore.csr -signkey devstore.rsa -out devstore.crt &&
openssl pkcs12 -export -out /https/devstore.academy-localhost.pfx -inkey devstore.rsa -in devstore.crt -password pass:9HoGMnb7Lu8NFdHBz4Vq2rtKivzMhmMXhtvuB4TZcLMmbWfFmDQCjJeLURAJ4GYe &&
rm devstore.rsa devstore.csr devstore.crt)"
volumes:
dpkeys:
dpkeys:

2 changes: 1 addition & 1 deletion docker/nginx/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM nginx
COPY nginx/nginx.prod.conf /etc/nginx/nginx.conf
COPY nginx/devstore.conf /etc/nginx/conf.d/devstore.conf
COPY nginx/nerdstore-certificate.pem /etc/nginx
COPY nginx/nerdstore-certificate.key /etc/nginx
57 changes: 57 additions & 0 deletions docker/nginx/conf.d/devstore.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@

upstream web-mvc {
server web-mvc:5001;
}

upstream rabbitmq {
least_conn;
server devstore-rabbit:15672;
}

server {
listen 80;
server_name $hostname;

location /rabbitmq/ {
rewrite ^/rabbitmq/(.*)$ /$1 break;
proxy_pass http://rabbitmq;
proxy_buffering off;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}

location / {
# Google doesn't like 302 redirects, so we use a 301 here
return 301 https://$host:7501$request_uri;
}
}

server {
listen 443 ssl;
server_name $hostname;

ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;

ssl_certificate /etc/nginx/nerdstore-certificate.pem;
ssl_certificate_key /etc/nginx/nerdstore-certificate.key;

location /rabbitmq/ {
return 302 http://$host:7500$request_uri;
}

location / {
proxy_pass http://web-mvc;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host $server_name;
}

}
43 changes: 0 additions & 43 deletions docker/nginx/nginx.prod.conf

This file was deleted.

Binary file removed docker/pfx/devstore.academy.localhost
Binary file not shown.
17 changes: 0 additions & 17 deletions docker/pfx/devstore.crt

This file was deleted.

15 changes: 0 additions & 15 deletions docker/pfx/devstore.csr

This file was deleted.

27 changes: 0 additions & 27 deletions docker/pfx/devstore.rsa

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ private static bool CanConnect(DbContext context)
context.Database.GetAppliedMigrations(); // Check the database connection
return true;
}
catch
catch (Exception ex)
{
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;

namespace DevStore.WebAPI.Core.DatabaseFlavor;

/// <summary>
/// SqlServer configuration
/// </summary>
public static class ContextConfiguration
{

/// <summary>
/// ASP.NET Identity Context config
/// </summary>
public static IServiceCollection PersistStore<TContext>(this IServiceCollection services, Action<DbContextOptionsBuilder> databaseConfig) where TContext : DbContext
{
// Add a DbContext to store Keys. SigningCredentials and DataProtectionKeys
if (services.All(x => x.ServiceType != typeof(TContext)))
services.AddDbContext<TContext>(databaseConfig);
return services;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace DevStore.WebAPI.Core.DatabaseFlavor;

public enum DatabaseType
{
SqlServer,
MySql,
Postgre,
Sqlite,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.Reflection;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using MySqlConnector;

namespace DevStore.WebAPI.Core.DatabaseFlavor;

public class ProviderConfiguration
{
private readonly string _connectionString;
public static ProviderConfiguration With;
private static readonly string MigrationAssembly = typeof(ProviderConfiguration).GetTypeInfo().Assembly.GetName().Name;

public static void Build(string connString)
{
if (With is null)
With = new ProviderConfiguration(connString);
}

public ProviderConfiguration(string connString) => _connectionString = connString;

public Action<DbContextOptionsBuilder> SqlServer =>
options => options.UseSqlServer(_connectionString, sql => sql.MigrationsAssembly(MigrationAssembly));

public Action<DbContextOptionsBuilder> MySql =>
options => options.UseMySql(_connectionString, ServerVersion.AutoDetect(_connectionString), sql => sql.MigrationsAssembly(MigrationAssembly));

public Action<DbContextOptionsBuilder> Postgre =>
options => options.UseNpgsql(_connectionString, sql => sql.MigrationsAssembly(MigrationAssembly));

public Action<DbContextOptionsBuilder> Sqlite =>
options => options.UseSqlite(_connectionString, sql => sql.MigrationsAssembly(MigrationAssembly));


/// <summary>
/// it's just a tuple. Returns 2 parameters.
/// Trying to improve readability at ConfigureServices
/// </summary>
public static (DatabaseType, string) DetectDatabase(IConfiguration configuration) => (
configuration.GetValue<DatabaseType>("AppSettings:DatabaseType"),
configuration.GetConnectionString("DefaultConnection"));
}

0 comments on commit 33361c4

Please sign in to comment.