|
1 | | -using System; |
2 | | -using System.Collections.Generic; |
3 | | -using Microsoft.AspNet.Authorization; |
4 | | -using Microsoft.AspNet.Builder; |
5 | | -using Microsoft.AspNet.Hosting; |
6 | | -using Microsoft.AspNet.Identity.EntityFramework; |
7 | | -using Microsoft.Data.Entity; |
8 | | -using Microsoft.Extensions.Configuration; |
9 | | -using Microsoft.Extensions.DependencyInjection; |
10 | | -using Microsoft.Extensions.Logging; |
11 | | -using Microsoft.Extensions.PlatformAbstractions; |
12 | | -using AutoMapper; |
13 | | -using MusicStore.Apis; |
14 | | -using MusicStore.Models; |
15 | | - |
16 | | -namespace MusicStore |
17 | | -{ |
18 | | - public class Startup |
19 | | - { |
20 | | - public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv) |
21 | | - { |
22 | | - // Setup configuration sources. |
23 | | - var builder = new ConfigurationBuilder() |
24 | | - .SetBasePath(appEnv.ApplicationBasePath) |
25 | | - .AddJsonFile("appsettings.json") |
26 | | - .AddEnvironmentVariables(); |
27 | | - Configuration = builder.Build(); |
28 | | - } |
29 | | - |
30 | | - public IConfigurationRoot Configuration { get; set; } |
31 | | - |
32 | | - // This method gets called by the runtime. |
33 | | - public void ConfigureServices(IServiceCollection services) |
34 | | - { |
35 | | - services.Configure<SiteSettings>(settings => |
36 | | - { |
37 | | - settings.DefaultAdminUsername = Configuration["DefaultAdminUsername"]; |
38 | | - settings.DefaultAdminPassword = Configuration["DefaultAdminPassword"]; |
39 | | - }); |
40 | | - |
41 | | - // Add MVC services to the services container. |
42 | | - services.AddMvc(); |
43 | | - |
44 | | - // Uncomment the following line to add Web API services which makes it easier to port Web API 2 controllers. |
45 | | - // You will also need to add the Microsoft.AspNet.Mvc.WebApiCompatShim package to the 'dependencies' section of project.json. |
46 | | - // services.AddWebApiConventions(); |
47 | | - |
48 | | - // Add EF services to the service container |
49 | | - services.AddEntityFramework() |
50 | | - .AddSqlite() |
51 | | - .AddDbContext<MusicStoreContext>(options => { |
52 | | - options.UseSqlite(Configuration["DbConnectionString"]); |
53 | | - }); |
54 | | - |
55 | | - // Add Identity services to the services container |
56 | | - services.AddIdentity<ApplicationUser, IdentityRole>() |
57 | | - .AddEntityFrameworkStores<MusicStoreContext>() |
58 | | - .AddDefaultTokenProviders(); |
59 | | - |
60 | | - // Uncomment the following line to add Web API services which makes it easier to port Web API 2 controllers. |
61 | | - // You will also need to add the Microsoft.AspNet.Mvc.WebApiCompatShim package to the 'dependencies' section of project.json. |
62 | | - // services.AddWebApiConventions(); |
63 | | - |
64 | | - // Configure Auth |
65 | | - services.Configure<AuthorizationOptions>(options => |
66 | | - { |
67 | | - options.AddPolicy("app-ManageStore", new AuthorizationPolicyBuilder().RequireClaim("app-ManageStore", "Allowed").Build()); |
68 | | - }); |
69 | | - |
70 | | - Mapper.CreateMap<AlbumChangeDto, Album>(); |
71 | | - Mapper.CreateMap<Album, AlbumChangeDto>(); |
72 | | - Mapper.CreateMap<Album, AlbumResultDto>(); |
73 | | - Mapper.CreateMap<AlbumResultDto, Album>(); |
74 | | - Mapper.CreateMap<Artist, ArtistResultDto>(); |
75 | | - Mapper.CreateMap<ArtistResultDto, Artist>(); |
76 | | - Mapper.CreateMap<Genre, GenreResultDto>(); |
77 | | - Mapper.CreateMap<GenreResultDto, Genre>(); |
78 | | - } |
79 | | - |
80 | | - // Configure is called after ConfigureServices is called. |
81 | | - public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) |
82 | | - { |
83 | | - // Initialize the sample data |
84 | | - SampleData.InitializeMusicStoreDatabaseAsync(app.ApplicationServices).Wait(); |
85 | | - |
86 | | - loggerFactory.MinimumLevel = LogLevel.Warning; |
87 | | - loggerFactory.AddConsole(); |
88 | | - loggerFactory.AddDebug(); |
89 | | - |
90 | | - // Configure the HTTP request pipeline. |
91 | | - |
92 | | - // Add the platform handler to the request pipeline. |
93 | | - app.UseIISPlatformHandler(); |
94 | | - |
95 | | - // Add the following to the request pipeline only in development environment. |
96 | | - if (env.IsDevelopment()) |
97 | | - { |
98 | | - app.UseDeveloperExceptionPage(); |
99 | | - } |
100 | | - else |
101 | | - { |
102 | | - // Add Error handling middleware which catches all application specific errors and |
103 | | - // send the request to the following path or controller action. |
104 | | - app.UseExceptionHandler("/Home/Error"); |
105 | | - } |
106 | | - |
107 | | - // Add static files to the request pipeline. |
108 | | - app.UseStaticFiles(); |
109 | | - |
110 | | - // Add MVC to the request pipeline. |
111 | | - app.UseMvc(routes => |
112 | | - { |
113 | | - // Matches requests that correspond to an existent controller/action pair |
114 | | - routes.MapRoute("default", "{controller}/{action}/{id:int?}"); |
115 | | - |
116 | | - // Matches any other request that doesn't appear to have a filename extension (defined as 'having a dot in the last URI segment'). |
117 | | - // This means you'll correctly get 404s for /some/dir/non-existent-image.png instead of returning the SPA HTML. |
118 | | - // However, it means requests like /customers/isaac.newton will *not* be mapped into the SPA, so if you need to accept |
119 | | - // URIs like that you'll need to match all URIs, e.g.: |
120 | | - // routes.MapRoute("spa-fallback", "{*anything}", new { controller = "Home", action = "Index" }); |
121 | | - // (which of course will match /customers/isaac.png too, so in that case it would serve the PNG image at that URL if one is on disk, |
122 | | - // or the SPA HTML if not). |
123 | | - routes.MapSpaFallbackRoute("spa-fallback", new { controller = "Home", action = "Index" }); |
124 | | - |
125 | | - // Uncomment the following line to add a route for porting Web API 2 controllers. |
126 | | - // routes.MapWebApiRoute("DefaultApi", "api/{controller}/{id?}"); |
127 | | - }); |
128 | | - } |
129 | | - } |
130 | | -} |
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using Microsoft.AspNet.Authorization; |
| 4 | +using Microsoft.AspNet.Builder; |
| 5 | +using Microsoft.AspNet.Hosting; |
| 6 | +using Microsoft.AspNet.Identity.EntityFramework; |
| 7 | +using Microsoft.Data.Entity; |
| 8 | +using Microsoft.Extensions.Configuration; |
| 9 | +using Microsoft.Extensions.DependencyInjection; |
| 10 | +using Microsoft.Extensions.Logging; |
| 11 | +using Microsoft.Extensions.PlatformAbstractions; |
| 12 | +using AutoMapper; |
| 13 | +using MusicStore.Apis; |
| 14 | +using MusicStore.Models; |
| 15 | + |
| 16 | +namespace MusicStore |
| 17 | +{ |
| 18 | + public class Startup |
| 19 | + { |
| 20 | + public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv) |
| 21 | + { |
| 22 | + // Setup configuration sources. |
| 23 | + var builder = new ConfigurationBuilder() |
| 24 | + .SetBasePath(appEnv.ApplicationBasePath) |
| 25 | + .AddJsonFile("appsettings.json") |
| 26 | + .AddEnvironmentVariables(); |
| 27 | + Configuration = builder.Build(); |
| 28 | + } |
| 29 | + |
| 30 | + public IConfigurationRoot Configuration { get; set; } |
| 31 | + |
| 32 | + // This method gets called by the runtime. |
| 33 | + public void ConfigureServices(IServiceCollection services) |
| 34 | + { |
| 35 | + services.Configure<SiteSettings>(settings => |
| 36 | + { |
| 37 | + settings.DefaultAdminUsername = Configuration["DefaultAdminUsername"]; |
| 38 | + settings.DefaultAdminPassword = Configuration["DefaultAdminPassword"]; |
| 39 | + }); |
| 40 | + |
| 41 | + // Add MVC services to the services container. |
| 42 | + services.AddMvc(); |
| 43 | + |
| 44 | + // Uncomment the following line to add Web API services which makes it easier to port Web API 2 controllers. |
| 45 | + // You will also need to add the Microsoft.AspNet.Mvc.WebApiCompatShim package to the 'dependencies' section of project.json. |
| 46 | + // services.AddWebApiConventions(); |
| 47 | + |
| 48 | + // Add EF services to the service container |
| 49 | + services.AddEntityFramework() |
| 50 | + .AddSqlite() |
| 51 | + .AddDbContext<MusicStoreContext>(options => { |
| 52 | + options.UseSqlite(Configuration["DbConnectionString"]); |
| 53 | + }); |
| 54 | + |
| 55 | + // Add Identity services to the services container |
| 56 | + services.AddIdentity<ApplicationUser, IdentityRole>() |
| 57 | + .AddEntityFrameworkStores<MusicStoreContext>() |
| 58 | + .AddDefaultTokenProviders(); |
| 59 | + |
| 60 | + // Uncomment the following line to add Web API services which makes it easier to port Web API 2 controllers. |
| 61 | + // You will also need to add the Microsoft.AspNet.Mvc.WebApiCompatShim package to the 'dependencies' section of project.json. |
| 62 | + // services.AddWebApiConventions(); |
| 63 | + |
| 64 | + // Configure Auth |
| 65 | + services.Configure<AuthorizationOptions>(options => |
| 66 | + { |
| 67 | + options.AddPolicy("app-ManageStore", new AuthorizationPolicyBuilder().RequireClaim("app-ManageStore", "Allowed").Build()); |
| 68 | + }); |
| 69 | + |
| 70 | + Mapper.CreateMap<AlbumChangeDto, Album>(); |
| 71 | + Mapper.CreateMap<Album, AlbumChangeDto>(); |
| 72 | + Mapper.CreateMap<Album, AlbumResultDto>(); |
| 73 | + Mapper.CreateMap<AlbumResultDto, Album>(); |
| 74 | + Mapper.CreateMap<Artist, ArtistResultDto>(); |
| 75 | + Mapper.CreateMap<ArtistResultDto, Artist>(); |
| 76 | + Mapper.CreateMap<Genre, GenreResultDto>(); |
| 77 | + Mapper.CreateMap<GenreResultDto, Genre>(); |
| 78 | + } |
| 79 | + |
| 80 | + // Configure is called after ConfigureServices is called. |
| 81 | + public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) |
| 82 | + { |
| 83 | + // Initialize the sample data |
| 84 | + SampleData.InitializeMusicStoreDatabaseAsync(app.ApplicationServices).Wait(); |
| 85 | + |
| 86 | + loggerFactory.MinimumLevel = LogLevel.Warning; |
| 87 | + loggerFactory.AddConsole(); |
| 88 | + loggerFactory.AddDebug(); |
| 89 | + |
| 90 | + // Configure the HTTP request pipeline. |
| 91 | + |
| 92 | + // Add the platform handler to the request pipeline. |
| 93 | + app.UseIISPlatformHandler(); |
| 94 | + |
| 95 | + // Add the following to the request pipeline only in development environment. |
| 96 | + if (env.IsDevelopment()) |
| 97 | + { |
| 98 | + app.UseDeveloperExceptionPage(); |
| 99 | + } |
| 100 | + else |
| 101 | + { |
| 102 | + // Add Error handling middleware which catches all application specific errors and |
| 103 | + // send the request to the following path or controller action. |
| 104 | + app.UseExceptionHandler("/Home/Error"); |
| 105 | + } |
| 106 | + |
| 107 | + // Add static files to the request pipeline. |
| 108 | + app.UseStaticFiles(); |
| 109 | + |
| 110 | + // Add MVC to the request pipeline. |
| 111 | + app.UseMvc(routes => |
| 112 | + { |
| 113 | + // Matches requests that correspond to an existent controller/action pair |
| 114 | + routes.MapRoute("default", "{controller}/{action}/{id:int?}"); |
| 115 | + |
| 116 | + // Matches any other request that doesn't appear to have a filename extension (defined as 'having a dot in the last URI segment'). |
| 117 | + // This means you'll correctly get 404s for /some/dir/non-existent-image.png instead of returning the SPA HTML. |
| 118 | + // However, it means requests like /customers/isaac.newton will *not* be mapped into the SPA, so if you need to accept |
| 119 | + // URIs like that you'll need to match all URIs, e.g.: |
| 120 | + // routes.MapRoute("spa-fallback", "{*anything}", new { controller = "Home", action = "Index" }); |
| 121 | + // (which of course will match /customers/isaac.png too, so in that case it would serve the PNG image at that URL if one is on disk, |
| 122 | + // or the SPA HTML if not). |
| 123 | + routes.MapSpaFallbackRoute("spa-fallback", new { controller = "Home", action = "Index" }); |
| 124 | + |
| 125 | + // Uncomment the following line to add a route for porting Web API 2 controllers. |
| 126 | + // routes.MapWebApiRoute("DefaultApi", "api/{controller}/{id?}"); |
| 127 | + }); |
| 128 | + } |
| 129 | + } |
| 130 | +} |
0 commit comments