Skip to content

Commit

Permalink
#117 Accessibility - Heroku deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
kgniazdowski committed Nov 14, 2021
1 parent a20c520 commit 94cdc03
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
14 changes: 9 additions & 5 deletions accessibility-service/src/Accessibility.Api/Program.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace Accessibility.Api
{
Expand All @@ -16,11 +11,20 @@ public static void Main(string[] args)
CreateHostBuilder(args).Build().Run();
}

private static bool IsDevelopment =>
Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Development";

public static string HerokuUrl =>
$"http://+:{Environment.GetEnvironmentVariable("PORT")}";

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
if (!IsDevelopment)
webBuilder.UseUrls(HerokuUrl);
});
}
}
20 changes: 18 additions & 2 deletions accessibility-service/src/Accessibility.Api/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,27 @@ namespace Accessibility.Api
public class Startup
{
private const string corsPolicyName = "mainCorsPolicy";
private readonly IWebHostEnvironment webHostEnvironment;

public Startup(IConfiguration configuration)
public Startup(IConfiguration configuration, IWebHostEnvironment webHostEnvironment)
{
Configuration = configuration;
this.webHostEnvironment = webHostEnvironment;
}

public IConfiguration Configuration { get; }

private string GetHerokuConnectionString() {
string connectionUrl = Environment.GetEnvironmentVariable("DATABASE_URL");

var databaseUri = new Uri(connectionUrl);

string db = databaseUri.LocalPath.TrimStart('/');
string[] userInfo = databaseUri.UserInfo.Split(':', StringSplitOptions.RemoveEmptyEntries);

return $"Host={databaseUri.Host};Database={db};Username={userInfo[0]};Password={userInfo[1]};SSL Mode=Require;Trust Server Certificate=True;";
}

public void ConfigureServices(IServiceCollection services)
{
services.AddControllers()
Expand All @@ -44,6 +57,9 @@ public void ConfigureServices(IServiceCollection services)
ConfigureOptions(services);
ConfigureCors(services);

if (!webHostEnvironment.IsDevelopment())
Configuration["ConnectionStrings:Accessibility"] = GetHerokuConnectionString();

var applicationAssembly = typeof(CreateBookingCommand).Assembly;
services.AddCoreServices(applicationAssembly);
services.AddAccessibilityModule(Configuration, applicationAssembly);
Expand All @@ -64,7 +80,7 @@ private void ConfigureOptions(IServiceCollection services)
private void ConfigureAuth(IServiceCollection services)
{
services.AddScoped<IAuthorizationHandler, MustBeEmployeeOfFacilityHandler>();

services
.AddAuthentication(options =>
{
Expand Down

0 comments on commit 94cdc03

Please sign in to comment.