Skip to content

Commit

Permalink
Added serve front end. Refactored serve html
Browse files Browse the repository at this point in the history
  • Loading branch information
derekShaheen committed Apr 7, 2024
1 parent 32e6677 commit ef7bdb6
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions WickerNetwork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ private async void HandleRequests(CancellationToken cancellationToken)
}
else if (request.Url.AbsolutePath == "/")
{
await ServeHtmlPage(response, WickerServer.Instance.FrontEnd != null ? WickerServer.Instance.FrontEnd.Value : "index.html");
await ServeFrontEnd(response, WickerServer.Instance.FrontEnd != null ? WickerServer.Instance.FrontEnd.Value : "index.html");
}
else if (request.Url.AbsolutePath == FAVICON_PATH)
{
Expand Down Expand Up @@ -217,7 +217,29 @@ private void ServeCommandHandlers(HttpListenerResponse response)
}
}

private async System.Threading.Tasks.Task ServeHtmlPage(HttpListenerResponse response, string fileName)
/// <summary>
/// Serve an HTML page from the user data directory.
/// </summary>
/// <param name="response"></param>
/// <param name="fileName"></param>
/// <returns></returns>
public void ServeHTMLPage(HttpListenerResponse response, string fileName)
{
var filePath = Path.Combine(WickerServer.userDataPath, fileName);
//await Utilities.EnsureFileExists(filePath, INDEX_URL);

if (File.Exists(filePath))
{
var pageContent = File.ReadAllBytes(filePath);
SendResponse(response, System.Text.Encoding.UTF8.GetString(pageContent), statusCode: 200, contentType: "text/html");
}
else
{
SendResponse(response, "Page not found.", statusCode: 404);
}
}

private async System.Threading.Tasks.Task ServeFrontEnd(HttpListenerResponse response, string fileName)
{
var filePath = Path.Combine(WickerServer.resourcesPath, fileName);
await Utilities.EnsureFileExists(filePath, INDEX_URL);
Expand Down

0 comments on commit ef7bdb6

Please sign in to comment.