Files
mixtape/zero.Web/Startup.cs
T
2020-05-07 11:45:28 +02:00

213 lines
7.7 KiB
C#

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.SpaServices.Webpack;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using Raven.Client.Documents;
using Raven.Client.Documents.Conventions;
using System;
using System.IO;
using zero.Core;
using zero.Core.Api;
using zero.Core.Entities;
using zero.Core.Extensions;
using zero.TestData;
using zero.TestData.Lists;
using zero.Web.Formatters;
namespace zero.Web
{
public class Startup
{
private readonly IConfiguration config;
private IWebHostEnvironment env;
/// <summary>
/// Bootstrap the application
/// </summary>
public Startup(IConfiguration config, IWebHostEnvironment env)
{
this.config = config;
this.env = env;
}
/// <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();
ConfigurationBinder.Bind(config, appConfig);
services.AddSingleton<IZeroConfiguration>(appConfig);
// add raven
services.AddSingleton(_ =>
{
DocumentStore store = new DocumentStore()
{
Urls = new string[1] { appConfig.Raven.Url },
Database = appConfig.Raven.Database
};
store.Conventions.FindCollectionName = type =>
{
Type finalType = type;
if (type.IsSubclassOf(typeof(SpaceContent)))
{
finalType = typeof(SpaceContent);
}
return Constants.Database.CollectionPrefix + DocumentConventions.DefaultGetCollectionName(finalType);
};
store.Conventions.TransformTypeCollectionNameToDocumentIdPrefix = name =>
{
return name.ToCamelCaseId();
};
store.Conventions.IdentityPartsSeparator = ".";
return store.Initialize();
});
// add zero core
//services.AddCore(appConfig, env);
services.AddZero(opts =>
{
opts.Spaces.AddList<TeamMember>("team", "Team", "Our team members", "fth-users");
opts.Spaces.AddList<News>("news", "News", "Articles about the company", "fth-edit");
opts.Spaces.AddSeparator();
opts.Spaces.AddEditor<SocialContent>("social", "Social", "Links to social media", "fth-twitter");
opts.Features.Add(Features.Wishlist, "Wishlist", "Frontend wishlist for logged-in users");
opts.Features.Add(Features.SocialShopping, "Social shopping", "Integrate products into social media portals");
opts.Renderers.Add<TeamMember, TeamMemberRenderer>();
opts.Renderers.Add<SocialContent, SocialContentRenderer>();
//var commercePermissions = new Core.Identity.PermissionCollection()
//{
// Label = "Commerce"
//};
//commercePermissions.Items.Add(new Core.Identity.Permission("commerce.orders", "Orders", "Manage and fulfill orders", Core.Identity.PermissionValueType.ReadWrite));
//commercePermissions.Items.Add(new Core.Identity.Permission("commerce.channels", "Channels", "Create and manage sales channels", Core.Identity.PermissionValueType.ReadWrite));
//commercePermissions.Items.Add(new Core.Identity.Permission("commerce.newchannels", "Create channels", "Create new channels", Core.Identity.PermissionValueType.Boolean));
//opts.Authorization.Permissions.Add(commercePermissions);
//opts.Sections.RemoveAt(1);
//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"));
//section.Children.Add(new Section("promotions", "Promotions"));
//section.Children.Add(new Section("channels", "Channels"));
//opts.Sections.Insert(3, section);
});
// add Core MVC
IMvcBuilder mvc = services.AddMvc(opts =>
{
opts.Filters.Add<Core.Attributes.OperationCancelledExceptionFilterAttribute>();
opts.InputFormatters.Insert(0, new RawJsonBodyInputFormatter());
})
//.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;
JsonConvert.DefaultSettings = () => opts.SerializerSettings;
});
if (Environment.GetEnvironmentVariable("DOTNET_WATCH") == "1")
{
mvc.AddRazorRuntimeCompilation();
}
services.Configure<IISOptions>(options =>
{
options.AutomaticAuthentication = false;
});
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
// TODO move registration into core
services.AddTransient<IZeroVue, ZeroVue>();
services.AddTransient<IPaths>(factory => new Paths(env.WebRootPath, true));
services.AddTransient<ISetupApi, SetupApi>();
services.AddTransient<ISectionsApi, SectionsApi>();
services.AddTransient<IApplicationsApi, ApplicationsApi>();
services.AddTransient<IPagesApi, PagesApi>();
services.AddTransient<IPageTreeApi, PageTreeApi>();
services.AddTransient<ISettingsApi, SettingsApi>();
services.AddTransient<IAuthenticationApi, AuthenticationApi>();
services.AddTransient<ICountriesApi, CountriesApi>();
services.AddTransient<IUserApi, UserApi>();
services.AddTransient<IUserRolesApi, UserRolesApi>();
services.AddTransient<IToken, Token>();
services.AddTransient<ISpacesApi, SpacesApi>();
services.AddTransient<ITranslationsApi, TranslationsApi>();
services.AddTransient<ILanguagesApi, LanguagesApi>();
services.AddTransient<IPermissionsApi, PermissionsApi>();
services.AddTransient<IMediaApi, MediaApi>();
services.AddTransient<IMediaUpload, MediaUpload>();
}
/// <summary>
/// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
/// </summary>
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IOptionsMonitor<ZeroOptions> zeroOptions)
{
string zeroPath = zeroOptions.CurrentValue.BackofficePath.EnsureEndsWith('/').EnsureStartsWith('/');
// enable webpack middleware
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
//app.UseDeveloperExceptionPage();
#pragma warning disable CS0618
app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions()
{
HotModuleReplacement = true
});
#pragma warning restore CS0618
}
else
{
app.UseHsts();
}
app.UseStaticFiles();
//app.UseCors();
app.UseZero();
app.Run(async (context) =>
{
await context.Response.WriteAsync("from app");
});
}
}
}