Created
July 9, 2017 19:09
-
-
Save scionwest/e6acd349ef2516e26afca970d66f68e9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Startup | |
{ | |
private IContainer ApplicationContainer; | |
public Startup(IHostingEnvironment env) | |
{ | |
var builder = new ConfigurationBuilder() | |
.SetBasePath(env.ContentRootPath) | |
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) | |
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) | |
.AddEnvironmentVariables(); | |
if (env.IsDevelopment()) | |
{ | |
builder.AddUserSecrets<Startup>(); | |
} | |
Configuration = builder.Build(); | |
} | |
public IConfigurationRoot Configuration { get; } | |
// This method gets called by the runtime. Use this method to add services to the container. | |
public IServiceProvider ConfigureServices(IServiceCollection services) | |
{ | |
// Setup our basic stuff with the ASP.Net Core Dependency pipeline | |
services.AddOptions(); | |
services.Configure<ConnectionStringOptions>(options => | |
{ | |
options.SqlConnectionString = this.Configuration.GetConnectionString(ConnectionStringOptions.ConnectionStringKey); | |
options.StorageConnectionString = this.Configuration[ConnectionStringOptions.AzureStorageConnectionStringKey]; | |
}); | |
services.Configure<SecretKeys>(options => options.MovieDbSecret = this.Configuration[SecretKeys.MovieDbSecretConfigKey]); | |
services.AddSingleton(new HttpClient()); | |
services.AddSingleton<IBackgroundTasker, BackgroundTasker>(); | |
services.AddSingleton<IEncryption, Encryption>(); | |
// Identity | |
services.AddIdentityServer() | |
.AddTemporarySigningCredential() | |
.AddInMemoryApiResources(IdentityConfiguration.GetApiResources()) | |
.AddInMemoryClients(IdentityConfiguration.GetClients()); | |
services.AddTransient<IResourceOwnerPasswordValidator, PasswordValidator>(); | |
services.AddTransient<IProfileService, ProfileService>(); | |
// Add framework services. | |
services.AddMvc(); | |
// Setup our Data Access with the Autofac Dependency pipeline | |
// ASP.Net Core out of the box does not provide the flexability needed for this. | |
var builder = new ContainerBuilder(); | |
builder.Populate(services); | |
builder.RegisterModule(new DataAccessDIModule()); | |
builder.RegisterModule(new ServicesDIModule()); | |
builder.Register(ctx => | |
{ | |
return new EndPointConfiguration | |
{ | |
ApiVersion = this.Configuration[$"{nameof(EndPointConfiguration)}:{nameof(EndPointConfiguration.ApiVersion)}"], | |
BaseUrl = this.Configuration[$"{nameof(EndPointConfiguration)}:{nameof(EndPointConfiguration.BaseUrl)}"], | |
PrivateApiPath = this.Configuration[$"{nameof(EndPointConfiguration)}:{nameof(EndPointConfiguration.PrivateApiPath)}"], | |
PublicApiPath = this.Configuration[$"{nameof(EndPointConfiguration)}:{nameof(EndPointConfiguration.PublicApiPath)}"], | |
UrlScheme = this.Configuration[$"{nameof(EndPointConfiguration)}:{nameof(EndPointConfiguration.UrlScheme)}"], | |
TokenPath = this.Configuration[$"{nameof(EndPointConfiguration)}:{nameof(EndPointConfiguration.TokenPath)}"], | |
}; | |
}).As<EndPointConfiguration>(); | |
builder.Register(ctx => new RepositoryFactory(this.ApplicationContainer, ctx.Resolve<ILogger<RepositoryFactory>>())) | |
.As<IRepositoryFactory>(); | |
this.ApplicationContainer = builder.Build(); | |
return new AutofacServiceProvider(this.ApplicationContainer); | |
} | |
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. | |
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) | |
{ | |
loggerFactory.AddConsole(Configuration.GetSection("Logging")); | |
IBuildService buildService = this.ApplicationContainer.Resolve<IBuildService>(); | |
if (buildService.IsDebug()) | |
{ | |
loggerFactory.AddDebug(); | |
} | |
else | |
{ | |
loggerFactory.AddAzureWebAppDiagnostics( | |
new AzureAppServicesDiagnosticsSettings | |
{ | |
OutputTemplate = "{Timestamp:yyyy-MM-dd HH:mm:ss zzz} [{Level}] {RequestId}-{SourceContext}: {Message}{NewLine}{Exception}" | |
}); | |
} | |
EndPointConfiguration apiConfig = this.ApplicationContainer.Resolve<EndPointConfiguration>(); | |
var identityOptions = new IdentityServerAuthenticationOptions | |
{ | |
Authority = apiConfig.Authority, | |
RequireHttpsMetadata = apiConfig.UrlScheme.Equals("https", StringComparison.OrdinalIgnoreCase), | |
ApiName = "api1", | |
}; | |
app.UseIdentityServerAuthentication(identityOptions); | |
app.UseIdentityServer(); | |
app.Use(async (context, next) => | |
{ | |
context.Response.Headers.Add("X-Expires", "000000"); | |
await next.Invoke(); | |
}); | |
app.UseMvc(); | |
//app.UseMvc(routeBuilder => routeBuilder.MapRoute( | |
// name: "default", | |
// template: "api/v1/{controller=Home}/{action=Index}/{id?}")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment