167 lines
5.2 KiB
C#
167 lines
5.2 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;
|
|
|
|
/// <summary>
|
|
/// Bootstrap the application
|
|
/// </summary>
|
|
public Startup(IConfiguration config)
|
|
{
|
|
this.config = config;
|
|
}
|
|
|
|
|
|
/// <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);
|
|
ZeroBuilder zero = 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(TestFeatures.Wishlist, "Wishlist", "Frontend wishlist for logged-in users");
|
|
opts.Features.Add(TestFeatures.SocialShopping, "Social shopping", "Integrate products into social media portals");
|
|
|
|
opts.Renderers.Add<TeamMember, TeamMemberRenderer>();
|
|
opts.Renderers.Add<SocialContent, SocialContentRenderer>();
|
|
});
|
|
|
|
// 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;
|
|
});
|
|
}
|
|
|
|
/// <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");
|
|
});
|
|
}
|
|
}
|
|
}
|