Files
mixtape/zero.Web/Startup.cs
T

203 lines
6.5 KiB
C#
Raw Normal View History

2020-03-22 14:47:59 +01:00
using Microsoft.AspNetCore.Authentication.Cookies;
2020-03-22 14:18:34 +01:00
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
2020-03-22 14:47:59 +01:00
using Microsoft.AspNetCore.SpaServices.Webpack;
using Microsoft.Extensions.Configuration;
2020-03-22 14:18:34 +01:00
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
2020-04-05 12:01:01 +02:00
using Microsoft.Extensions.Options;
2020-03-22 14:47:59 +01:00
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
2020-04-06 00:26:31 +02:00
using Raven.Client.Documents;
using Raven.Client.Documents.Conventions;
2020-03-22 14:47:59 +01:00
using System;
using System.Threading.Tasks;
2020-03-24 23:09:29 +01:00
using zero.Core;
2020-04-03 16:45:47 +02:00
using zero.Core.Api;
2020-04-06 00:26:31 +02:00
using zero.Core.Entities;
2020-04-05 12:01:01 +02:00
using zero.Core.Extensions;
2020-03-22 14:18:34 +01:00
2020-03-24 23:09:29 +01:00
namespace zero.Web
2020-03-22 14:18:34 +01:00
{
public class Startup
{
2020-03-22 14:47:59 +01:00
private readonly IConfiguration config;
private IWebHostEnvironment env;
/// <summary>
/// Bootstrap the application
/// </summary>
public Startup(IConfiguration config, IWebHostEnvironment env)
2020-03-22 14:18:34 +01:00
{
2020-03-22 14:47:59 +01:00
this.config = config;
this.env = env;
2020-03-22 14:18:34 +01:00
}
2020-03-22 14:47:59 +01:00
/// <summary>
/// This method gets called by the runtime. Use this method to add services to the container.
/// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
/// </summary>
public void ConfigureServices(IServiceCollection services)
{
//CultureInfo cultureInfo = new CultureInfo("en-US");
//cultureInfo.NumberFormat.CurrencySymbol = "€";
//CultureInfo.DefaultThreadCurrentCulture = cultureInfo;
// build and register app configuration
services.Configure<IZeroConfiguration>(config);
ZeroConfiguration appConfig = new ZeroConfiguration();
2020-03-22 14:47:59 +01:00
ConfigurationBinder.Bind(config, appConfig);
services.AddSingleton<IZeroConfiguration>(appConfig);
2020-03-22 14:47:59 +01:00
2020-04-06 00:26:31 +02:00
// add raven
2020-04-06 13:24:46 +02:00
services.AddSingleton(_ =>
2020-04-06 00:26:31 +02:00
{
DocumentStore store = new DocumentStore()
{
Urls = new string[1] { appConfig.Raven.Url },
Database = appConfig.Raven.Database
};
store.Conventions.FindCollectionName = type =>
{
return Constants.Database.CollectionPrefix + DocumentConventions.DefaultGetCollectionName(type);
};
store.Conventions.IdentityPartsSeparator = ".";
return store.Initialize();
});
2020-03-24 23:09:29 +01:00
// add zero core
2020-03-22 14:47:59 +01:00
//services.AddCore(appConfig, env);
services.AddZero(opts =>
{
//opts.Sections.RemoveAt(1);
2020-04-06 00:26:31 +02:00
var section = new Section("commerce", "Commerce", "fth-shopping-bag", "#52bba1");
section.Children.Add(new Section("orders", "Orders"));
section.Children.Add(new Section("customers", "Customers"));
section.Children.Add(new Section("catalogue & ÖBB", "Catalogue"));
2020-04-06 00:26:31 +02:00
section.Children.Add(new Section("promotions", "Promotions"));
section.Children.Add(new Section("channels", "Channels"));
opts.Sections.Insert(3, section);
});
2020-03-22 14:47:59 +01:00
//services.AddAuthentication(opts =>
//{
// opts.
//});
2020-03-22 14:47:59 +01:00
// add cookie-based authentication
services.AddAuthentication(Constants.Auth.Scheme)
.AddCookie(Constants.Auth.Scheme, opts => {
opts.Cookie.Name = Constants.Auth.CookieName;
2020-03-24 01:15:37 +01:00
// override redirect to login page (handled by vue frontend) and return a 401 instead
2020-03-22 14:47:59 +01:00
opts.Events.OnRedirectToLogin = (context) =>
{
context.Response.StatusCode = 401;
return Task.CompletedTask;
};
});
// add Core MVC
IMvcBuilder mvc = services.AddMvc(opts =>
{
opts.Filters.Add<Core.Attributes.OperationCancelledExceptionFilterAttribute>();
})
//.ExtendWithCore()
.AddNewtonsoftJson(opts =>
{
opts.SerializerSettings.Converters.Add(new IsoDateTimeConverter() { DateTimeFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'" });
opts.SerializerSettings.Converters.Add(new StringEnumConverter(new CamelCaseNamingStrategy()));
opts.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
});
if (Environment.GetEnvironmentVariable("DOTNET_WATCH") == "1")
{
mvc.AddRazorRuntimeCompilation();
}
services.Configure<IISOptions>(options =>
{
options.AutomaticAuthentication = false;
});
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
2020-04-03 16:45:47 +02:00
// TODO move registration into core
services.AddTransient<IZeroVue, ZeroVue>();
2020-04-03 16:45:47 +02:00
services.AddTransient<ISetupApi, SetupApi>();
2020-04-06 00:26:31 +02:00
services.AddTransient<ISectionsApi, SectionsApi>();
services.AddTransient<IApplicationsApi, ApplicationsApi>();
2020-04-06 15:53:32 +02:00
services.AddTransient<IPagesApi, PagesApi>();
services.AddTransient<IPageTreeApi, PageTreeApi>();
2020-04-08 00:22:04 +02:00
services.AddTransient<ISettingsApi, SettingsApi>();
2020-04-08 13:07:15 +02:00
services.AddTransient<IAuthenticationApi, AuthenticationApi>();
2020-03-22 14:47:59 +01:00
}
/// <summary>
/// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
/// </summary>
2020-04-05 12:01:01 +02:00
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IOptionsMonitor<ZeroOptions> zeroOptions)
2020-03-22 14:18:34 +01:00
{
string zeroPath = zeroOptions.CurrentValue.BackofficePath.EnsureEndsWith('/').EnsureStartsWith('/');
2020-04-05 12:01:01 +02:00
2020-03-22 14:47:59 +01:00
// enable webpack middleware
2020-03-22 14:18:34 +01:00
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
2020-03-22 14:47:59 +01:00
//app.UseDeveloperExceptionPage();
#pragma warning disable CS0618
app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions()
{
HotModuleReplacement = true
});
#pragma warning restore CS0618
}
else
{
app.UseHsts();
2020-03-22 14:18:34 +01:00
}
2020-03-22 14:47:59 +01:00
app.UseStaticFiles();
//app.UseCors();
2020-03-22 14:18:34 +01:00
app.UseWhen(ctx => ctx.Request.Path.ToString().StartsWith(zeroPath.TrimEnd('/')), zeroApp =>
2020-03-22 14:18:34 +01:00
{
zeroApp.UseRouting();
zeroApp.UseAuthentication();
zeroApp.UseAuthorization();
zeroApp.UseEndpoints(endpoints =>
{
// setup route
endpoints.MapControllerRoute(
name: "setup",
pattern: zeroPath + "setup",
defaults: new
{
controller = "Setup",
action = "Index"
}
);
// routes for API
endpoints.MapControllerRoute(
name: "api",
pattern: zeroPath + "api/{controller=Index}/{action=Index}/{id?}"
);
// fallbacks for SPA
endpoints.MapFallbackToController(zeroPath + "{**path}", "Index", "Index");
});
2020-03-22 14:18:34 +01:00
});
}
}
}