Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Deliverystack.Core.Stackcontent.Models.Repositories;
- namespace Csweb
- {
- using System;
- using Microsoft.AspNetCore.Builder;
- using Microsoft.AspNetCore.Hosting;
- using Microsoft.Extensions.Configuration;
- using Microsoft.Extensions.DependencyInjection;
- using Microsoft.Extensions.Hosting;
- using Contentstack.AspNetCore;
- using Deliverystack.Core;
- using Deliverystack.Core.Stackcontent.Configuration;
- using Root.Core.Models.Configuration;
- using Microsoft.AspNetCore.Hosting.Server.Features;
- using System.Linq;
- using Deliverystack.Core.Models.Repositories;
- using System.Threading;
- public class Startup
- {
- private string _baseUrl = null;
- public Startup(IConfiguration configuration)
- {
- Configuration = configuration;
- }
- private ContentstackRepository Repository { get; set; }
- public IConfiguration Configuration { get; }
- // This method gets called by the runtime. Use this method to add services to the container.
- public void ConfigureServices(IServiceCollection services)
- {
- services.AddControllersWithViews();
- services.AddSerializationConfigurator(Configuration);
- services.AddContentstack(Configuration);
- try
- {
- services.AddRepository(Configuration);
- ServiceProvider serviceProvider = services.BuildServiceProvider();
- Repository = (ContentstackRepository) serviceProvider.GetService<IRepository>();
- Repository.Configuration.BaseUrl = _baseUrl;
- //TODO: automatic? serviceProvider.Dispose();
- }
- catch (Exception ex)
- {
- new Instrument().Exception(this, ex, "exception adding repository");
- }
- }
- // private string _listening = null;
- // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
- public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime appLifetime)
- {
- //TODO: fails on Azure
- if (app.ServerFeatures.Get<IServerAddressesFeature>().Addresses.Any())
- {
- _baseUrl = app.ServerFeatures.Get<IServerAddressesFeature>().Addresses.First();
- }
- if (Repository != null)
- {
- Repository.Configuration.BaseUrl = _baseUrl;
- }
- if (env.IsDevelopment())
- {
- app.UseDeveloperExceptionPage();
- }
- else
- {
- app.UseExceptionHandler("/Home/Error");
- // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
- app.UseHsts();
- }
- app.UseHttpsRedirection();
- app.UseStaticFiles();
- app.UseRouting();
- app.UseAuthorization();
- // app.UseStatusCodePages();
- app.UseEndpoints(endpoints =>
- {
- endpoints.MapControllerRoute(
- name: "sitemap",
- "sitemap.xml",
- new { controller = "SitemapXml", action = "SiteMap" }
- );
- if (env.IsDevelopment())
- {
- endpoints.MapControllerRoute(
- name: "default",
- pattern: "Contentstack/{action}",
- defaults: new {controller = "Contentstack", action = "ContentTypes"});
- }
- endpoints.MapControllerRoute(
- name: "entry",
- pattern: "{*url}",
- new {controller = "Entry", action = "Index"});
- });
- appLifetime.ApplicationStarted.Register(() =>
- {
- new Instrument().Track(this, "ApplicationStarted : " + _baseUrl);
- });
- appLifetime.ApplicationStopping.Register(() =>
- {
- new Instrument().Track(this, "ApplicationStopping");
- });
- appLifetime.ApplicationStopped.Register(() =>
- {
- new Instrument().Track(this, "ApplicationStopped");
- });
- if (Repository.Configuration.PreloadOutputCaches)
- {
- Thread thread = new Thread(() => { Repository.PreloadOutputCaches(); });
- thread.IsBackground = true;
- thread.Start();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment