-
Notifications
You must be signed in to change notification settings - Fork 1
/
Startup.cs
171 lines (150 loc) · 5.82 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
160
161
162
163
164
165
166
167
168
169
170
171
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using InfovalutarDB;
using InfovalutarLoadAndSave;
using InfoValutarLoadingLibs;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.Server.Features;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Net.Http.Headers;
using NSwag;
namespace InfoValutarWebAPI
{
class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
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.AddResponseCaching();
services.AddSpaStaticFiles(configuration =>
{
configuration.RootPath = "wwwroot";
});
services.AddCors(options =>
{
options.AddPolicy("AllowedAll",
builder =>
{
builder
.AllowAnyHeader()
.AllowAnyOrigin()
.AllowAnyMethod();
});
});
services.AddControllers(c=>
{
c.CacheProfiles.Add("Default30",
new CacheProfile()
{
Duration = 30
});
c.CacheProfiles.Add("Default1Day",
new CacheProfile()
{
Duration = 60*60*24
});
});
services.AddSingleton<LoadExchangeProviders>(new LoadExchangeProviders("plugins"));
services.AddSingleton<InMemoryDB>();
services.AddScoped<IRetrieve>(s => new RetrieveSqlServer(s.GetService<InMemoryDB>()));
services.AddScoped<ISave>(s => new SaveSqlServer(s.GetService<InMemoryDB>()));
services.AddScoped<LoadAndSaveLastData>();
services.AddApiVersioning();
services.AddOpenApiDocument(c =>
{
c.PostProcess = d =>
{
d.Info.Title = "Infovalutar API";
d.Info.Version = Assembly.GetExecutingAssembly().GetName().Version.ToString();
d.Info.Description = "Source code / docs at https://github.com/ignatandrei/InfoValutar/";
d.Info.Contact = new OpenApiContact
{
Name = "Andrei Ignat",
Email = string.Empty,
Url = "http://msprogrammer.serviciipeweb.ro/category/exchange-rates/"
};
d.Info.License = new OpenApiLicense()
{
Name = "Use under MIT",
//Url = "https://example.com/license"
};
};
});
}
private static void HandleBank(IApplicationBuilder app)
{
app.Use(async (context, next) =>
{
var req = context.Request;
var newUrl = new StringBuilder().Append(req.Scheme + "://").Append(req.Host).Append(req.PathBase).Append("/api/v1.0/rates").Append(req.Path).Append(req.QueryString);
var p = "/api/v1.0/rates" + context.Request.Path;
await Task.Delay(10);
context.Response.Redirect(newUrl.ToString());
return;
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseCors("AllowedAll");
app.UseHttpsRedirection();
app.UseDefaultFiles();
app.UseStaticFiles();
app.UseRouting();
app.UseResponseCaching();
app.UseAuthorization();
app.UseOpenApi();
app.UseSwaggerUi3();
app.UseSpa(spa =>
{
// To learn more about options for serving an Angular SPA from ASP.NET Core,
// see https://go.microsoft.com/fwlink/?linkid=864501
spa.Options.SourcePath = "ClientApp";
});
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
var service = app.ApplicationServices.GetService<LoadExchangeProviders>();
foreach (var item in service.Banks())
{
app.MapWhen(cnt =>
cnt.Request.Path.Value.StartsWith("/" + item + "/", StringComparison.InvariantCultureIgnoreCase)
||
cnt.Request.Path.Value.EndsWith("." + item, StringComparison.InvariantCultureIgnoreCase)
, HandleBank);
}
//maybe do with https://docs.microsoft.com/en-us/aspnet/core/fundamentals/url-rewriting?view=aspnetcore-3.0
//app.Use(async (context, next) =>
//{
// if (context.Request.Path == "/")
// {
// context.Response.Redirect("/swagger");
// return;
// }
// await next.Invoke();
//});
var sa = app.ServerFeatures.Get<IServerAddressesFeature>();
var urls = string.Join(",", sa.Addresses.Select(it => it + "/swagger"));
Console.WriteLine("please use " + urls);
}
}
}