Эх сурвалжийг харах

Load config when startup.

justin.xing 3 жил өмнө
parent
commit
85b14a82a5

+ 9 - 0
fis/IPlatformService.cs

@@ -0,0 +1,9 @@
+namespace fis
+{
+    public interface IPlatformService
+    {
+        
+    }
+
+}
+

+ 47 - 0
fis/JsonRpcHandler.cs

@@ -0,0 +1,47 @@
+using System.IO;
+using JsonRpcLite.InProcess;
+using JsonRpcLite.Rpc;
+using Xilium.CefGlue.Common.Handlers;
+
+namespace fis
+{
+    public sealed class JsonRpcHandler<T> : IHostHandler
+    {
+        private readonly JsonRpcInProcessEngine _engine = new();
+        private readonly JsonRpcServer _server = new();
+        private readonly JsonRpcClient _client = new();
+
+        public string Host { get; }
+
+        public JsonRpcHandler(string host, T service)
+        {
+            Host = host;
+            _server.UseEngine(_engine);
+            _client.UseEngine(_engine);
+            _server.RegisterService(service);
+            _server.Start();
+        }
+
+        public HandleResult? Handle(string path, byte[]? postData)
+        {
+            try
+            {
+                var serviceName = path;
+                if (serviceName.StartsWith("/"))
+                {
+                    serviceName = serviceName.Substring(1);
+                }
+                var resultData = _client.ProcessAsync(serviceName, postData).Result;
+                var resultStream = new MemoryStream(resultData);
+                return new HandleResult(resultStream, "application/json");
+            }
+            catch
+            {
+                // ignored
+            }
+
+            return null;
+        }
+    }
+}
+

+ 8 - 0
fis/MacService.cs

@@ -0,0 +1,8 @@
+namespace fis
+{
+    public class MacService : IPlatformService
+    {
+
+    }
+
+}

+ 18 - 3
fis/MainWindow.axaml.cs

@@ -26,11 +26,26 @@ namespace fis
         {
             AvaloniaXamlLoader.Load(this);
             var browserContainer = this.FindControl<Border>("BrowserBorder");
-            var flyinsonoHandler = new FileSystemHostHandler("flyinsono.com",Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "flyinsono.com"));
+            var appHandler = new FileSystemHostHandler(ShellConfig.Instance.AppHost, ShellConfig.Instance.AppResourcePath);
+            IPlatformService platformService;
+            if (Environment.OSVersion.Platform == PlatformID.Win32NT)
+            {
+                platformService = new WinService();
+            }
+            else if (Environment.OSVersion.Platform == PlatformID.MacOSX)
+            {
+                platformService = new MacService();
+            }
+            else
+            {
+                throw new NotSupportedException($"Platform {Environment.OSVersion.Platform} is not suppoorted");
+            }
+            var platformHandler = new JsonRpcHandler<IPlatformService>(ShellConfig.Instance.LocalApiHost, platformService);
             var handler = new HostRequestHandler();
-            handler.RegisterHostHandler(flyinsonoHandler);
+            handler.RegisterHostHandler(appHandler);
+            handler.RegisterHostHandler(platformHandler);
             _browser = new AvaloniaCefBrowser();
-            _browser.Address = "http://flyinsono.com/index.html";
+            _browser.Address = ShellConfig.Instance.AppHost;
             _browser.RequestHandler = handler;
             _browser.ContextMenuHandler = new TextContextMenuHandler(_browser);
             browserContainer.Child = _browser;

+ 1 - 4
fis/Program.cs

@@ -1,9 +1,6 @@
-using System;
-using System.Collections.Generic;
+using System.Collections.Generic;
 using System.Globalization;
 using Avalonia;
-using Avalonia.Controls;
-using Avalonia.Controls.ApplicationLifetimes;
 using Xilium.CefGlue;
 using Xilium.CefGlue.Common;
 

+ 62 - 0
fis/ShellConfig.cs

@@ -0,0 +1,62 @@
+using System;
+using System.IO;
+using System.Text.Json;
+
+namespace fis
+{
+    public class ShellConfig
+    {
+        public readonly static ShellConfig Instance = Load();
+
+        //Gets or sets the app host which is the web App's access url
+        public string AppHost { get; set; } = "app.fis.plus";
+
+        //Gets or sets the App resource path, which stores the web content and its resources.
+        public string AppResourcePath { get; set; } = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "App");
+
+        //Gets or sets the api host for local call, usually provider platform apis.
+        public string LocalApiHost { get; set; } = "platform.fis.plus";
+
+        //Gets or sets the resource path for local application resources, eg. icons, images, audios and videos.
+        public string LocalResourcePath { get; set; } = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Resources");
+
+
+        private static ShellConfig Load()
+        {
+            var configPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ShellConfig.json");
+            if (File.Exists(configPath))
+            {
+                ShellConfig? config = null;
+                try
+                {
+                    config = JsonSerializer.Deserialize<ShellConfig>(File.ReadAllText(configPath));
+                    if(config != null)
+                    {
+                        return config;
+                    }
+                }
+                catch
+                {
+                    //
+                }
+                return new ShellConfig()
+                {
+                    AppResourcePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "App"),
+                    LocalResourcePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Resources"),
+                    AppHost = "app.fis.plus",
+                    LocalApiHost = "platform.fis.plus"
+                };
+            }
+            else
+            {
+                return new ShellConfig()
+                {
+                    AppResourcePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "App"),
+                    LocalResourcePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Resources"),
+                    AppHost = "app.fis.plus",
+                    LocalApiHost = "platform.fis.plus"
+                };
+            }
+        }
+    }
+}

+ 8 - 0
fis/WinService.cs

@@ -0,0 +1,8 @@
+namespace fis
+{
+    public class WinService : IPlatformService
+    {
+
+    }
+
+}

+ 1 - 0
fis/fis.Mac.csproj

@@ -11,6 +11,7 @@
     <PackageReference Include="Avalonia.Diagnostics" Version="0.10.3" />
     <PackageReference Include="Cef.Native.osx-x64" Version="1.0.0" />
     <PackageReference Include="CefGlueBrowserProcess.osx-x64" Version="1.0.0" />
+    <PackageReference Include="JsonRpcLite" Version="1.0.0" />
     <PackageReference Include="Vinno.CefGlue.Avalonia" Version="1.0.0" />
    </ItemGroup>
 </Project>

+ 1 - 0
fis/fis.Win.csproj

@@ -11,6 +11,7 @@
     <PackageReference Include="Avalonia.Diagnostics" Version="0.10.3" />
     <PackageReference Include="Cef.Native.win-x86" Version="1.0.0" />
     <PackageReference Include="CefGlueBrowserProcess.win-x86" Version="1.0.0" />
+    <PackageReference Include="JsonRpcLite" Version="1.0.0" />
     <PackageReference Include="Vinno.CefGlue.Avalonia" Version="1.0.0" />
    </ItemGroup>
 </Project>