migration & environment config

This commit is contained in:
2019-03-23 17:26:49 +01:00
parent 8842a192c7
commit 2d6037ce99
19 changed files with 1910 additions and 35 deletions

View File

@@ -133,6 +133,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="App_Start\DependencyInjectionConfig.cs" />
<Compile Include="App_Start\EnvironmentConfig.cs" />
<Compile Include="App_Start\WebApiConfig.cs" />
<Compile Include="Controllers\FilesController.cs" />
<Compile Include="Controllers\SongsController.cs" />
@@ -178,6 +179,7 @@
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Content Include="App_Data\environment.json" />
<None Include="packages.config" />
<None Include="Properties\PublishProfiles\deploy to test.pubxml" />
</ItemGroup>
@@ -192,9 +194,7 @@
<DependentUpon>201903172028249_Log.cs</DependentUpon>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<Folder Include="App_Data\" />
</ItemGroup>
<ItemGroup />
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>

View File

@@ -0,0 +1,20 @@
using Newtonsoft.Json;
using System.IO;
namespace API.App_Start {
public static class EnvironmentConfig {
public static Config Read() {
var path = System.Web.Hosting.HostingEnvironment.MapPath(@"~/App_Data");
var jsonFile = Path.Combine(path, "environment.json");
if (!File.Exists(jsonFile)) return null;
var file = File.ReadAllText(jsonFile);
return JsonConvert.DeserializeObject<Config>(file);
}
}
public class Config {
public string ConnectionString { get; set; }
public string CorsUrls { get; set; }
}
}

View File

@@ -11,8 +11,9 @@ namespace API {
public static void Register(HttpConfiguration config) {
// CORS
var cors = new EnableCorsAttribute("http://localhost:4200", "*", "*");
cors.SupportsCredentials = true;
var cors = new EnableCorsAttribute(EnvironmentConfig.Read()?.CorsUrls ?? "http://localhost:4200", "*", "*") {
SupportsCredentials = true
};
config.EnableCors(cors);
// Web API configuration and services

View File

@@ -1,11 +1,12 @@
using API.Migrations;
using API.App_Start;
using API.Migrations;
using API.Models;
using System.Data.Entity;
namespace API.Database {
public class DataContext : DbContext {
public DataContext() : base("DataContext") {
public DataContext() : base(EnvironmentConfig.Read()?.ConnectionString ?? "DataContext") {
System.Data.Entity.Database.SetInitializer(new MigrateDatabaseToLatestVersion<DataContext, Configuration>());
}