Skip to content

Commit

Permalink
refactor startup for Web and Api
Browse files Browse the repository at this point in the history
  • Loading branch information
alimon808 committed Oct 21, 2017
1 parent daede57 commit 7e04f0b
Show file tree
Hide file tree
Showing 15 changed files with 164 additions and 190 deletions.
5 changes: 2 additions & 3 deletions ContosoUniversity.Api/ContosoUniversity.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,16 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Rewrite" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.Facebook" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.Google" Version="2.0.0" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ContosoUniversity.Data\ContosoUniversity.Data.csproj" />
<ProjectReference Include="..\ContosoUniversity.Services\ContosoUniversity.Services.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions ContosoUniversity.Api/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http:https://localhost:6187/",
"sslPort": 0
"applicationUrl": "https:https://localhost:44352/",
"sslPort": 44352
}
},
"profiles": {
Expand Down
80 changes: 5 additions & 75 deletions ContosoUniversity.Api/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.EntityFrameworkCore;
using ContosoUniversity.Data;
using ContosoUniversity.Data.Interfaces;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Rewrite;
using ContosoUniversity.Services;
using ContosoUniversity.Data.DbContexts;
using ContosoUniversity.Data.Entities;
using Microsoft.AspNetCore.Identity;

namespace ContosoUniversity.Api
{
Expand All @@ -34,86 +31,19 @@ public Startup(IHostingEnvironment env)

public void ConfigureServices(IServiceCollection services)
{
if (CurrentEnvironment.IsEnvironment("Testing"))
{
if (OperatingSystem.IsMacOs())
{
//store sqlite data base output directory
var location = System.Reflection.Assembly.GetEntryAssembly().Location;
var directoryName = System.IO.Path.GetDirectoryName(location);
var dataSource = $"Data Source={directoryName}//ContosoDb.sqlite";
services.AddDbContext<ApplicationContext>(options => options.UseSqlite(dataSource));
services.AddDbContext<SecureApplicationContext>(options => options.UseSqlite(dataSource));
}
else
{
services.AddDbContext<ApplicationContext>(optionsBuilder => optionsBuilder.UseInMemoryDatabase("TestDb"));
services.AddDbContext<SecureApplicationContext>(optionsBuilder => optionsBuilder.UseInMemoryDatabase("TestDb"));
}
}
else
{
if (OperatingSystem.IsMacOs())
{
//store sqlite data base output directory
var location = System.Reflection.Assembly.GetEntryAssembly().Location;
var directoryName = System.IO.Path.GetDirectoryName(location);
var dataSource = $"Data Source={directoryName}//ContosoDb.sqlite";
services.AddDbContext<ApplicationContext>(options => options.UseSqlite(dataSource));
}
else
{

services.AddDbContext<ApplicationContext>(options =>
{
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"), x => x.MigrationsHistoryTable("Migration", "Contoso"));
});
services.AddDbContext<SecureApplicationContext>(options =>
{
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"), x => x.MigrationsHistoryTable("Migration", "Contoso"));
});
}
}

services.AddIdentity<ApplicationUser, IdentityRole>(config =>
{
config.SignIn.RequireConfirmedEmail = true;
})
.AddEntityFrameworkStores<SecureApplicationContext>()
.AddDefaultTokenProviders();

services.AddAuthentication().AddGoogle(googleOptions =>
{
googleOptions.ClientId = Configuration["Authentication:Google:ClientId"];
googleOptions.ClientSecret = Configuration["Authentication:Google:ClientSecret"];
});

services.AddAuthentication().AddFacebook(facebookOptions =>
{
facebookOptions.AppId = Configuration["Authentication:Facebook:AppId"];
facebookOptions.AppSecret = Configuration["Authentication:Facebook:AppSecret"];
});

services.AddScoped<IDbInitializer, DbInitializer>();
services.AddScoped(typeof(IRepository<>), typeof(Repository<>));
services.AddMvc();

services.Configure<SampleData>(Configuration.GetSection("SampleData"));
services.Configure<IdentityUserOptions>(Configuration.GetSection("IdentityUser"));
services.AddCustomizedContext(Configuration, CurrentEnvironment);
services.AddCustomizedIdentity(Configuration);
services.AddCustomizedMvc();
}

public void Configure(IApplicationBuilder app, ApplicationContext context, ILoggerFactory loggerFactory, IDbInitializer dbInitializer)
{
if (CurrentEnvironment.IsDevelopment() || CurrentEnvironment.IsEnvironment("Testing"))
{
var sampleData = new SampleData();
Configuration.GetSection("SampleData").Bind(sampleData);
//todo: redesign
dbInitializer.Initialize();
}

app.UseRewriter(new RewriteOptions().AddRedirectToHttps());
app.UseMvc();
//app.UseMvcWithDefaultRoute();
}
}
}
4 changes: 0 additions & 4 deletions ContosoUniversity.Data/ContosoUniversity.Data.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,4 @@
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ContosoUniversity.Services\ContosoUniversity.Services.csproj" />
</ItemGroup>
</Project>
5 changes: 1 addition & 4 deletions ContosoUniversity.Data/DbContexts/ApplicationContext.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using ContosoUniversity.Data.Entities;
using Microsoft.EntityFrameworkCore;
using ContosoUniversity.Services;
using ContosoUniversity.Data.DbContexts;
using System;

namespace ContosoUniversity.Data
{
Expand All @@ -24,7 +21,7 @@ public ApplicationContext(DbContextOptions<ApplicationContext> options) : base(o
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
string schema = "Contoso";
if (ContosoUniversity.Services.OperatingSystem.IsMacOs())
if (OperatingSystem.IsMacOs())
{
schema = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using Microsoft.Extensions.Configuration;
using System;
using System.IO;
using ContosoUniversity.Services;

namespace ContosoUniversity.Data
{
Expand All @@ -20,7 +19,7 @@ public ApplicationContext CreateDbContext(string[] args)
.Build();

var builder = new DbContextOptionsBuilder<ApplicationContext>();
if (ContosoUniversity.Services.OperatingSystem.IsMacOs())
if (OperatingSystem.IsMacOs())
{
builder.UseSqlite("Data Source=ContosoUniversity.sqlite");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using ContosoUniversity.Data.Entities;
using ContosoUniversity.Services;

namespace ContosoUniversity.Data.DbContexts
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using Microsoft.Extensions.Configuration;
using System;
using System.IO;
using ContosoUniversity.Data;

namespace ContosoUniversity.Data.DbContexts
{
Expand All @@ -20,7 +19,7 @@ public SecureApplicationContext CreateDbContext(string[] args)

var builder = new DbContextOptionsBuilder<SecureApplicationContext>();

if (ContosoUniversity.Services.OperatingSystem.IsMacOs())
if (OperatingSystem.IsMacOs())
{
builder.UseSqlite("Data Source=ContosoUniversity.sqlite");
}
Expand Down
12 changes: 11 additions & 1 deletion ContosoUniversity.Data/DbInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using System.Threading.Tasks;
using ContosoUniversity.Data.Interfaces;
using Microsoft.Extensions.Options;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Hosting;

namespace ContosoUniversity.Data
{
Expand All @@ -18,14 +20,15 @@ public class DbInitializer : IDbInitializer
private readonly UserManager<ApplicationUser> _userManager;
private readonly RoleManager<IdentityRole> _roleManager;
private readonly IdentityUserOptions _identityUser;
private readonly IHostingEnvironment _environment;

public DbInitializer(ApplicationContext context,
SecureApplicationContext secureContext,
ILoggerFactory loggerFactory,
IOptions<SampleData> dataOptions,
UserManager<ApplicationUser> userManager,
RoleManager<IdentityRole> roleManager,
IOptions<IdentityUserOptions> identityUserOptions)
IOptions<IdentityUserOptions> identityUserOptions, IHostingEnvironment env)
{
_context = context;
_secureContext = secureContext;
Expand All @@ -34,6 +37,7 @@ public class DbInitializer : IDbInitializer
_userManager = userManager;
_roleManager = roleManager;
_identityUser = identityUserOptions.Value;
_environment = env;
}
public void Initialize()
{
Expand All @@ -47,6 +51,12 @@ private void InitializeContext()
if (_context.Database.EnsureCreated())
{
_logger.LogInformation("Creating database schema...");

// create identity tables
if (!_environment.IsEnvironment("Testing"))
{
_secureContext.Database.Migrate();
}
}
var unitOfWork = new UnitOfWork(_context);
var seedData = new SeedData(_logger, unitOfWork, _data);
Expand Down
10 changes: 10 additions & 0 deletions ContosoUniversity.Data/OperatingSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Runtime.InteropServices;

namespace ContosoUniversity.Data
{
public static class OperatingSystem
{
public static bool IsWindows() => RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
public static bool IsMacOs() => RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
}
}
8 changes: 8 additions & 0 deletions ContosoUniversity.Services/ContosoUniversity.Services.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,17 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.Facebook" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.Google" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="2.0.0" />
<PackageReference Include="Sendgrid" Version="9.9.0" />
<PackageReference Include="Twilio" Version="5.6.4" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ContosoUniversity.Data\ContosoUniversity.Data.csproj" />
</ItemGroup>

</Project>
Loading

0 comments on commit 7e04f0b

Please sign in to comment.