-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInit.cs
More file actions
161 lines (136 loc) · 6.15 KB
/
Init.cs
File metadata and controls
161 lines (136 loc) · 6.15 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
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
namespace Apagee;
public static class Init
{
public static async Task<WebApplication> InitApagee(this WebApplication app)
{
await app.InitDatabase();
await app.CheckFirstTimeUserSetup();
await app.InitKeypair();
await app.InitFirstTimeValues();
await app.InitMediaStorage();
await app.Services.GetRequiredService<SettingsService>().RefreshSettings();
return app;
}
public static async Task<WebApplication> InitFirstTimeValues(this WebApplication app)
{
var kvService = app.Services.GetRequiredService<KeyValueService>();
var val = await kvService.Get(Globals.APP_START_DATE_KEY);
if (val is null || (val is string s && !DateTime.TryParse(s, out _)))
{
await kvService.Set(Globals.APP_START_DATE_KEY, DateTime.UtcNow.ToString("O"));
}
return app;
}
public static async Task<WebApplication> InitKeypair(this WebApplication app)
{
var keypairHelper = app.Services.GetRequiredService<KeypairHelper>();
await keypairHelper.TryCreateUserKeypair();
await keypairHelper.TryCreateSiteActorKeypair();
if (keypairHelper.ActorRsaPrivateKey is null || keypairHelper.ActorRsaPublicKey is null)
{
if (Globals.CanRunWithoutKeys)
{
Output.WriteLine($"{Output.Ansi.Yellow} % Warning: HttpSig keypair is missing - federation will not work.");
Output.WriteLine($"{Output.Ansi.Yellow} % Continuing startup because {Globals.ENV_UNSAFE_KEYS} is set.");
}
else
{
Output.WriteLine($"{Output.Ansi.Red} % HttpSig keypair is missing - federation will definitely not work.");
Output.WriteLine($"{Output.Ansi.Red} % The app will now exit to protect itself from federation errors.");
Output.WriteLine($"{Output.Ansi.Red} % You can override this behavior by setting this environment variable: (not recommended)");
Output.WriteLine($"{Output.Ansi.Red} % {Globals.ENV_UNSAFE_KEYS}=1");
Environment.Exit(6);
}
}
else
{
Output.WriteLine($"{Output.Ansi.Green} % HttpSig keys loaded.");
}
return app;
}
public static async Task<WebApplication> InitMediaStorage(this WebApplication app)
{
try
{
if (!Directory.Exists(Globals.MediaDir))
{
Output.WriteLine($"{Output.Ansi.Blue} 🖹 Created public media directory at: {Globals.MediaDir}");
Directory.CreateDirectory(Globals.MediaDir);
}
using (var fs = File.Create(Path.Combine(Globals.MediaDir, Globals.MEDIA_TMP_PROBE_FILENAME)))
{
fs.WriteByte((byte)'1');
fs.Close();
}
File.Delete(Path.Combine(Globals.MediaDir, Globals.MEDIA_TMP_PROBE_FILENAME));
Output.WriteLine($"{Output.Ansi.Green} 🖹 Media directory initialized. File count: {Output.Ansi.Bold}{Directory.GetFiles(Globals.MediaDir, "*", searchOption: SearchOption.AllDirectories).Length:#,#0}");
}
catch (Exception ex)
{
Output.WriteLine($"{Output.Ansi.Red} 🖹 Media storage error - is the directory writeable? ({ex.GetType().Name}: {ex.Message})");
}
return app;
}
public static async Task<WebApplication> InitDatabase(this WebApplication app)
{
try
{
await app.Services.GetRequiredService<StorageService>().StartupDbConnection();
}
catch (ApageeException aex)
{
Output.WriteLine($"{Output.Ansi.Red}Startup error: {aex.Message}");
if (aex.InnerException is not null)
{
Output.WriteLine($" {Output.Ansi.Red}Nested error: {aex.InnerException.GetType().FullName}: {aex.InnerException.Message}");
}
if (app.Environment.IsDevelopment())
{
Output.WriteLine(Output.Ansi.Yellow + aex.ToString());
}
Environment.Exit(4);
}
catch (Exception ex)
{
Output.WriteLine($"{Output.Ansi.Red}Unknown startup error: {ex.GetType().FullName}: {ex.Message}");
if (ex.InnerException is not null)
{
Output.WriteLine($" {Output.Ansi.Red}Nested error: {ex.InnerException.GetType().FullName}: {ex.InnerException.Message}");
}
if (app.Environment.IsDevelopment())
{
Output.WriteLine(Output.Ansi.Yellow + ex.ToString());
}
Environment.Exit(5);
}
return app;
}
public static async Task<WebApplication> CheckFirstTimeUserSetup(this WebApplication app)
{
var userService = app.Services.GetRequiredService<UserService>();
var user = await userService.GetUser();
if (user is null)
{
var newPass = RandomUtils.GetRandomAlphanumeric(24);
var newUser = new User
{
Uid = Guid.NewGuid().ToString(),
PassHash = "",
Username = "admin"
};
await userService.UpsertUser(newUser, newPass);
Output.WriteLine($"""
{Output.Ansi.Magenta}
+-------------------------------------------------+
| No user was detected, auto-generating... |
| (This information will only be displayed once.) |
| |
| User: {Output.Ansi.Bold}admin{Output.Ansi.Reset}{Output.Ansi.Magenta} |
| Pass: {Output.Ansi.Bold}{newPass}{Output.Ansi.Reset}{Output.Ansi.Magenta} |
| |
+-------------------------------------------------+
""");
}
return app;
}
}