public void RouterCanRouteSimpleResource() { var router = new Router(); bool routerDidRoute = false; router.Route(HttpMethods.Get, "/test", (req, res) => { routerDidRoute = true; }); // Need to mock httplistenercontext to be able to test for real. }
public static void Main(string[] args) { var router = new Router(); router.Route(HttpMethods.Get, "/entities", Callback); router.Route(HttpMethods.Get, "/entities/", Callback); router.Route(HttpMethods.Get, "/entities/{userId}", Callback); router.Route(HttpMethods.Get, "/entities/{userId}/", Callback); router.Route(HttpMethods.Get, "/entities/{userId}/names", Callback); router.Route(HttpMethods.Get, "/entities/{userId}/names/", Callback); router.Route(HttpMethods.Get, "/entities/{userId}/names/{nameId}", Callback); router.Route(HttpMethods.Get, "/entities/{userId}/names/{nameId}/", Callback); using (var httpServer = new HttpServer(router)) { Console.ReadLine(); } }
public HttpServer(Router router) { _cancelTokenSource = new CancellationTokenSource(); _server = new HttpListener(); _server.Prefixes.Add(@"http://localhost:5556/"); _server.Start(); _serverTask = Task.Run(async () => { while (!_cancelTokenSource.IsCancellationRequested) { var context = await Task.Run(() => _server.GetContext(), _cancelTokenSource.Token); var endpoint = context.Request.Url.LocalPath; router.HandleRequest(endpoint, context); } }, _cancelTokenSource.Token); }