Skip to content

Commit

Permalink
Relative base path for OWIN setup
Browse files Browse the repository at this point in the history
  • Loading branch information
mausch committed Jun 30, 2015
1 parent 85795ad commit 0208b3e
Show file tree
Hide file tree
Showing 14 changed files with 69 additions and 41 deletions.
5 changes: 3 additions & 2 deletions QuartzNetWebConsole.Web/ControllerFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Linq;
using System.Web;
using MiniMVC;
using QuartzNetWebConsole.Utils;
using Response = QuartzNetWebConsole.Utils.Response;
using Route = System.Collections.Generic.KeyValuePair<string, System.Action<System.Web.HttpContextBase>>;

Expand All @@ -16,9 +17,9 @@ public override IHttpHandler GetHandler(HttpContextBase context) {
.FirstOrDefault();
}

private static Action<HttpContextBase> ToWebAction(Func<Uri, Response> handler) {
private static Action<HttpContextBase> ToWebAction(Func<RelativeUri, Response> handler) {
return ctx => {
var response = handler(ctx.Request.Url);
var response = handler(new RelativeUri(ctx.Request.Url.PathAndQuery));
EvaluateResponse(response, ctx.Response);
};
}
Expand Down
9 changes: 4 additions & 5 deletions QuartzNetWebConsole/Controllers/JobGroupController.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
using System;
using System.Collections.Specialized;
using System.Linq;
using System.Xml.Linq;
using Quartz;
using Quartz.Impl.Matchers;
using QuartzNetWebConsole.Utils;
using QuartzNetWebConsole.Views;

namespace QuartzNetWebConsole.Controllers {
public class JobGroupController {
public static Response Execute(Uri url, Func<ISchedulerWrapper> getScheduler) {
public static Response Execute(RelativeUri uri, Func<ISchedulerWrapper> getScheduler) {
var scheduler = getScheduler();
var querystring = url.ParseQueryString();
var querystring = QueryStringParser.ParseQueryString(uri.Query);

var group = querystring["group"];
var jobNames = scheduler.GetJobKeys(GroupMatcher<JobKey>.GroupEquals(group));
var runningJobs = scheduler.GetCurrentlyExecutingJobs();
Expand All @@ -23,7 +22,7 @@ public static Response Execute(Uri url, Func<ISchedulerWrapper> getScheduler) {
});
var paused = scheduler.IsJobGroupPaused(group);
var highlight = querystring["highlight"];
var view = Views.Views.JobGroup(group, paused, highlight, url.PathAndQuery, jobs);
var view = Views.Views.JobGroup(group, paused, highlight, uri.PathAndQuery, jobs);
return new Response.XDocumentResponse(Helpers.XHTML(view));
}
}
Expand Down
2 changes: 1 addition & 1 deletion QuartzNetWebConsole/Controllers/LogController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ static LogController() {
DefaultPageSize = 25;
}

public static Response Execute(Uri url) {
public static Response Execute(RelativeUri url) {
var thisUrl = url.PathAndQuery.Split('?')[0];
var qs = url.ParseQueryString();
var pageSize = GetPageSize(qs);
Expand Down
2 changes: 1 addition & 1 deletion QuartzNetWebConsole/Controllers/SchedulerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static MethodParameters GetMethodParameters(NameValueCollection qs) {
return new MethodParameters(method, redirect, parameters);
}

public static Response Execute(Uri url, Func<ISchedulerWrapper> getScheduler) {
public static Response Execute(RelativeUri url, Func<ISchedulerWrapper> getScheduler) {
var scheduler = getScheduler();
var p = GetMethodParameters(url.ParseQueryString());
p.method.Invoke(scheduler, p.parameters.ToArray());
Expand Down
3 changes: 1 addition & 2 deletions QuartzNetWebConsole/Controllers/StaticController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Specialized;
using System.IO;
using System.Reflection;
using QuartzNetWebConsole.Utils;
Expand All @@ -8,7 +7,7 @@ namespace QuartzNetWebConsole.Controllers {
public class StaticController {
private static readonly Assembly assembly = typeof(StaticController).Assembly;

public static Response Execute(Uri url) {
public static Response Execute(RelativeUri url) {
var querystring = url.ParseQueryString();
var resource = querystring["r"];
resource = string.Format("{0}.Resources.{1}", assembly.FullName.Split(',')[0], resource);
Expand Down
2 changes: 1 addition & 1 deletion QuartzNetWebConsole/Controllers/TriggerGroupController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ private static IEnumerable<TriggerWithState> GetTriggers(ISchedulerWrapper sched
});
}

public static Response Execute(Uri url, Func<ISchedulerWrapper> getScheduler) {
public static Response Execute(RelativeUri url, Func<ISchedulerWrapper> getScheduler) {
var scheduler = getScheduler();
var qs = url.ParseQueryString();
var highlight = qs["highlight"];
Expand Down
4 changes: 1 addition & 3 deletions QuartzNetWebConsole/Controllers/TriggersByJobController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
using QuartzNetWebConsole.Utils;
using QuartzNetWebConsole.Views;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Xml.Linq;

namespace QuartzNetWebConsole.Controllers {
public class TriggersByJobController {
Expand All @@ -19,7 +17,7 @@ private static IEnumerable<TriggerWithState> GetTriggers(ISchedulerWrapper sched
});
}

public static Response Execute(Uri url, Func<ISchedulerWrapper> getScheduler) {
public static Response Execute(RelativeUri url, Func<ISchedulerWrapper> getScheduler) {
var scheduler = getScheduler();
var querystring = url.ParseQueryString();
var highlight = querystring["highlight"];
Expand Down
1 change: 1 addition & 0 deletions QuartzNetWebConsole/QuartzNetWebConsole.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
<Compile Include="Utils\LimitedList.cs" />
<Compile Include="Utils\Owin.cs" />
<Compile Include="Utils\QueryStringParser.cs" />
<Compile Include="Utils\RelativeUri.cs" />
<Compile Include="Utils\Response.cs" />
<Compile Include="Utils\SchedulerWrapper.cs" />
</ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions QuartzNetWebConsole/Routing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using QuartzNetWebConsole.Controllers;
using QuartzNetWebConsole.Utils;

using Route = System.Collections.Generic.KeyValuePair<string, System.Func<System.Uri, QuartzNetWebConsole.Utils.Response>>;
using Route = System.Collections.Generic.KeyValuePair<string, System.Func<QuartzNetWebConsole.Utils.RelativeUri, QuartzNetWebConsole.Utils.Response>>;

namespace QuartzNetWebConsole {
public class Routing {
Expand All @@ -12,10 +12,10 @@ private static ISchedulerWrapper GetSchedulerWrapper() {
return new SchedulerWrapper(Setup.Scheduler());
}
public static readonly IEnumerable<Route> Routes =
new[] { Route("jobgroup", url => JobGroupController.Execute(url, GetSchedulerWrapper)), Route("index", ctx => IndexController.Execute(GetSchedulerWrapper)), Route("log", LogController.Execute), Route("scheduler", ctx => SchedulerController.Execute(ctx, GetSchedulerWrapper)), Route("static", StaticController.Execute), Route("triggerGroup", ctx => TriggerGroupController.Execute(ctx, GetSchedulerWrapper)), Route("triggersByJob", ctx => TriggersByJobController.Execute(ctx, GetSchedulerWrapper)),
new[] { Route("jobgroup", url => JobGroupController.Execute(url, GetSchedulerWrapper)), Route("index", _ => IndexController.Execute(GetSchedulerWrapper)), Route("log", LogController.Execute), Route("scheduler", ctx => SchedulerController.Execute(ctx, GetSchedulerWrapper)), Route("static", StaticController.Execute), Route("triggerGroup", ctx => TriggerGroupController.Execute(ctx, GetSchedulerWrapper)), Route("triggersByJob", ctx => TriggersByJobController.Execute(ctx, GetSchedulerWrapper)),
};

private static Route Route(string path, Func<Uri, Response> action) {
private static Route Route(string path, Func<RelativeUri, Response> action) {

return new Route(path, action);

Expand Down
11 changes: 7 additions & 4 deletions QuartzNetWebConsole/Setup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,17 @@ static Setup() {

public delegate Task AppFunc(IDictionary<string, object> env);

public static Func<AppFunc, AppFunc> Owin(Func<IScheduler> scheduler) {
public static Func<AppFunc, AppFunc> Owin(string basePath, Func<IScheduler> scheduler) {
Setup.Scheduler = scheduler;
return app => env => {
var uri = env.GetOwinUri();
var pathAndQuery = env.GetOwinRelativeUri();
if (!pathAndQuery.Path.StartsWith(basePath))
return app(env);
var response =
Routing.Routes
.Where(x => uri.AbsolutePath.Split('.')[0].EndsWith(x.Key, StringComparison.InvariantCultureIgnoreCase))
.Select(r => r.Value(uri).EvaluateResponse())
.Where(x => pathAndQuery.Path.Replace(basePath, "").Split('.')[0].EndsWith(x.Key, StringComparison.InvariantCultureIgnoreCase))
.Select(r => r.Value(pathAndQuery).EvaluateResponse())
.FirstOrDefault();
return response == null ? app(env) : response(env);
};
Expand Down
25 changes: 10 additions & 15 deletions QuartzNetWebConsole/Utils/Owin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,17 @@
using System.Threading.Tasks;

namespace QuartzNetWebConsole.Utils {
internal static class Owin {
public static Uri GetOwinUri(this IDictionary<string, object> env) {
var headers = (IDictionary<string, string[]>) env["owin.RequestHeaders"];
var scheme = (string) env["owin.RequestScheme"];
var hostAndPort = headers["Host"].First().Split(':');
var host = hostAndPort[0];
var port = hostAndPort.Length > 1 ? int.Parse(hostAndPort[1]) : (scheme == Uri.UriSchemeHttp ? 80 : 443);
var path = (string) env["owin.RequestPathBase"] + (string) env["owin.RequestPath"];
var query = (string) env["owin.RequestQueryString"];
public static class Owin {
public static string GetOwinRequestPath(this IDictionary<string, object> env) {
return (string)env["owin.RequestPath"];
}

var uriBuilder = new UriBuilder(scheme: scheme, host: host, portNumber: port) {
Path = path,
Query = query,
};
public static string GetOwinRequestQueryString(this IDictionary<string, object> env) {
return (string)env["owin.RequestQueryString"];
}

return uriBuilder.Uri;
public static RelativeUri GetOwinRelativeUri(this IDictionary<string, object> env) {
return new RelativeUri(env.GetOwinRequestPath() + "?" + env.GetOwinRequestQueryString());
}

public static Stream GetOwinResponseBody(this IDictionary<string, object> env) {
Expand Down Expand Up @@ -53,7 +48,7 @@ public static void SetOwinStatusCode(this IDictionary<string, object> env, int s
public static Setup.AppFunc EvaluateResponse(this Response response) {
return env => response.Match(
content: async x => {
env.SetOwinContentType(x.ContentType, encoding.BodyName);
env.SetOwinContentType(contentType: x.ContentType, charset: encoding.BodyName);
var content = Encoding.UTF8.GetBytes(x.Content);
env.SetOwinContentLength(content.Length);
await env.GetOwinResponseBody().WriteAsync(content, 0, content.Length);
Expand Down
10 changes: 7 additions & 3 deletions QuartzNetWebConsole/Utils/QueryStringParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@

namespace QuartzNetWebConsole.Utils {
public static class QueryStringParser {
public static NameValueCollection ParseQueryString(this Uri uri) {
if (string.IsNullOrEmpty(uri.Query) || uri.Query[0] != '?')
public static NameValueCollection ParseQueryString(string q) {
if (string.IsNullOrEmpty(q))
return new NameValueCollection();
var query = uri.Query.Substring(1).Split('&').Select(x => x.Split('=').Select(WebUtility.UrlDecode).ToArray());
var query = q.Split('&').Select(x => x.Split('=').Select(WebUtility.UrlDecode).ToArray());
var r = new NameValueCollection();
foreach (var kv in query) {
var value = kv.Length < 2 ? "" : kv[1];
r.Add(kv[0], value);
}
return r;
}

public static NameValueCollection ParseQueryString(this RelativeUri uri) {
return ParseQueryString(uri.Query);
}
}
}
28 changes: 28 additions & 0 deletions QuartzNetWebConsole/Utils/RelativeUri.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace QuartzNetWebConsole.Utils {
public sealed class RelativeUri {
public readonly string PathAndQuery;

public RelativeUri(string pathAndQuery) {
PathAndQuery = pathAndQuery;
}

public string Query {
get {
var parts = PathAndQuery.Split('?');
if (parts.Length < 2)
return "";
return parts[1];
}
}

public string Path {
get {
return PathAndQuery.Split('?')[0];
}
}

public override string ToString() {
return PathAndQuery;
}
}
}
2 changes: 1 addition & 1 deletion SampleApp.Owin/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ static void Start(IAppBuilder app) {
// A dummy calendar
//scheduler.AddCalendar("myCalendar", new DummyCalendar { Description = "dummy calendar" }, false, false);

app.Use(QuartzNetWebConsole.Setup.Owin(() => scheduler));
app.Use(QuartzNetWebConsole.Setup.Owin("/quartz", () => scheduler));
}

private static void Main(string[] args) {
Expand Down

0 comments on commit 0208b3e

Please sign in to comment.