Enhance WebController to support a web site
gerritv opened this issue · 4 comments
I need/want to add some web pages to the server. This will lead to using that web pages to show statistics of my libraries and to also Update/Remove etc. books and documents so I don't have to use Command window. To that end I added an empty index.html to the project. And then enhanced the webcontroller to support a general web site. If you issue http://:8080 it will get index.html (as default page)
public class WebController : ApiController
{
/// <summary>
/// Handles web site if present, otherwise redirects to /opds
/// </summary>
/// <param name="filename"></param>
/// <returns>Http Response message</returns>
//[Route("{*filename}")]
[HttpGet]
public HttpResponseMessage ServeIndex(string filename)
{
var file = GetFile("index.html");
if (filename == null)
{
if (file == null)
{
file = Request.CreateResponse(HttpStatusCode.Moved);
file.Headers.Location = new Uri("/opds", UriKind.Relative);
}
else
{
return file;
}
}
else
{
if ((file = GetFile(filename)) == null)
{
file = Request.CreateResponse(HttpStatusCode.NotFound);
}
}
return file;
}
index.html
served on all routes except /static/*
and /opds/*
because of Single-Page Applications (written in React, Angular, etc...) routing requirement (called "HTML5 mode" sometimes). I didn't want to build general purpose server as it'll still requires ajax calls on all pages.
However I can change logic if you really need to serve bunch of static htmls: if filename
+ .html
exists serve it, otherwise check for index.html
existence. If both failed - redirect to /opds
or say "Not found".
I think (thought) that is what my code accomplished? :-) I will send you an email today with my thoughts on what my goals are for the document management problem I am solving. Your recent checkins go a long way to making this a perfect solution for me.
Well, maybe not an email 'Mailbox size limit exceeded '