Files
JavaScriptServices/samples/react/MusicStore/Startup.cs

119 lines
4.8 KiB
C#
Executable File

using AutoMapper;
using Microsoft.AspNet.Authorization;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.AspNet.SpaServices.Webpack;
using Microsoft.Data.Entity;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using MusicStore.Apis;
using MusicStore.Models;
namespace MusicStore
{
public class Startup
{
public Startup(IHostingEnvironment env)
{
// Set up configuration sources.
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; set; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.Configure<SiteSettings>(settings =>
{
settings.DefaultAdminUsername = Configuration["DefaultAdminUsername"];
settings.DefaultAdminPassword = Configuration["DefaultAdminPassword"];
});
// Add MVC services to the services container.
services.AddMvc();
// Uncomment the following line to add Web API services which makes it easier to port Web API 2 controllers.
// You will also need to add the Microsoft.AspNet.Mvc.WebApiCompatShim package to the 'dependencies' section of project.json.
// services.AddWebApiConventions();
// Add EF services to the service container
services.AddEntityFramework()
.AddSqlite()
.AddDbContext<MusicStoreContext>(options => {
options.UseSqlite(Configuration["DbConnectionString"]);
});
// Add Identity services to the services container
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<MusicStoreContext>()
.AddDefaultTokenProviders();
// Uncomment the following line to add Web API services which makes it easier to port Web API 2 controllers.
// You will also need to add the Microsoft.AspNet.Mvc.WebApiCompatShim package to the 'dependencies' section of project.json.
// services.AddWebApiConventions();
// Configure Auth
services.Configure<AuthorizationOptions>(options =>
{
options.AddPolicy("app-ManageStore", new AuthorizationPolicyBuilder().RequireClaim("app-ManageStore", "Allowed").Build());
});
Mapper.CreateMap<AlbumChangeDto, Album>();
Mapper.CreateMap<Album, AlbumChangeDto>();
Mapper.CreateMap<Album, AlbumResultDto>();
Mapper.CreateMap<AlbumResultDto, Album>();
Mapper.CreateMap<Artist, ArtistResultDto>();
Mapper.CreateMap<ArtistResultDto, Artist>();
Mapper.CreateMap<Genre, GenreResultDto>();
Mapper.CreateMap<GenreResultDto, Genre>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(LogLevel.Warning);
// Initialize the sample data
SampleData.InitializeMusicStoreDatabaseAsync(app.ApplicationServices).Wait();
if (env.IsDevelopment()) {
app.UseDeveloperExceptionPage();
} else {
app.UseExceptionHandler("/Home/Error");
}
app.UseIISPlatformHandler();
// In dev mode, the JS/TS/etc is compiled and served dynamically and supports hot replacement.
// In production, we assume you've used webpack to emit the prebuilt content to disk.
if (env.IsDevelopment()) {
app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions {
HotModuleReplacement = true,
ReactHotModuleReplacement = true
});
}
app.UseStaticFiles();
app.UseMvc(routes => {
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
routes.MapSpaFallbackRoute(
name: "spa-fallback",
defaults: new { controller = "Home", action = "Index" });
});
}
// Entry point for the application.
public static void Main(string[] args) => Microsoft.AspNet.Hosting.WebApplication.Run<Startup>(args);
}
}