|
18 | 18 |
|
19 | 19 | namespace API
|
20 | 20 | {
|
21 |
| - public class Program |
22 |
| - { |
23 |
| - private static int _httpPort; |
24 |
| - |
25 |
| - protected Program() |
26 |
| - { |
27 |
| - } |
28 |
| - |
29 |
| - public static string GetAppSettingFilename() |
30 |
| - { |
31 |
| - var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"); |
32 |
| - var isDevelopment = environment == Environments.Development; |
33 |
| - return "appsettings" + (isDevelopment ? ".Development" : "") + ".json"; |
34 |
| - } |
35 |
| - |
36 |
| - public static async Task Main(string[] args) |
37 |
| - { |
38 |
| - Console.OutputEncoding = System.Text.Encoding.UTF8; |
39 |
| - |
40 |
| - // Before anything, check if JWT has been generated properly or if user still has default |
41 |
| - if (!Configuration.CheckIfJwtTokenSet(GetAppSettingFilename()) && Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") != Environments.Development) |
42 |
| - { |
43 |
| - Console.WriteLine("Generating JWT TokenKey for encrypting user sessions..."); |
44 |
| - var rBytes = new byte[128]; |
45 |
| - using (var crypto = new RNGCryptoServiceProvider()) crypto.GetBytes(rBytes); |
46 |
| - var base64 = Convert.ToBase64String(rBytes).Replace("/", ""); |
47 |
| - Configuration.UpdateJwtToken(GetAppSettingFilename(), base64); |
48 |
| - } |
49 |
| - |
50 |
| - // Get HttpPort from Config |
51 |
| - _httpPort = Configuration.GetPort(GetAppSettingFilename()); |
52 |
| - |
53 |
| - |
54 |
| - var host = CreateHostBuilder(args).Build(); |
55 |
| - |
56 |
| - using var scope = host.Services.CreateScope(); |
57 |
| - var services = scope.ServiceProvider; |
58 |
| - |
59 |
| - try |
| 21 | + public class Program |
| 22 | + { |
| 23 | + private static readonly int HttpPort = Configuration.Port; |
| 24 | + |
| 25 | + protected Program() |
| 26 | + { |
| 27 | + } |
| 28 | + |
| 29 | + public static async Task Main(string[] args) |
| 30 | + { |
| 31 | + Console.OutputEncoding = System.Text.Encoding.UTF8; |
| 32 | + |
| 33 | + // Before anything, check if JWT has been generated properly or if user still has default |
| 34 | + if (!Configuration.CheckIfJwtTokenSet() && |
| 35 | + Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") != Environments.Development) |
| 36 | + { |
| 37 | + Console.WriteLine("Generating JWT TokenKey for encrypting user sessions..."); |
| 38 | + var rBytes = new byte[128]; |
| 39 | + using (var crypto = new RNGCryptoServiceProvider()) crypto.GetBytes(rBytes); |
| 40 | + Configuration.JwtToken = Convert.ToBase64String(rBytes).Replace("/", string.Empty); |
| 41 | + } |
| 42 | + |
| 43 | + var host = CreateHostBuilder(args).Build(); |
| 44 | + |
| 45 | + using var scope = host.Services.CreateScope(); |
| 46 | + var services = scope.ServiceProvider; |
| 47 | + |
| 48 | + try |
| 49 | + { |
| 50 | + var context = services.GetRequiredService<DataContext>(); |
| 51 | + var roleManager = services.GetRequiredService<RoleManager<AppRole>>(); |
| 52 | + // Apply all migrations on startup |
| 53 | + await context.Database.MigrateAsync(); |
| 54 | + await Seed.SeedRoles(roleManager); |
| 55 | + await Seed.SeedSettings(context); |
| 56 | + } |
| 57 | + catch (Exception ex) |
| 58 | + { |
| 59 | + var logger = services.GetRequiredService<ILogger<Program>>(); |
| 60 | + logger.LogError(ex, "An error occurred during migration"); |
| 61 | + } |
| 62 | + |
| 63 | + await host.RunAsync(); |
| 64 | + } |
| 65 | + |
| 66 | + private static IHostBuilder CreateHostBuilder(string[] args) => |
| 67 | + Host.CreateDefaultBuilder(args) |
| 68 | + .ConfigureWebHostDefaults(webBuilder => |
60 | 69 | {
|
61 |
| - var context = services.GetRequiredService<DataContext>(); |
62 |
| - var roleManager = services.GetRequiredService<RoleManager<AppRole>>(); |
63 |
| - // Apply all migrations on startup |
64 |
| - await context.Database.MigrateAsync(); |
65 |
| - await Seed.SeedRoles(roleManager); |
66 |
| - await Seed.SeedSettings(context); |
67 |
| - } |
68 |
| - catch (Exception ex) |
69 |
| - { |
70 |
| - var logger = services.GetRequiredService <ILogger<Program>>(); |
71 |
| - logger.LogError(ex, "An error occurred during migration"); |
72 |
| - } |
73 |
| - |
74 |
| - await host.RunAsync(); |
75 |
| - } |
76 |
| - |
77 |
| - private static IHostBuilder CreateHostBuilder(string[] args) => |
78 |
| - Host.CreateDefaultBuilder(args) |
79 |
| - .ConfigureWebHostDefaults(webBuilder => |
80 |
| - { |
81 |
| - webBuilder.UseKestrel((opts) => |
82 |
| - { |
83 |
| - opts.ListenAnyIP(_httpPort, options => |
84 |
| - { |
85 |
| - options.Protocols = HttpProtocols.Http1AndHttp2; |
86 |
| - }); |
87 |
| - }); |
88 |
| - |
89 |
| - var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"); |
90 |
| - if (environment != Environments.Development) |
91 |
| - { |
92 |
| - webBuilder.UseSentry(options => |
| 70 | + webBuilder.UseKestrel((opts) => |
| 71 | + { |
| 72 | + opts.ListenAnyIP(HttpPort, options => { options.Protocols = HttpProtocols.Http1AndHttp2; }); |
| 73 | + }); |
| 74 | + |
| 75 | + var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"); |
| 76 | + if (environment != Environments.Development) |
| 77 | + { |
| 78 | + webBuilder.UseSentry(options => |
| 79 | + { |
| 80 | + options.Dsn = "https://[email protected]/5757423"; |
| 81 | + options.MaxBreadcrumbs = 200; |
| 82 | + options.AttachStacktrace = true; |
| 83 | + options.Debug = false; |
| 84 | + options.SendDefaultPii = false; |
| 85 | + options.DiagnosticLevel = SentryLevel.Debug; |
| 86 | + options.ShutdownTimeout = TimeSpan.FromSeconds(5); |
| 87 | + options.Release = BuildInfo.Version.ToString(); |
| 88 | + options.AddExceptionFilterForType<OutOfMemoryException>(); |
| 89 | + options.AddExceptionFilterForType<NetVips.VipsException>(); |
| 90 | + options.AddExceptionFilterForType<InvalidDataException>(); |
| 91 | + options.AddExceptionFilterForType<KavitaException>(); |
| 92 | + |
| 93 | + options.BeforeSend = sentryEvent => |
| 94 | + { |
| 95 | + if (sentryEvent.Exception != null |
| 96 | + && sentryEvent.Exception.Message.StartsWith("[GetCoverImage]") |
| 97 | + && sentryEvent.Exception.Message.StartsWith("[BookService]") |
| 98 | + && sentryEvent.Exception.Message.StartsWith("[ExtractArchive]") |
| 99 | + && sentryEvent.Exception.Message.StartsWith("[GetSummaryInfo]") |
| 100 | + && sentryEvent.Exception.Message.StartsWith("[GetSummaryInfo]") |
| 101 | + && sentryEvent.Exception.Message.StartsWith("[GetNumberOfPagesFromArchive]") |
| 102 | + && sentryEvent.Exception.Message.Contains("EPUB parsing error") |
| 103 | + && sentryEvent.Exception.Message.Contains("Unsupported EPUB version") |
| 104 | + && sentryEvent.Exception.Message.Contains("Incorrect EPUB") |
| 105 | + && sentryEvent.Exception.Message.Contains("Access is Denied")) |
93 | 106 | {
|
94 |
| - options.Dsn = "https://[email protected]/5757423"; |
95 |
| - options.MaxBreadcrumbs = 200; |
96 |
| - options.AttachStacktrace = true; |
97 |
| - options.Debug = false; |
98 |
| - options.SendDefaultPii = false; |
99 |
| - options.DiagnosticLevel = SentryLevel.Debug; |
100 |
| - options.ShutdownTimeout = TimeSpan.FromSeconds(5); |
101 |
| - options.Release = BuildInfo.Version.ToString(); |
102 |
| - options.AddExceptionFilterForType<OutOfMemoryException>(); |
103 |
| - options.AddExceptionFilterForType<NetVips.VipsException>(); |
104 |
| - options.AddExceptionFilterForType<InvalidDataException>(); |
105 |
| - options.AddExceptionFilterForType<KavitaException>(); |
| 107 | + return null; // Don't send this event to Sentry |
| 108 | + } |
106 | 109 |
|
107 |
| - options.BeforeSend = sentryEvent => |
108 |
| - { |
109 |
| - if (sentryEvent.Exception != null |
110 |
| - && sentryEvent.Exception.Message.StartsWith("[GetCoverImage]") |
111 |
| - && sentryEvent.Exception.Message.StartsWith("[BookService]") |
112 |
| - && sentryEvent.Exception.Message.StartsWith("[ExtractArchive]") |
113 |
| - && sentryEvent.Exception.Message.StartsWith("[GetSummaryInfo]") |
114 |
| - && sentryEvent.Exception.Message.StartsWith("[GetSummaryInfo]") |
115 |
| - && sentryEvent.Exception.Message.StartsWith("[GetNumberOfPagesFromArchive]") |
116 |
| - && sentryEvent.Exception.Message.Contains("EPUB parsing error") |
117 |
| - && sentryEvent.Exception.Message.Contains("Unsupported EPUB version") |
118 |
| - && sentryEvent.Exception.Message.Contains("Incorrect EPUB") |
119 |
| - && sentryEvent.Exception.Message.Contains("Access is Denied")) |
120 |
| - { |
121 |
| - return null; // Don't send this event to Sentry |
122 |
| - } |
| 110 | + sentryEvent.ServerName = null; // Never send Server Name to Sentry |
| 111 | + return sentryEvent; |
| 112 | + }; |
123 | 113 |
|
124 |
| - sentryEvent.ServerName = null; // Never send Server Name to Sentry |
125 |
| - return sentryEvent; |
126 |
| - }; |
127 |
| - |
128 |
| - options.ConfigureScope(scope => |
129 |
| - { |
130 |
| - scope.User = new User() |
131 |
| - { |
132 |
| - Id = HashUtil.AnonymousToken() |
133 |
| - }; |
134 |
| - scope.Contexts.App.Name = BuildInfo.AppName; |
135 |
| - scope.Contexts.App.Version = BuildInfo.Version.ToString(); |
136 |
| - scope.Contexts.App.StartTime = DateTime.UtcNow; |
137 |
| - scope.Contexts.App.Hash = HashUtil.AnonymousToken(); |
138 |
| - scope.Contexts.App.Build = BuildInfo.Release; |
139 |
| - scope.SetTag("culture", Thread.CurrentThread.CurrentCulture.Name); |
140 |
| - scope.SetTag("branch", BuildInfo.Branch); |
141 |
| - }); |
142 |
| - |
143 |
| - }); |
144 |
| - } |
145 |
| - |
146 |
| - webBuilder.UseStartup<Startup>(); |
147 |
| - }); |
148 |
| - } |
| 114 | + options.ConfigureScope(scope => |
| 115 | + { |
| 116 | + scope.User = new User() |
| 117 | + { |
| 118 | + Id = HashUtil.AnonymousToken() |
| 119 | + }; |
| 120 | + scope.Contexts.App.Name = BuildInfo.AppName; |
| 121 | + scope.Contexts.App.Version = BuildInfo.Version.ToString(); |
| 122 | + scope.Contexts.App.StartTime = DateTime.UtcNow; |
| 123 | + scope.Contexts.App.Hash = HashUtil.AnonymousToken(); |
| 124 | + scope.Contexts.App.Build = BuildInfo.Release; |
| 125 | + scope.SetTag("culture", Thread.CurrentThread.CurrentCulture.Name); |
| 126 | + scope.SetTag("branch", BuildInfo.Branch); |
| 127 | + }); |
| 128 | + }); |
| 129 | + } |
| 130 | + |
| 131 | + webBuilder.UseStartup<Startup>(); |
| 132 | + }); |
| 133 | + } |
149 | 134 | }
|
0 commit comments