RestListener.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using System.Net;
  2. using System.Security.Cryptography.X509Certificates;
  3. using GenHTTP.Api.Infrastructure;
  4. using GenHTTP.Engine;
  5. using GenHTTP.Modules.Practices;
  6. namespace InABox.API
  7. {
  8. public static class RestListener
  9. {
  10. private static IServerHost? _host;
  11. //private static RestPusher? _pusher;
  12. public static X509Certificate2? Certificate { get; private set; }
  13. public static void Start()
  14. {
  15. _host?.Start();
  16. //_pusher?.Start();
  17. }
  18. public static void Stop()
  19. {
  20. _host?.Stop();
  21. //_pusher?.Stop();
  22. }
  23. public static void Init(ushort port, X509Certificate2? cert)
  24. {
  25. _host = Host.Create();
  26. _host.Handler(new RestHandlerBuilder()).Defaults().Backlog(1024);
  27. Certificate = cert;
  28. RestService.IsHTTPS = cert != null;
  29. if (cert != null)
  30. _host?.Bind(IPAddress.Any, port, cert);
  31. else
  32. _host?.Bind(IPAddress.Any, port);
  33. }
  34. // /// <summary>
  35. // /// Clears certificate and host information, and stops the listener.
  36. // /// </summary>
  37. // public static void Clear()
  38. // {
  39. // _host?.Stop();
  40. // _host = null;
  41. //
  42. // //_pusher?.Stop();
  43. // //_pusher = null;
  44. //
  45. // Certificate = null;
  46. // }
  47. /// <summary>
  48. /// Initialise rest listener, and set up web socket port if non-zero.
  49. /// </summary>
  50. /// <param name="webSocketPort">The web-socket port to use, or 0 for no websocket.</param>
  51. // public static void Init() //int webSocketPort)
  52. // {
  53. // if(webSocketPort != 0)
  54. // {
  55. // _pusher = new RestPusher(webSocketPort);
  56. // PushManager.AddPusher(_pusher);
  57. // }
  58. //
  59. // _host = Host.Create();
  60. // _host.Handler(new RestHandlerBuilder(_pusher?.Port)).Defaults().Backlog(1024);
  61. // }
  62. }
  63. }