1
0
mirror of https://github.com/chylex/Minecraft-Phantom-Panel.git synced 2024-11-25 16:42:54 +01:00

Compare commits

...

1 Commits

Author SHA1 Message Date
1ce0c4cfa9
WIP 2023-10-15 01:16:48 +02:00
4 changed files with 137 additions and 0 deletions

24
.run/Web.run.xml Normal file
View File

@ -0,0 +1,24 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Web" type="DotNetProject" factoryName=".NET Project">
<option name="EXE_PATH" value="$PROJECT_DIR$/.artifacts/bin/Phantom.Web/debug/Phantom.Web.exe" />
<option name="PROGRAM_PARAMETERS" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/.workdir/Web" />
<option name="PASS_PARENT_ENVS" value="1" />
<envs>
<env name="ASPNETCORE_ENVIRONMENT" value="Development" />
<env name="WEB_SERVER_HOST" value="localhost" />
</envs>
<option name="USE_EXTERNAL_CONSOLE" value="0" />
<option name="USE_MONO" value="0" />
<option name="RUNTIME_ARGUMENTS" value="" />
<option name="PROJECT_PATH" value="$PROJECT_DIR$/Web/Phantom.Web/Phantom.Web.csproj" />
<option name="PROJECT_EXE_PATH_TRACKING" value="1" />
<option name="PROJECT_ARGUMENTS_TRACKING" value="1" />
<option name="PROJECT_WORKING_DIRECTORY_TRACKING" value="0" />
<option name="PROJECT_KIND" value="DotNetCore" />
<option name="PROJECT_TFM" value="net8.0" />
<method v="2">
<option name="Build" />
</method>
</configuration>
</component>

View File

@ -0,0 +1,69 @@
using System.Reflection;
using Phantom.Common.Logging;
using Phantom.Controller.Services;
using Phantom.Utils.Cryptography;
using Phantom.Utils.IO;
using Phantom.Utils.Runtime;
using Phantom.Utils.Tasks;
using Phantom.Web;
var cancellationTokenSource = new CancellationTokenSource();
PosixSignals.RegisterCancellation(cancellationTokenSource, static () => {
PhantomLogger.Root.InformationHeading("Stopping Phantom Panel web...");
});
static void CreateFolderOrStop(string path, UnixFileMode chmod) {
if (!Directory.Exists(path)) {
try {
Directories.Create(path, chmod);
} catch (Exception e) {
PhantomLogger.Root.Fatal(e, "Error creating folder: {FolderName}", path);
throw StopProcedureException.Instance;
}
}
}
try {
var fullVersion = AssemblyAttributes.GetFullVersion(Assembly.GetExecutingAssembly());
PhantomLogger.Root.InformationHeading("Initializing Phantom Panel web...");
PhantomLogger.Root.Information("Web version: {Version}", fullVersion);
var (webServerHost, webServerPort, webBasePath) = Variables.LoadOrStop();
string webKeysPath = Path.GetFullPath("./keys");
CreateFolderOrStop(webKeysPath, Chmod.URWX);
PhantomLogger.Root.InformationHeading("Launching Phantom Panel web...");
var taskManager = new TaskManager(PhantomLogger.Create<TaskManager>("Web"));
try {
var configuration = new Configuration(PhantomLogger.Create("Web"), webServerHost, webServerPort, webBasePath, webKeysPath, cancellationTokenSource.Token);
var administratorToken = TokenGenerator.Create(60);
PhantomLogger.Root.Information("Your administrator token is: {AdministratorToken}", administratorToken);
PhantomLogger.Root.Information("For administrator setup, visit: {HttpUrl}{SetupPath}", configuration.HttpUrl, configuration.BasePath + "setup");
var serviceConfiguration = new ServiceConfiguration(fullVersion, TokenGenerator.GetBytesOrThrow(administratorToken), cancellationTokenSource.Token);
var webApplication = Launcher.CreateApplication(configuration, serviceConfiguration, taskManager);
await Launcher.Launch(configuration, webApplication);
} finally {
cancellationTokenSource.Cancel();
await taskManager.Stop();
}
return 0;
} catch (OperationCanceledException) {
return 0;
} catch (StopProcedureException) {
return 1;
} catch (Exception e) {
PhantomLogger.Root.Fatal(e, "Caught exception in entry point.");
return 1;
} finally {
cancellationTokenSource.Dispose();
PhantomLogger.Root.Information("Bye!");
PhantomLogger.Dispose();
}

View File

@ -0,0 +1,7 @@
namespace Phantom.Controller.Services;
public sealed record ServiceConfiguration(
string Version,
byte[] AdministratorToken,
CancellationToken CancellationToken
);

View File

@ -0,0 +1,37 @@
using Phantom.Common.Logging;
using Phantom.Utils.Runtime;
namespace Phantom.Web;
sealed record Variables(
string ControllerHost,
ushort ControllerPort,
string? WebKeyToken,
string? WebKeyFilePath,
string WebServerHost,
ushort WebServerPort,
string WebBasePath
) {
private static Variables LoadOrThrow() {
var (webKeyToken, webKeyFilePath) = EnvironmentVariables.GetEitherString("WEB_KEY", "WEB_KEY_FILE").Require;
return new Variables(
EnvironmentVariables.GetString("CONTROLLER_HOST").Require,
EnvironmentVariables.GetPortNumber("CONTROLLER_PORT").WithDefault(9402),
webKeyToken,
webKeyFilePath,
EnvironmentVariables.GetString("WEB_SERVER_HOST").WithDefault("0.0.0.0"),
EnvironmentVariables.GetPortNumber("WEB_SERVER_PORT").WithDefault(9400),
EnvironmentVariables.GetString("WEB_BASE_PATH").Validate(static value => value.StartsWith('/') && value.EndsWith('/'), "Environment variable must begin and end with '/'").WithDefault("/")
);
}
public static Variables LoadOrStop() {
try {
return LoadOrThrow();
} catch (Exception e) {
PhantomLogger.Root.Fatal(e.Message);
throw StopProcedureException.Instance;
}
}
}