ln.http/ln.http.service/Program.cs

60 lines
1.7 KiB
C#

using System.IO;
using System.Reflection;
using System.Threading;
using ln.bootstrap;
using ln.http.router;
using ln.templates.html;
namespace ln.http.service
{
class Program
{
static void Main(string[] args)
{
Bootstrap
.DefaultInstance
.ServiceContainer.RegisterService<HttpServiceHelper>();
Bootstrap
.DefaultInstance
.Start();
}
}
class HttpServiceHelper : HttpRouter
{
private TemplateDocument _templateDocument;
public HttpServiceHelper(HttpServer httpServer)
:base(httpServer)
{
using (StreamReader reader = new StreamReader(
Assembly
.GetExecutingAssembly()
.GetManifestResourceStream("ln.http.service.error.html")
)
)
{
_templateDocument = TemplateReader.ReadTemplate(reader);
}
Map(HttpMethod.ANY, "/_err.html", HttpRoutePriority.LOW, this.HttpError);
}
bool HttpError(HttpContext httpContext)
{
HttpResponse httpResponse =
new HttpResponse(httpContext.HttpException?.HttpStatusCode ?? HttpStatusCode.InternalServerError);
RenderContext renderContext = new RenderContext(httpResponse.ContentWriter);
renderContext
.GetEngine()
.SetValue("httpContext", httpContext);
_templateDocument.RenderTemplate(renderContext);
httpContext.Response = httpResponse;
return true;
}
}
}