-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
230 lines (196 loc) · 13.3 KB
/
Program.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
using System;
using System.Configuration;
using System.Net.Http.Headers;
using Azure.Identity;
using LivestreamRecorder.DB.Interfaces;
using LivestreamRecorder.DB.Models;
using LivestreamRecorderBackend.Interfaces;
using LivestreamRecorderBackend.Services;
using LivestreamRecorderBackend.Services.Authentication;
using LivestreamRecorderBackend.Services.PlatformService;
using LivestreamRecorderBackend.Services.StorageService;
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Abstractions;
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Configurations;
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Enums;
using Microsoft.Extensions.Azure;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Minio;
using Serilog;
using TwitchLib.Api;
using TwitchLib.Api.Core;
using TwitchLib.Api.Interfaces;
using Log = LivestreamRecorderBackend.Helper.Log;
#if COUCHDB
using CouchDB.Driver.DependencyInjection;
using CouchDB.Driver.Options;
using LivestreamRecorder.DB.CouchDB;
#endif
#if COSMOSDB
using LivestreamRecorder.DB.CosmosDB;
using Microsoft.EntityFrameworkCore;
#endif
IHostBuilder builder = new HostBuilder()
.ConfigureFunctionsWebApplication()
.ConfigureServices((_, services) =>
{
services.AddHttpClient("client",
config =>
{
config.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue(".NET", "8.0"));
config.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("Recorder.moe", "1.0"));
config.DefaultRequestHeaders.UserAgent.Add(
new ProductInfoHeaderValue("(+https://recorder.moe)"));
});
services.AddSingleton<IOpenApiConfigurationOptions>(_ =>
{
var options = new OpenApiConfigurationOptions
{
Servers = DefaultOpenApiConfigurationOptions.GetHostNames(),
OpenApiVersion = OpenApiVersionType.V3,
IncludeRequestingHostName = true,
ForceHttps = false,
ForceHttp = false
};
return options;
});
ILogger logger = Log.MakeLogger();
services.AddSingleton(logger);
services.AddMemoryCache(option => option.SizeLimit = 1024);
#region CosmosDB
#if COSMOSDB
services.AddDbContext<PublicContext>((options) =>
{
options
//.EnableSensitiveDataLogging()
.UseCosmos(
connectionString: Environment.GetEnvironmentVariable(
"CosmosDB_Public_ConnectionString")!,
databaseName: "Public",
cosmosOptionsAction: option => option.GatewayModeMaxConnectionLimit(380));
},
ServiceLifetime.Singleton,
ServiceLifetime.Singleton);
services.AddDbContext<PrivateContext>((options) =>
{
options
//.EnableSensitiveDataLogging()
.UseCosmos(
connectionString: Environment.GetEnvironmentVariable(
"CosmosDB_Private_ConnectionString")!,
databaseName: "Private",
cosmosOptionsAction: option
=> option.GatewayModeMaxConnectionLimit(380));
},
ServiceLifetime.Singleton,
ServiceLifetime.Singleton);
services.AddSingleton<UnitOfWork_Public>();
services.AddSingleton<UnitOfWork_Private>();
services.AddSingleton<IVideoRepository>(
(s) => new VideoRepository((IUnitOfWork)s.GetRequiredService(typeof(UnitOfWork_Public))));
services.AddSingleton<IChannelRepository>(
(s) => new ChannelRepository((IUnitOfWork)s.GetRequiredService(typeof(UnitOfWork_Public))));
services.AddSingleton<IUserRepository>(
(s) => new UserRepository((IUnitOfWork)s.GetRequiredService(typeof(UnitOfWork_Private))));
#endif
#endregion
#region CouchDB
#if COUCHDB
services.AddCouchContext<CouchDBContext>(options =>
{
options
.UseEndpoint(Environment.GetEnvironmentVariable("CouchDB_Endpoint")!)
.UseCookieAuthentication(username: Environment.GetEnvironmentVariable("CouchDB_Username")!,
password: Environment.GetEnvironmentVariable("CouchDB_Password")!)
#if !RELEASE
.ConfigureFlurlClient(setting
=> setting.BeforeCall = call
=> Serilog.Log.Debug("Sending request to couch: {request} {body}",
call,
call.RequestBody))
#endif
.SetPropertyCase(PropertyCaseType.None);
});
services.AddSingleton<UnitOfWork_Public>();
services.AddSingleton<UnitOfWork_Private>();
services.AddSingleton<IVideoRepository>(
s => new VideoRepository((IUnitOfWork)s.GetRequiredService(typeof(UnitOfWork_Public))));
services.AddSingleton<IChannelRepository>(
s => new ChannelRepository((IUnitOfWork)s.GetRequiredService(typeof(UnitOfWork_Public))));
services.AddSingleton<IUserRepository>(
s => new UserRepository((IUnitOfWork)s.GetRequiredService(typeof(UnitOfWork_Private))));
#endif
#endregion
#region Storage
if (Environment.GetEnvironmentVariable("StorageService") == "AzureBlobStorage")
{
if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable("Blob_ConnectionString"))
|| string.IsNullOrEmpty(Environment.GetEnvironmentVariable("Blob_ContainerNamePrivate"))
|| string.IsNullOrEmpty(Environment.GetEnvironmentVariable("Blob_ContainerNamePublic")))
{
logger.Fatal(
"Invalid ENV for BlobStorage. Please set Blob_ConnectionString, Blob_ContainerNamePrivate, Blob_ContainerNamePublic");
throw new ConfigurationErrorsException(
"Invalid ENV for BlobStorage. Please set Blob_ConnectionString, Blob_ContainerNamePrivate, Blob_ContainerNamePublic");
}
services.AddAzureClients(clientsBuilder =>
{
clientsBuilder.UseCredential(new DefaultAzureCredential())
.AddBlobServiceClient(Environment.GetEnvironmentVariable("Blob_ConnectionString"));
});
services.AddSingleton<IStorageService, AbsService>();
}
else if (Environment.GetEnvironmentVariable("StorageService") == "S3")
{
if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable("S3_Endpoint"))
|| string.IsNullOrEmpty(Environment.GetEnvironmentVariable("S3_AccessKey"))
|| string.IsNullOrEmpty(Environment.GetEnvironmentVariable("S3_SecretKey"))
|| string.IsNullOrEmpty(Environment.GetEnvironmentVariable("S3_Secure"))
|| string.IsNullOrEmpty(Environment.GetEnvironmentVariable("S3_BucketNamePrivate"))
|| string.IsNullOrEmpty(Environment.GetEnvironmentVariable("S3_BucketNamePublic")))
{
logger.Fatal(
"Invalid ENV for S3. Please set S3_Endpoint, S3_AccessKey, S3_SecretKey, S3_Secure, S3_BucketNamePrivate, S3_BucketNamePublic");
throw new ConfigurationErrorsException(
"Invalid ENV for S3. Please set S3_Endpoint, S3_AccessKey, S3_SecretKey, S3_Secure, S3_BucketNamePrivate, S3_BucketNamePublic");
}
IMinioClient? minio = new MinioClient()
.WithEndpoint(Environment.GetEnvironmentVariable("S3_Endpoint"))
.WithCredentials(Environment.GetEnvironmentVariable("S3_AccessKey"),
Environment.GetEnvironmentVariable("S3_SecretKey"))
.WithSSL(bool.Parse(Environment.GetEnvironmentVariable("S3_Secure") ?? "false"))
.Build();
services.AddSingleton(minio);
services.AddSingleton<IStorageService, S3Service>();
}
else
{
logger.Fatal("Invalid ENV StorageService. Should be AzureBlobStorage or S3.");
throw new ConfigurationErrorsException("Invalid ENV StorageService. Should be AzureBlobStorage or S3.");
}
#endregion
services.AddSingleton<ChannelService>();
services.AddSingleton<UserService>();
services.AddSingleton<VideoService>();
services.AddSingleton<ITwitchAPI, TwitchAPI>(_ =>
{
var api = new TwitchAPI(
settings: new ApiSettings
{
ClientId = Environment.GetEnvironmentVariable("Twitch_ClientId"),
Secret = Environment.GetEnvironmentVariable("Twitch_ClientSecret")
});
return api;
});
services.AddSingleton<Fc2Service>();
services.AddSingleton<TwitcastingService>();
services.AddSingleton<TwitchService>();
services.AddSingleton<YoutubeService>();
services.AddSingleton<AuthenticationService>();
services.AddSingleton<GoogleService>();
services.AddSingleton<GitHubService>();
services.AddSingleton<MicrosoftService>();
services.AddSingleton<DiscordService>();
});
IHost host = builder.Build();
host.Run();