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; /// /// Bootstrap the application /// public Startup(IConfiguration config, IWebHostEnvironment env) { this.config = config; this.env = env; } /// /// 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 /// public void ConfigureServices(IServiceCollection services) { //CultureInfo cultureInfo = new CultureInfo("en-US"); //cultureInfo.NumberFormat.CurrencySymbol = "€"; //CultureInfo.DefaultThreadCurrentCulture = cultureInfo; // build and register app configuration services.Configure(config); ZeroConfiguration appConfig = new ZeroConfiguration(); ConfigurationBinder.Bind(config, appConfig); services.AddSingleton(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("team", "Team", "Our team members", "fth-users"); opts.Spaces.AddList("news", "News", "Articles about the company", "fth-edit"); opts.Spaces.AddSeparator(); opts.Spaces.AddEditor("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(); opts.Renderers.Add(); //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(); 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(options => { options.AutomaticAuthentication = false; }); services.AddSingleton(); // TODO move registration into core services.AddTransient(); services.AddTransient(factory => new Paths(env.WebRootPath, true)); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); } /// /// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. /// public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IOptionsMonitor 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"); }); } } }