mirror of
https://github.com/aspnet/JavaScriptServices.git
synced 2025-12-22 17:47:53 +00:00
Initial state
This commit is contained in:
139
samples/angular/MusicStore/Startup.cs
Executable file
139
samples/angular/MusicStore/Startup.cs
Executable file
@@ -0,0 +1,139 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using AutoMapper;
|
||||
using Microsoft.AspNet.Authorization;
|
||||
using Microsoft.AspNet.Builder;
|
||||
using Microsoft.AspNet.Hosting;
|
||||
using Microsoft.AspNet.Http;
|
||||
using Microsoft.AspNet.Identity.EntityFramework;
|
||||
using Microsoft.AspNet.NodeServices;
|
||||
using Microsoft.Data.Entity;
|
||||
using Microsoft.Dnx.Runtime;
|
||||
using Microsoft.Framework.Configuration;
|
||||
using Microsoft.Framework.DependencyInjection;
|
||||
using Microsoft.Framework.Logging;
|
||||
using MusicStore.Apis;
|
||||
using MusicStore.Models;
|
||||
|
||||
namespace MusicStore
|
||||
{
|
||||
public class Startup
|
||||
{
|
||||
public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
|
||||
{
|
||||
// Setup configuration sources.
|
||||
var builder = new ConfigurationBuilder()
|
||||
.SetBasePath(appEnv.ApplicationBasePath)
|
||||
.AddJsonFile("appsettings.json")
|
||||
.AddEnvironmentVariables();
|
||||
Configuration = builder.Build();
|
||||
}
|
||||
|
||||
public IConfigurationRoot Configuration { get; set; }
|
||||
|
||||
// This method gets called by the runtime.
|
||||
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>();
|
||||
}
|
||||
|
||||
// Configure is called after ConfigureServices is called.
|
||||
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
|
||||
{
|
||||
// Initialize the sample data
|
||||
SampleData.InitializeMusicStoreDatabaseAsync(app.ApplicationServices).Wait();
|
||||
|
||||
loggerFactory.MinimumLevel = LogLevel.Information;
|
||||
loggerFactory.AddConsole();
|
||||
loggerFactory.AddDebug();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
|
||||
// Add the platform handler to the request pipeline.
|
||||
app.UseIISPlatformHandler();
|
||||
|
||||
// Add the following to the request pipeline only in development environment.
|
||||
if (env.IsDevelopment())
|
||||
{
|
||||
app.UseDeveloperExceptionPage();
|
||||
}
|
||||
else
|
||||
{
|
||||
// Add Error handling middleware which catches all application specific errors and
|
||||
// send the request to the following path or controller action.
|
||||
app.UseExceptionHandler("/Home/Error");
|
||||
}
|
||||
|
||||
var nodeInstance = new NodeInstance();
|
||||
app.Use(async (context, next) => {
|
||||
if (context.Request.Path.Value.EndsWith(".less")) {
|
||||
// Note: check for directory traversal
|
||||
var output = await nodeInstance.Invoke("lessCompiler.js", env.WebRootPath + context.Request.Path.Value);
|
||||
await context.Response.WriteAsync(output);
|
||||
} else {
|
||||
await next();
|
||||
}
|
||||
});
|
||||
|
||||
// Add static files to the request pipeline.
|
||||
app.UseStaticFiles();
|
||||
|
||||
// Add MVC to the request pipeline.
|
||||
app.UseMvc(routes =>
|
||||
{
|
||||
routes.MapRoute(
|
||||
name: "default",
|
||||
template: "{controller=Home}/{action=Index}/{id?}");
|
||||
|
||||
routes.MapRoute("spa-fallback", "{*anything}", new { controller = "Home", action = "Index" });
|
||||
|
||||
// Uncomment the following line to add a route for porting Web API 2 controllers.
|
||||
// routes.MapWebApiRoute("DefaultApi", "api/{controller}/{id?}");
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user