-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
194 lines (174 loc) · 5.76 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using MCloudServer;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using SimplePasscode;
var builder = WebApplication.CreateBuilder(args);
builder.WebHost.ConfigureKestrel(kestrel =>
{
kestrel.AddServerHeader = false;
});
var myConfig = builder.Configuration.Get<MCloudConfig>();
var services = builder.Services;
// Configure services
services.AddControllers();
services.AddSingleton<AppService>();
services.AddSingleton(myConfig);
services.AddSingleton<MessageService>();
services.AddSingleton<ConvertService>();
services.AddDbContext<DbCtx>(options =>
{
if (myConfig.DbType == DbType.PostgreSQL)
{
options.UseNpgsql(myConfig.DbStr ?? throw new Exception("DbStr is required for PostgreSQL"));
}
else if (myConfig.DbType == DbType.SQLite)
{
var fileName = myConfig.DbStr ?? "data/data.db";
Directory.CreateDirectory(Path.GetDirectoryName(fileName));
options.UseSqlite("Filename=" + fileName);
}
else
{
throw new Exception("unknown DbType in app configration");
}
});
services.AddCors();
services.AddScoped<UserService>();
services.AddAuthentication("UserAuth")
.AddScheme<AuthenticationSchemeOptions, UserService.AuthHandler>("UserAuth", null);
services.AddSingleton<StorageService>(new LocalStorageService());
services.AddSingleton<FileService>();
services.Configure<ForwardedHeadersOptions>(options =>
{
options.ForwardedHeaders =
ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
});
var app = builder.Build();
var env = app.Environment;
// Preparing the database
using (var scope = app.Services.CreateScope())
{
var dbctx = scope.ServiceProvider.GetService<DbCtx>();
var logger = scope.ServiceProvider.GetService<ILogger<Program>>();
var appService = scope.ServiceProvider.GetService<AppService>();
var fileService = scope.ServiceProvider.GetService<FileService>();
await dbctx.Database.MigrateAsync();
await MigrationService.AppMigrate(appService, dbctx, logger, fileService);
await AppCheckFirstRun(appService, dbctx, logger);
}
// Configure middlewares
app.Use((ctx, next) => {
ctx.Response.Headers.Add("Server", "MusicCloudServer");
return next();
});
app.UseForwardedHeaders();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseCors(builder => builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());
if (string.IsNullOrEmpty(myConfig.Passcode) == false)
{
app.UsePasscode(new SimplePasscodeOptions
{
CookieName = "mcloud_passcode",
Passcode = myConfig.Passcode,
Filter = ctx => !ctx.Request.Path.StartsWithSegments("/api/storage")
&& !ctx.Request.Path.StartsWithSegments("/.well-known")
});
}
if (myConfig.StaticDir != null)
{
var fileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), myConfig.StaticDir));
app.UseDefaultFiles(new DefaultFilesOptions
{
FileProvider = fileProvider,
DefaultFileNames = new[] { "index.html" }
});
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = fileProvider,
ServeUnknownFileTypes = true
});
}
if (string.IsNullOrEmpty(myConfig.StorageDir) == false)
{
string path = Path.Combine(Directory.GetCurrentDirectory(), myConfig.StorageDir);
Directory.CreateDirectory(path);
var fp = new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(path),
RequestPath = "/api/storage",
ServeUnknownFileTypes = true
};
app.Use(async (ctx, next) =>
{
if (ctx.Request.Path.StartsWithSegments("/api/storage"))
{
ctx.Response.Headers.Add("Cache-Control", "public, max-age=31536000, immutable");
await next();
if (ctx.Response.StatusCode >= 300 && !ctx.Response.HasStarted) {
ctx.Response.Headers.Remove("Cache-Control");
}
} else {
await next();
}
});
app.UseStaticFiles(fp);
}
app.Use((ctx, next) =>
{
if (ctx.Request.Path.StartsWithSegments("/api/my", out var remaining))
{
ctx.Request.Path = "/api/users/me" + remaining;
}
return next();
});
app.UseWebSockets();
app.Use(async (ctx, next) =>
{
if (ctx.Request.Path == "/api/ws" && ctx.WebSockets.IsWebSocketRequest)
{
var ws = await ctx.WebSockets.AcceptWebSocketAsync();
await ctx.RequestServices.GetService<MessageService>().HandleWebSocket(ws);
}
else
{
await next(ctx);
}
});
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
app.Run();
async Task AppCheckFirstRun(AppService appService, DbCtx dbctx, ILogger logger)
{
if (await dbctx.Users.CountAsync() == 0) {
logger.LogInformation("No user is found, creating the default \"admin\" user.");
var user = new User{
username = "admin",
passwd = Utils.HashPassword("admin"),
lists = new List<int>(),
role = UserRole.SuperAdmin
};
dbctx.Users.Add(user);
await dbctx.SaveChangesAsync();
dbctx.Entry(user).State = EntityState.Detached;
}
}