-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStartup.cs
184 lines (161 loc) · 6.8 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
176
177
178
179
180
181
182
183
184
using System;
using System.IO;
using System.Linq;
using CommicDB.Controllers;
using CommicDB.DB;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Primitives;
using Microsoft.Net.Http.Headers;
using Newtonsoft.Json;
using System.Threading;
using System.Threading.Tasks;
namespace CommicDB
{
public class Startup
{
/// <summary>
/// Scant einmal am Tag nach neuen Ausgabe
/// </summary>
public static Timer DataTimer { get; private set; }
/// <summary>
/// API Daten
/// </summary>
public static string APIKEY = "f65041c032be86da24e07882e341c3c2363bed7a";
public static string FULLAPIKEY = "?api_key=" + APIKEY;
public Startup(IHostingEnvironment env)
{
env.EnvironmentName = "Development";
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc().AddJsonOptions(options => {
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
}); ;
services.AddResponseCompression(options =>
{
options.EnableForHttps = true;
options.Providers.Add<BrotliCompressionProvider>();
});
var connString = Configuration.GetConnectionString("DefaultConnection")
.Replace("D:\\Visual Studio\\ComicDB\\CommicDB\\CommicDB.mdf", Path.Combine(Directory.GetCurrentDirectory(), "CommicDB.mdf"));
services.AddDbContext<ComicDBContext>(options => options.UseSqlServer(connString));
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, ComicDBContext comicDB)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseResponseCompression();
comicDB.Database.Migrate();
//CheckService
var state = new object();
Startup.DataTimer = new Timer((s) =>
{
CheckNewIssues(comicDB);
}, state, 24 * 60 * 60 * 1000, 24 * 60 * 60 * 1000);
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
// Für Debug caches deaktivieren
app.UseStaticFiles(new StaticFileOptions()
{
OnPrepareResponse = context =>
{
context.Context.Response.Headers.Add("Cache-Control", "no-cache, no-store");
context.Context.Response.Headers.Add("Expires", "-1");
}
});
// node_modules verfügbar machen
app.UseStaticFiles(new StaticFileOptions()
{
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), @"node_modules")),
RequestPath = new PathString("/node_modules"),
});
}
else
{
app.UseExceptionHandler("/Home/Error");
// Live mit richtigen Caches
app.UseStaticFiles(new StaticFileOptions()
{
OnPrepareResponse = context =>
{
if (!StringValues.IsNullOrEmpty(context.Context.Response.Headers[HeaderNames.AcceptEncoding]))
context.Context.Response.Headers.Append(HeaderNames.Vary, HeaderNames.AcceptEncoding);
context.Context.Response.Headers.Add("Cache-Control", "public,max-age=" + 60 * 60 * 24);
}
});
// Dist verfügbar machen
app.UseStaticFiles(new StaticFileOptions()
{
FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), @"dist")),
RequestPath = new PathString("/dist"),
OnPrepareResponse = context =>
{
if (!StringValues.IsNullOrEmpty(context.Context.Response.Headers[HeaderNames.AcceptEncoding]))
context.Context.Response.Headers.Append(HeaderNames.Vary, HeaderNames.AcceptEncoding);
context.Context.Response.Headers.Add("Cache-Control", "public,max-age=" + 60 * 60 * 24);
}
});
}
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
routes.MapRoute(
name: "angular",
template: "{action=Index}",
defaults: new { controller = "Home"});
});
}
/// <summary>
/// Sucht nach neuen Ausgaben
/// Und löscht den Cache
/// </summary>
/// <returns></returns>
private async void CheckNewIssues(ComicDBContext comicDB)
{
try
{
var checkData = comicDB.CheckData.ToList();
var controller = new DataController(comicDB);
foreach(var check in checkData)
{
var volume = await controller.GetVolume(check.VolumeId);
if(check.LastCount != volume.IssueCount)
{
check.LastCount = volume.IssueCount;
check.HasNew = true;
}
}
comicDB.SaveChanges();
//Cache aufräumen
foreach(var file in Directory.GetFiles("Cache"))
{
File.Delete(file);
}
}
catch(Exception ex)
{
}
}
}
}