浏览代码

Created a "Hello, World!" web listener.

Kenric Nugteren 1 年之前
父节点
当前提交
cbbfa8de97
共有 1 个文件被更改,包括 68 次插入0 次删除
  1. 68 0
      prs.licensing/Engine/LicensingEngine.cs

+ 68 - 0
prs.licensing/Engine/LicensingEngine.cs

@@ -1,5 +1,7 @@
 using Comal.Classes;
 using Comal.Stores;
+using GenHTTP.Api.Protocol;
+using GenHTTP.Modules.IO;
 using InABox.Clients;
 using InABox.Configuration;
 using InABox.Core;
@@ -14,11 +16,57 @@ using System.Linq;
 using System.Text;
 using System.Threading;
 using System.Threading.Tasks;
+using RequestMethod = GenHTTP.Api.Protocol.RequestMethod;
 
 namespace PRSLicensing;
 
+public class LicensingHandler : Handler<LicensingHandlerProperties>
+{
+    public override void Init(LicensingHandlerProperties properties)
+    {
+    }
+
+    private IResponseBuilder HandleGET(IRequest request)
+    {
+        return request.Respond().Content("Hello, World!").Status(ResponseStatus.OK);
+    }
+
+    private IResponseBuilder HandlePOST(IRequest request)
+    {
+        return request.Respond().Status(ResponseStatus.NotFound);
+    }
+
+    public override ValueTask<IResponse?> HandleAsync(IRequest request)
+    {
+        try
+        {
+            switch (request.Method.KnownMethod)
+            {
+                case RequestMethod.GET:
+                    return new ValueTask<IResponse?>(HandleGET(request).Build());
+                case RequestMethod.POST:
+                    return new ValueTask<IResponse?>(HandlePOST(request).Build());
+                default:
+                    Logger.Send(LogType.Error, ClientFactory.UserID, $"Request method {request.Method.RawMethod} unknown");
+                    return new ValueTask<IResponse?>(request.Respond().Status(ResponseStatus.MethodNotAllowed).Build());
+            }
+        }
+        catch (Exception eListen)
+        {
+            Logger.Send(LogType.Error, ClientFactory.UserID, eListen.Message);
+            return new ValueTask<IResponse?>(request.Respond().Status(ResponseStatus.InternalServerError).Build());
+        }
+    }
+}
+
+public class LicensingHandlerProperties
+{
+}
+
 public class LicensingEngine : Engine<LicensingEngineProperties>
 {
+    private Listener<LicensingHandler, LicensingHandlerProperties> listener;
+
     public override void Run()
     {
         Logger.Send(LogType.Information, "", "Starting..");
@@ -43,8 +91,23 @@ public class LicensingEngine : Engine<LicensingEngineProperties>
         ConfigurationUtils.RegisterClasses();
 
         Logger.Send(LogType.Information, "", "Starting Listener on port " + Properties.ListenPort);
+
+        try
+        {
+            listener = new Listener<LicensingHandler, LicensingHandlerProperties>(new LicensingHandlerProperties());
+            listener.InitHTTPS((ushort)Properties.ListenPort, CertificateFileName());
+
+            Logger.Send(LogType.Information, "", "Starting Web Listener on port " + Properties.ListenPort);
+            listener.Start();
+        }
+        catch (Exception eListen)
+        {
+            Logger.Send(LogType.Error, ClientFactory.UserID, eListen.Message);
+        }
     }
 
+    private string CertificateFileName() => Properties.CertificateFile;
+
     private void CheckConnection()
     {
         // Wait for server connection
@@ -61,6 +124,7 @@ public class LicensingEngine : Engine<LicensingEngineProperties>
     public override void Stop()
     {
         Logger.Send(LogType.Information, "", "Stopping");
+        listener?.Stop();
     }
 }
 
@@ -75,6 +139,10 @@ public class LicensingEngineProperties : ServerProperties
     [EditorSequence(2)]
     public int ListenPort { get; set; }
 
+    [EditorSequence(3)]
+    [FileNameEditor("Certificate Files (*.pfx)|*.pfx")]
+    public string CertificateFile { get; set; }
+
     public override ServerType Type()
     {
         return ServerType.Other;