add DATABASE_URL handling, closes #1

This commit is contained in:
YuukanOO 2021-01-15 20:37:27 +01:00
parent e8a4a87bb3
commit caff5055c2
5 changed files with 65 additions and 1 deletions

11
.vscode/launch.json vendored
View File

@ -4,6 +4,17 @@
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/Apps/Sandbox/bin/Debug/net5.0/Sandbox.dll",
"args": [],
"cwd": "${workspaceFolder}/Apps/Sandbox",
"stopAtEntry": false,
"console": "internalConsole"
},
{
"name": ".NET Core Launch (web)",
"type": "coreclr",

12
Apps/Sandbox/Program.cs Normal file
View File

@ -0,0 +1,12 @@
using System;
namespace Sandbox
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hey!");
}
}
}

View File

@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
</Project>

View File

@ -5,6 +5,7 @@ using MyHN.Application;
using MyHN.Domain;
using MediatR;
using MyHN.Infrastructure.Repositories;
using System;
namespace MyHN.Infrastructure
{
@ -41,7 +42,8 @@ namespace MyHN.Infrastructure
, IConfiguration configuration)
{
services.AddDbContext<MyHNDbContext>(options =>
options.UseSqlite(configuration.GetConnectionString("Default")));
options.UseSqlite(configuration.GetOrParseConnectionString()));
services.AddScoped<ILinkRepository, LinkRepository>();
services.AddScoped<ICommentRepository, CommentRepository>();
services.AddScoped<IContext, MyHNDbContext>();
@ -49,5 +51,21 @@ namespace MyHN.Infrastructure
return new MyHNServicesBuilder(services);
}
private static string GetOrParseConnectionString(this IConfiguration configuration)
{
// Since it will be defined as an uri from heroku...
var envVar = Environment.GetEnvironmentVariable("DATABASE_URL");
if (string.IsNullOrWhiteSpace(envVar))
{
return configuration.GetConnectionString("Default");
}
var uri = new Uri(envVar);
var userInfo = uri.UserInfo.Split(':');
return $"Host={uri.Host};Port={uri.Port};Database={uri.AbsolutePath.Substring(1)};Username={userInfo[0]};Password={userInfo[1]}";
}
}
}

View File

@ -17,6 +17,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Api", "Apps\Api\Api.csproj"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlazorClient", "Apps\BlazorClient\BlazorClient.csproj", "{A8CCBB1E-8161-4030-A4B6-299C7D589939}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sandbox", "Apps\Sandbox\Sandbox.csproj", "{D21E8C20-42A0-4EDD-B6B2-31966C9D935C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -111,6 +113,18 @@ Global
{A8CCBB1E-8161-4030-A4B6-299C7D589939}.Release|x64.Build.0 = Release|Any CPU
{A8CCBB1E-8161-4030-A4B6-299C7D589939}.Release|x86.ActiveCfg = Release|Any CPU
{A8CCBB1E-8161-4030-A4B6-299C7D589939}.Release|x86.Build.0 = Release|Any CPU
{D21E8C20-42A0-4EDD-B6B2-31966C9D935C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D21E8C20-42A0-4EDD-B6B2-31966C9D935C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D21E8C20-42A0-4EDD-B6B2-31966C9D935C}.Debug|x64.ActiveCfg = Debug|Any CPU
{D21E8C20-42A0-4EDD-B6B2-31966C9D935C}.Debug|x64.Build.0 = Debug|Any CPU
{D21E8C20-42A0-4EDD-B6B2-31966C9D935C}.Debug|x86.ActiveCfg = Debug|Any CPU
{D21E8C20-42A0-4EDD-B6B2-31966C9D935C}.Debug|x86.Build.0 = Debug|Any CPU
{D21E8C20-42A0-4EDD-B6B2-31966C9D935C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D21E8C20-42A0-4EDD-B6B2-31966C9D935C}.Release|Any CPU.Build.0 = Release|Any CPU
{D21E8C20-42A0-4EDD-B6B2-31966C9D935C}.Release|x64.ActiveCfg = Release|Any CPU
{D21E8C20-42A0-4EDD-B6B2-31966C9D935C}.Release|x64.Build.0 = Release|Any CPU
{D21E8C20-42A0-4EDD-B6B2-31966C9D935C}.Release|x86.ActiveCfg = Release|Any CPU
{D21E8C20-42A0-4EDD-B6B2-31966C9D935C}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -122,5 +136,6 @@ Global
{22BC27B6-608F-4DBB-9CDB-6A14D53A9BE3} = {55C05C83-8BEB-4FEC-B930-8DC161C99E7C}
{7002AB9E-7457-4CE8-9368-AFD5A1F0119A} = {55C05C83-8BEB-4FEC-B930-8DC161C99E7C}
{A8CCBB1E-8161-4030-A4B6-299C7D589939} = {55C05C83-8BEB-4FEC-B930-8DC161C99E7C}
{D21E8C20-42A0-4EDD-B6B2-31966C9D935C} = {55C05C83-8BEB-4FEC-B930-8DC161C99E7C}
EndGlobalSection
EndGlobal