-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStartup.cs
139 lines (119 loc) · 5.4 KB
/
Startup.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using AutoMapper;
using Collections.API.Factories;
using Collections.API.Factories.Interfaces;
using Collections.API.Mappings.Profiles;
using Collections.API.MIddleware;
using Collections.API.Repositories;
using Collections.API.Repositories.Interfaces;
using Collections.API.Services;
using Collections.API.Services.Interfaces;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.PlatformAbstractions;
using NLog.Extensions.Logging;
namespace Collections.API
{
/// <summary>
/// Startup Class
/// </summary>
public class Startup
{
/// <summary>
/// Gets or sets the Auto mapper configuration
/// </summary>
private MapperConfiguration AutoMapperConfig { get; set; }
/// <summary>
/// Gets the configuration
/// </summary>
public IConfigurationRoot Configuration { get; }
/// <summary>
/// Initializes a new instance of the <see cref="Startup"/> class.
/// </summary>
/// <param name="env">The environment</param>
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
this.AutoMapperConfig = new MapperConfiguration(
x =>
{
x.AddProfile(new AlbumsProfile());
x.AddProfile(new MoviesProfile());
x.AddProfile(new BooksProfile());
x.AddProfile(new VideoGamesProfile());
});
builder.AddEnvironmentVariables();
this.Configuration = builder.Build();
}
/// <summary>
/// Configures the services the application needs
/// </summary>
/// <param name="services">The services.</param>
public void ConfigureServices(IServiceCollection services)
{
services.AddOptions();
services.AddMvc();
services.AddRouting(options => options.LowercaseUrls = true);
// Swagger config, adds support for multiple concurrent documentation one per version.
services.AddSwaggerGen(options =>
{
var basePath = PlatformServices.Default.Application.ApplicationBasePath;
options.MultipleApiVersions(new Swashbuckle.Swagger.Model.Info[]
{
// New API versions should be added as models similar to the one below
new Swashbuckle.Swagger.Model.Info
{
Version = "v1",
Title = this.Configuration["SwaggerInfo:Title"],
Description = this.Configuration["SwaggerInfo:Description"],
Contact = new Swashbuckle.Swagger.Model.Contact
{
Name = this.Configuration["SwaggerInfo:Contact:Name"]
}
}
}, (description, version) =>
{
return description.RelativePath.Contains($"api/{version}");
});
options.IncludeXmlComments($"{basePath}\\Collections.API.xml");
options.DescribeAllEnumsAsStrings();
options.GroupActionsBy(c => c.GroupName);
});
services.AddSingleton(x => this.AutoMapperConfig.CreateMapper());
services.AddSingleton<IMongoDbFactory>(x => new MongoDbFactory(this.Configuration["ConnectionString:DefaultConnection"], this.Configuration["AppSettings:CollectionName"]));
services.AddSingleton(this.Configuration);
services.Configure<AppSettings>(this.Configuration.GetSection("AppSettings"));
services.AddScoped(x => x.GetService<IOptionsSnapshot<AppSettings>>().Value);
services.AddTransient<IQueryService, QueryService>();
services.AddTransient<ICollectionService, CollectionService>();
services.AddTransient<ICollectionRepository, CollectionRepository>();
services.AddTransient<IDataRepository, MongoDbDataRepository>();
}
/// <summary>
/// Configures the specified application itself.
/// </summary>
/// <param name="app">The application.</param>
/// <param name="env">The environment</param>
/// <param name="loggerFactory">The logger factory.</param>
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
var basePath = PlatformServices.Default.Application.ApplicationBasePath;
loggerFactory.AddConsole();
if (env.IsDevelopment())
{
loggerFactory.AddDebug();
}
loggerFactory.AddNLog();
loggerFactory.ConfigureNLog($"{basePath}\\nlog.config");
app.UseMiddleware(typeof(ErrorHandlerMiddleware));
app.UseMvc();
app.UseSwagger();
app.UseSwaggerUi();
}
}
}