forked from fopwoc/owobot-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
88 lines (77 loc) · 2.79 KB
/
Program.cs
File metadata and controls
88 lines (77 loc) · 2.79 KB
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
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Http;
using Microsoft.Extensions.Logging;
using owobot_csharp;
using owobot_csharp.Abstract;
using owobot_csharp.Data;
using owobot_csharp.Exceptions;
using owobot_csharp.Extensions;
using owobot_csharp.Services;
using Singularity;
using Telegram.Bot;
using Telegram.Bot.Polling;
var logger = LoggerFactory.Create(config =>
{
config.AddConsole();
}).CreateLogger("Program");
IConfiguration configuration = new ConfigurationBuilder()
.AddEnvironmentVariables()
.AddCommandLine(args)
.Build();
var validator = new Validator(configuration);
try
{
validator.Validate();
}
catch (ValidationException)
{
logger.LogError("Please, fix the errors listed above and try again");
return 1;
}
if (!Directory.Exists("Essentials"))
{
logger.LogInformation(@"Creating ""Essentials"" Directory");
Directory.CreateDirectory("Essentials");
}
try
{
await using var applicationContext = new ApplicationContext();
var pendingMigrations = await applicationContext.Database.GetPendingMigrationsAsync();
if (pendingMigrations.Any())
{
logger.LogInformation("Initializing migration...");
await applicationContext.Database.MigrateAsync();
logger.LogInformation("Migration successful");
}
}
catch (Exception exception)
{
logger.LogError("Something went wrong during migration process. Please, try restarting the bot.");
logger.LogError("If that won't help, don't hesitate to create issue on my GitHub page!");
logger.LogError("Exception type: {exceptionType}", exception.GetType());
logger.LogError("Exception message: {exceptionMessage}", exception.Message);
return 1;
}
var host = Host.CreateDefaultBuilder(args)
.UseSingularity()
.ConfigureContainer<ContainerBuilder>(builder =>
{
builder.Register<IUpdateHandler, UpdateHandler>(configuration => configuration.With(Lifetimes.Transient));
builder.Register<IReceiverService, ReceiverService>(configuration => configuration.With(Lifetimes.Transient));
builder.Register<IHelperService, HelperService>(configuration => configuration.With(Lifetimes.Transient));
builder.Register<DbContext, ApplicationContext>(configuration => configuration.With(Lifetimes.PerScope));
})
.ConfigureServices(services =>
{
services.ConfigureOwobot(configuration, validator.GetProxy());
services.AddHostedService<PollingService>();
//Removing all logs with requests info due to privacy settings
services.RemoveAll<IHttpMessageHandlerBuilderFilter>();
})
.Build();
await host.RunAsync();
return 0;