fix: added GetRouteCollection method, additional refactoring to resolve use of base class

This commit is contained in:
jdevito
2023-01-30 16:25:23 -06:00
parent ba930dafaf
commit 627a39e63a
2 changed files with 49 additions and 28 deletions

View File

@@ -14,7 +14,7 @@ namespace PepperDash.Core.Web.RequestHandlers
protected override void HandleConnect(HttpCwsContext context) protected override void HandleConnect(HttpCwsContext context)
{ {
context.Response.StatusCode = 501; context.Response.StatusCode = 501;
context.Response.StatusDescription = "Not Implemented"; context.Response.StatusDescription = "Not Implemented";
context.Response.End(); context.Response.End();
} }

View File

@@ -1,6 +1,10 @@
using System; using System;
using System.Collections.Generic;
using System.Linq;
using Crestron.SimplSharp; using Crestron.SimplSharp;
using Crestron.SimplSharp.WebScripting; using Crestron.SimplSharp.WebScripting;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using PepperDash.Core.Web.RequestHandlers; using PepperDash.Core.Web.RequestHandlers;
namespace PepperDash.Core.Web namespace PepperDash.Core.Web
@@ -18,8 +22,8 @@ namespace PepperDash.Core.Web
private const uint DebugInfo = 1; private const uint DebugInfo = 1;
private const uint DebugVerbose = 2; private const uint DebugVerbose = 2;
private HttpCwsServer _server;
private readonly CCriticalSection _serverLock = new CCriticalSection(); private readonly CCriticalSection _serverLock = new CCriticalSection();
private HttpCwsServer _server;
/// <summary> /// <summary>
/// Web API server key /// Web API server key
@@ -41,6 +45,28 @@ namespace PepperDash.Core.Web
/// </summary> /// </summary>
public bool IsRegistered { get; private set; } public bool IsRegistered { get; private set; }
/// <summary>
/// Http request handler
/// </summary>
//public IHttpCwsHandler HttpRequestHandler
//{
// get { return _server.HttpRequestHandler; }
// set
// {
// if (_server == null) return;
// _server.HttpRequestHandler = value;
// }
//}
/// <summary>
/// Received request event handler
/// </summary>
//public event EventHandler<HttpCwsRequestEventArgs> ReceivedRequestEvent
//{
// add { _server.ReceivedRequestEvent += new HttpCwsRequestEventHandler(value); }
// remove { _server.ReceivedRequestEvent -= new HttpCwsRequestEventHandler(value); }
//}
/// <summary> /// <summary>
/// Constructor for S+. Make sure to set necessary properties using init method /// Constructor for S+. Make sure to set necessary properties using init method
/// </summary> /// </summary>
@@ -60,12 +86,12 @@ namespace PepperDash.Core.Web
} }
/// <summary> /// <summary>
/// /// Constructor
/// </summary> /// </summary>
/// <param name="key"></param> /// <param name="key"></param>
/// <param name="name"></param> /// <param name="name"></param>
/// <param name="basePath"></param> /// <param name="basePath"></param>
public WebApiServer(string key, string name, string basePath) public WebApiServer(string key, string name, string basePath)
{ {
Key = key; Key = key;
Name = string.IsNullOrEmpty(name) ? DefaultName : name; Name = string.IsNullOrEmpty(name) ? DefaultName : name;
@@ -73,6 +99,9 @@ namespace PepperDash.Core.Web
if (_server == null) _server = new HttpCwsServer(BasePath); if (_server == null) _server = new HttpCwsServer(BasePath);
_server.setProcessName(Key);
_server.HttpRequestHandler = new DefaultRequestRequestHandler();
CrestronEnvironment.ProgramStatusEventHandler += CrestronEnvironment_ProgramStatusEventHandler; CrestronEnvironment.ProgramStatusEventHandler += CrestronEnvironment_ProgramStatusEventHandler;
CrestronEnvironment.EthernetEventHandler += CrestronEnvironment_EthernetEventHandler; CrestronEnvironment.EthernetEventHandler += CrestronEnvironment_EthernetEventHandler;
} }
@@ -85,7 +114,7 @@ namespace PepperDash.Core.Web
{ {
if (programEventType != eProgramStatusEventType.Stopping) return; if (programEventType != eProgramStatusEventType.Stopping) return;
Debug.Console(DebugInfo, this, "Program stopping. Disabling Server"); Debug.Console(DebugInfo, this, "Program stopping. stopping server");
Stop(); Stop();
} }
@@ -147,6 +176,14 @@ namespace PepperDash.Core.Web
_server.Routes.Remove(route); _server.Routes.Remove(route);
} }
/// <summary>
/// Returns a list of the current routes
/// </summary>
public HttpCwsRouteCollection GetRouteCollection()
{
return _server.Routes;
}
/// <summary> /// <summary>
/// Starts CWS instance /// Starts CWS instance
/// </summary> /// </summary>
@@ -168,9 +205,9 @@ namespace PepperDash.Core.Web
return; return;
} }
Debug.Console(DebugInfo, this, "Starting server");
IsRegistered = _server.Register(); IsRegistered = _server.Register();
Debug.Console(DebugInfo, this, "Starting server, registration {0}", IsRegistered ? "was successful" : "failed");
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -201,6 +238,9 @@ namespace PepperDash.Core.Web
} }
IsRegistered = _server.Unregister() == false; IsRegistered = _server.Unregister() == false;
Debug.Console(DebugInfo, this, "Stopping server, unregistration {0}", IsRegistered ? "failed" : "was successful");
_server.Dispose(); _server.Dispose();
_server = null; _server = null;
} }
@@ -229,27 +269,8 @@ namespace PepperDash.Core.Web
{ {
try try
{ {
Debug.Console(DebugInfo, this, @"RecieveRequestEventHandler var j = JsonConvert.SerializeObject(args.Context, Formatting.Indented);
Method: {0} Debug.Console(DebugVerbose, this, "RecieveRequestEventHandler Context:\x0d\x0a{0}", j);
Path: {1}
PathInfo: {2}
PhysicalPath: {3}
ContentType: {4}
RawUrl: {5}
Url: {6}
UserAgent: {7}
UserHostAddress: {8}
UserHostName: {9}",
args.Context.Request.HttpMethod,
args.Context.Request.Path,
args.Context.Request.PathInfo,
args.Context.Request.PhysicalPath,
args.Context.Request.ContentType,
args.Context.Request.RawUrl,
args.Context.Request.Url,
args.Context.Request.UserAgent,
args.Context.Request.UserHostAddress,
args.Context.Request.UserHostName);
} }
catch (Exception ex) catch (Exception ex)
{ {