-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStartup.cs
175 lines (148 loc) · 7.45 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
172
173
174
175
#region Related components
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Diagnostics;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using net.vieapps.Components.Utility;
#endregion
namespace net.vieapps.Services.SRP
{
public class Startup
{
public static void Main(string[] args)
=> WebHost.CreateDefaultBuilder(args).Run<Startup>(args);
public Startup(IConfiguration configuration)
=> this.Configuration = configuration;
public IConfiguration Configuration { get; }
public LogLevel LogLevel => this.Configuration.GetAppSetting("Logging/LogLevel/Default", UtilityService.GetAppSetting("Logs:Level", "Information")).TryToEnum(out LogLevel logLevel) ? logLevel : LogLevel.Information;
bool IsCacheEnabled { get; } = !"true".IsEquals(UtilityService.GetAppSetting("Cache:Disabled"));
bool IsRouterEnabled { get; } = !"true".IsEquals(UtilityService.GetAppSetting("Router:Disabled"));
public void ConfigureServices(IServiceCollection services)
{
// mandatory services
services
.AddHttpContextAccessor()
.AddResponseCompression(options => options.EnableForHttps = true)
.AddLogging(builder => builder.SetMinimumLevel(this.LogLevel));
if (this.IsCacheEnabled)
services.AddCache(options => this.Configuration.GetSection("Cache").Bind(options));
// config authentication with proxy/load balancer
if (Global.UseIISIntegration)
services.Configure<IISOptions>(options => options.ForwardClientCertificate = false);
else
{
var certificateHeader = "true".IsEquals(UtilityService.GetAppSetting("Proxy:UseAzure"))
? "X-ARR-ClientCert"
: UtilityService.GetAppSetting("Proxy:X-Forwarded-Certificate");
if (!string.IsNullOrWhiteSpace(certificateHeader))
services.AddCertificateForwarding(options => options.CertificateHeader = certificateHeader);
}
// config options of IIS server (for working with InProcess hosting model)
if (Global.UseIISInProcess)
services.Configure<IISServerOptions>(options => Global.PrepareIISServerOptions(options, _ => options.MaxRequestBodySize = 1024 * 1024 * Global.MaxRequestBodySize));
}
public void Configure(IApplicationBuilder appBuilder, IHostApplicationLifetime appLifetime, IWebHostEnvironment environment)
{
// environments
var stopwatch = Stopwatch.StartNew();
Console.OutputEncoding = Encoding.UTF8;
Global.ServiceName = "SRP";
var loggerFactory = appBuilder.ApplicationServices.GetService<ILoggerFactory>();
var logPath = UtilityService.GetAppSetting("Path:Logs");
if ("true".IsEquals(UtilityService.GetAppSetting("Logs:WriteFiles", "true")) && !string.IsNullOrWhiteSpace(logPath) && Directory.Exists(logPath))
{
logPath = Path.Combine(logPath, "{Hour}" + $"_{Global.ServiceName.ToLower()}.http.pid-{Environment.ProcessId}.txt");
loggerFactory.AddFile(logPath, this.LogLevel);
}
else
logPath = null;
// setup the service
Logger.AssignLoggerFactory(loggerFactory);
Global.Logger = loggerFactory.CreateLogger<Startup>();
Global.ServiceProvider = appBuilder.ApplicationServices;
Global.RootPath = environment.ContentRootPath;
Global.Logger.LogInformation($"The {Global.ServiceName} HTTP service is starting");
Global.Logger.LogInformation($"Version: {typeof(Startup).Assembly.GetVersion()}");
#if DEBUG
Global.Logger.LogInformation($"Working mode: DEBUG ({(environment.IsDevelopment() ? "Development" : "Production")})");
#else
Global.Logger.LogInformation($"Working mode: RELEASE ({(environment.IsDevelopment() ? "Development" : "Production")})");
#endif
Global.Logger.LogInformation($"Service URIs:\r\n\t- Round robin: services.{Global.ServiceName.ToLower()}.http\r\n\t- Single (unique): services.{Extensions.GetUniqueName($"{Global.ServiceName}.http")}");
Global.Logger.LogInformation($"Environment:\r\n\t{Extensions.GetRuntimeEnvironment()}\r\n\t- Node ID: {Extensions.GetNodeID()}");
JsonConvert.DefaultSettings = () => new JsonSerializerSettings
{
Formatting = Formatting.None,
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
DateTimeZoneHandling = DateTimeZoneHandling.Local
};
// prepare outgoing proxy
var proxy = UtilityService.GetAppSetting("Proxy:Host");
if (!string.IsNullOrWhiteSpace(proxy))
try
{
UtilityService.AssignWebProxy(proxy, UtilityService.GetAppSetting("Proxy:Port").CastAs<int>(), UtilityService.GetAppSetting("Proxy:User"), UtilityService.GetAppSetting("Proxy:UserPassword"), UtilityService.GetAppSetting("Proxy:Bypass")?.ToArray(";"));
}
catch (Exception ex)
{
Global.Logger.LogError($"Error occurred while assigning web-proxy => {ex.Message}", ex);
}
// setup middlewares
appBuilder
.UseForwardedHeaders(Global.GetForwardedHeadersOptions())
.UseStatusCodeHandler()
.UseResponseCompression()
.UseCookiePolicy();
if (this.IsCacheEnabled)
appBuilder.UseCache();
appBuilder.UseMiddleware<Handler>();
// connect to API Gateway Router
if (this.IsRouterEnabled)
Handler.Connect();
// on started
appLifetime.ApplicationStarted.Register(() =>
{
Global.Logger.LogInformation($"API Gateway Router: {new Uri(Router.GetRouterStrInfo()).GetResolvedURI()}");
Global.Logger.LogInformation($"API Gateway HTTP service: {UtilityService.GetAppSetting("HttpUri:APIs", "None")}");
Global.Logger.LogInformation($"Files HTTP service: {UtilityService.GetAppSetting("HttpUri:Files", "None")}");
Global.Logger.LogInformation($"Portals HTTP service: {UtilityService.GetAppSetting("HttpUri:Portals", "None")}");
Global.Logger.LogInformation($"Root (base) directory: {Global.RootPath}");
Global.Logger.LogInformation($"Temporary directory: {UtilityService.GetAppSetting("Path:Temp", "None")}");
Global.Logger.LogInformation($"Status files directory: {UtilityService.GetAppSetting("Path:Status", "None")}");
Global.Logger.LogInformation($"Static files directory: {UtilityService.GetAppSetting("Path:Statics", "None")}");
Global.Logger.LogInformation($"Static segments: {Global.StaticSegments.ToString(", ")}");
Global.Logger.LogInformation($"Logging level: {this.LogLevel} - Local rolling log files is {(string.IsNullOrWhiteSpace(logPath) ? "disabled" : $"enabled => {logPath}")}");
Global.Logger.LogInformation($"Show debugs: {Global.IsDebugLogEnabled} - Show results: {Global.IsDebugResultsEnabled} - Show stacks: {Global.IsDebugStacksEnabled}");
stopwatch.Stop();
Global.Logger.LogInformation($"The {Global.ServiceName} HTTP service is started - PID: {Environment.ProcessId} - Execution times: {stopwatch.GetElapsedTimes()}");
Global.Logger = loggerFactory.CreateLogger<Handler>();
});
// on stopping
appLifetime.ApplicationStopping.Register(() => Global.Logger = loggerFactory.CreateLogger<Startup>());
// on stopped
appLifetime.ApplicationStopped.Register(() =>
{
if (this.IsRouterEnabled)
Handler.Disconnect();
Global.CancellationTokenSource.Cancel();
Global.CancellationTokenSource.Dispose();
Global.Logger.LogInformation($"The {Global.ServiceName} HTTP service was stopped");
});
// don't terminate the process immediately, wait for the Main thread to exit gracefully
Console.CancelKeyPress += (sender, args) =>
{
appLifetime.StopApplication();
args.Cancel = true;
};
}
}
}