-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStartup.cs
159 lines (147 loc) · 6.89 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Xml.XPath;
using AutoMapper;
using Core2WebApi.Common;
using Core2WebApi.Common.Security;
using Core2WebApi.Common.Web;
using Core2WebApi.Controllers.V1.Derivatives.Future;
using Core2WebApi.Data.Entities;
using Core2WebApi.Data.QueryProcessors;
using Core2WebApi.Data.SqlServer.QueryProcessors;
using Core2WebApi.LinkServices;
using Core2WebApi.Services.BrokersService;
using Core2WebApi.Services.Derivatives.Future;
using Core2WebApi.Services.InquiryProcessing;
using Core2WebApi.Services.InquiryProcessing.Derivatives.Future;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.PlatformAbstractions;
using Swashbuckle.AspNetCore.Swagger;
namespace Core2WebApi {
/// <summary>
/// Startup Class
/// </summary>
public class Startup {
/// <summary>
/// Constructor
/// </summary>
/// <param name="configuration"></param>
public Startup (IConfiguration configuration) {
Configuration = configuration;
}
/// <summary>
/// Configuration
/// </summary>
/// <returns></returns>
public IConfiguration Configuration { get; }
/// <summary>
/// ConfigureServices
/// </summary>
/// <param name="services"></param>
public void ConfigureServices (IServiceCollection services) {
services.AddAutoMapper ();
services.AddSwaggerGen (options => {
options.SwaggerDoc ("v1", new Info {
Title = "IME API",
Version = "v1",
Description = "فراهم کردن داده های بورس کالای برای فعالین بازار سرمایه",
TermsOfService = "None",
Contact = new Contact {
Name = "ime.co.ir/api", Email = "[email protected]", Url = "https://ime.co.ir.api/v1"
}
});
var filePath = Path.Combine (PlatformServices.Default.Application.ApplicationBasePath, "SWCapi.xml");
options.IncludeXmlComments (filePath);
});
services.AddApiVersioning (Options => {
Options.ReportApiVersions = true;
Options.DefaultApiVersion = new ApiVersion (1, 0);
});
AddBindings (services);
services.AddMvc ();
}
private void AddBindings (IServiceCollection services) {
services.AddIdentity<IHttpContextAccessor, HttpContextAccessor> ();
services.AddSingleton<IApplicationInfo, ApplicationInfoAdapter> ();
services.AddSingleton<IUserSession, UserSession> (); //(userSession);
services.AddSingleton<IWebUserSession, UserSession> (); //(userSession);
services.AddSingleton<ICommonLinkService, CommonLinkService> ();
services.AddSingleton<IPagedDataRequestFactory, PagedDataRequestFactory> ();
AddDbContexts (services);
AddBrokerBindings (services);
AddFutureContractBinding (services);
}
private void AddBrokerBindings (IServiceCollection services) {
services.AddScoped<IBrokersService, BrokersService> ();
services.AddScoped<IBrokerServiceDependencyBlock, BrokerServiceDependencyBlock> ();
services.AddSingleton<IBrokerLinkService, BrokerLinkService> ();
services.AddScoped<IAllBrokersInquiryProcessor, AllBrokersInquiryProcessor> ();
services.AddSingleton<IAllBrokersQueryProcessor, AllBrokersQueryProcessor> ();
services.AddSingleton<IBrokerByIdInquiryProcessor, BrokerByIdInquiryProcessor> ();
services.AddSingleton<IBrokerByIdQueryProcessor, BrokerByIdQueryProcessor> ();
}
private void AddFutureContractBinding (IServiceCollection services) {
services.AddScoped<IFutureContractsService, FutureContractsService> ();
services.AddSingleton<IFutureContractLinkService, FutureContractLinkService> ();
services.AddScoped<IAllFutureContractInquiryProcessor, AllFutureContractInquiryProcessor> ();
services.AddSingleton<IAllFutureContractQueryProcessor, AllFutureContractQueryProcessor> ();
services.AddScoped<IFutureContractsDependencyBlock, FutureContractsDependencyBlock> ();
services.AddSingleton<IFutureContractByIdInquiryProcessor, FutureContractByIdInquiryProcessor> ();
services.AddSingleton<IFutureContractByIdQueryProcessor, FutureContractByIdQueryProcessor> ();
}
private void AddDbContexts (IServiceCollection services) {
services.AddDbContext<InformingDBContext> ();
services.AddDbContext<FutureSnapshotContext> ();
}
/// <summary>
/// Configure
/// </summary>
/// <param name="app"></param>
/// <param name="env"></param>
/// <param name="loggerFactory"></param>
/// <param name="applicationInfo"></param>
public void Configure (
IApplicationBuilder app,
IHostingEnvironment env,
ILoggerFactory loggerFactory,
IApplicationInfo applicationInfo) {
loggerFactory.AddFile ("logs/" + applicationInfo.ApplicationName + "-{Date}.txt");
if (env.IsDevelopment ()) {
app.UseDeveloperExceptionPage ();
} else {
app.UseExceptionHandler (options => {
options.Run (async context => {
context.Response.StatusCode = (int) HttpStatusCode.InternalServerError;
context.Response.ContentType = "text/html";
var ex = context.Features.Get<IExceptionHandlerFeature> ();
if (ex != null) {
var err = $"<h1>Error: {ex.Error.Message}</h1>{ex.Error.StackTrace }";
await context.Response.WriteAsync (err).ConfigureAwait (false);
}
});
});
}
// Enable middleware to serve generated Swagger as a JSON endpoint.
app.UseSwagger ();
// Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.), specifying the Swagger JSON endpoint.
app.UseSwaggerUI (c => {
c.SwaggerEndpoint ("/swagger/v1/swagger.json", "My API V1");
});
app.UseMvc (r => {
r.MapRoute ("default", "{controller}/{action}");
});
}
}
}