using System.Runtime.InteropServices; using XFEExtension.NetCore.CyberComm; using XFEExtension.NetCore.ServerInteractive.Utilities.Extensions; using XFEExtension.NetCore.ServerInteractive.Utilities.Server; using XFEWorkflow.Space.Contracts; using XFEWorkflow.Space.Server; string? Option(string name) => args.FirstOrDefault(x => x.StartsWith(name + "=", StringComparison.Ordinal))?[(name.Length + 1)..]; var root = Path.GetFullPath(Option("--data") ?? Path.Combine(AppContext.BaseDirectory, "data")); Directory.CreateDirectory(Path.Combine(root, "logs")); Console.OutputEncoding = System.Text.Encoding.UTF8; var store = new SpaceStore(root); SpaceHost.Store = store; if (args.Contains("--init-admin")) { var account = Option("--account") ?? throw new ArgumentException("请提供 --account=管理员账号"); Console.Write("管理员密码(至少 12 位,支持标准输入):"); string password; if (Console.IsInputRedirected) password = Console.ReadLine() ?? ""; else { var text = new System.Text.StringBuilder(); while (true) { var key = Console.ReadKey(true); if (key.Key == ConsoleKey.Enter) break; if (key.Key == ConsoleKey.Backspace) { if (text.Length > 0) text.Length--; } else if (!char.IsControl(key.KeyChar)) text.Append(key.KeyChar); } password = text.ToString(); Console.WriteLine(); } var user = await store.RegisterAsync(account, password, account, true); Console.WriteLine("管理员已创建:" + user.UserName); return; } var url = Option("--url") ?? store.Profile.Read(p => p.InstanceListenUrl); var pfx = Option("--tls-pfx") ?? store.Profile.Read(p => p.InstancePfxPath); store.Profile.Update(p => { p.InstanceListenUrl = url; p.InstancePfxPath = pfx; }); await store.Profile.SaveAsync(); var core = XFEServerCoreBuilder.CreateBuilder().UseXFEStandardServerCore(options => { options.GetUserFunction = () => store.Users; options.AddUserFunction = store.UpdateUser; options.UpdateUserFunction = store.UpdateUser; options.GetEncryptedUserLoginModelFunction = () => store.Sessions; options.AddEncryptedUserLoginModelFunction = store.AddSession; options.RemoveEncryptedUserLoginModelFunction = store.RemoveSession; options.GetLoginKeepDays = () => 7; options.LoginResultConvertFunction = user => store.PublicUser((XFEExtension.NetCore.ServerInteractive.Interfaces.IUserInfo)user); }).AddService().AddService().AddService().AddService() .Build(options => { options.ServerCoreName = "XFEWorkflow.Space"; options.MainEntryPoint = "space/v1"; options.AcceptGet = true; options.AcceptPost = true; options.AcceptNonStandardJson = true; options.BindIP(url); if (pfx.Length > 0) options.Tls = new CyberCommTlsOptions { PfxPath = Path.GetFullPath(pfx), PfxPassword = Environment.GetEnvironmentVariable("XFE_TLS_PFX_PASSWORD") }; }); await using var server = XFEServerBuilder.CreateBuilder().UseXFEServer().AddInitializer(new SpaceLogInitializer(root)).AddServerCore(core).Build(); using var shutdown = new CancellationTokenSource(); Console.CancelKeyPress += (_, e) => { e.Cancel = true; shutdown.Cancel(); }; AppDomain.CurrentDomain.ProcessExit += (_, _) => { if (!shutdown.IsCancellationRequested) shutdown.Cancel(); }; using var signal = !OperatingSystem.IsWindows() ? PosixSignalRegistration.Create(PosixSignal.SIGTERM, ctx => { ctx.Cancel = true; shutdown.Cancel(); }) : null; Console.WriteLine($"XFEWorkflow Space:{url}space/v1/ · 数据:{root}"); try { await server.RunAsync(shutdown.Token); } finally { using var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(15)); await store.Profile.FlushAsync(timeout.Token); } sealed class SpaceLogInitializer(string root) : XFEExtension.NetCore.ServerInteractive.Implements.ServerService.ServerInitializerServiceBase { public override void Initialize() => XFEExtension.NetCore.XFEConsole.XFEConsole.Log.LogPath = Path.Combine(root, "logs", DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss") + ".log"); }