示例#1
0
        private static void Main(string[] args)
        {
            var options = (from arg in args
                           where arg.StartsWith("--")
                           let pair = arg.Substring(2).Split(new[] { '=' }, StringSplitOptions.RemoveEmptyEntries)
                                      where pair.Length == 2
                                      let key = pair[0].Trim()
                                                let val = pair[1].Trim()
                                                          select new KeyValuePair <string, string>(key, val))
                          .ToDictionary(x => x.Key, x => x.Value, StringComparer.InvariantCultureIgnoreCase);

            var app = new ExpressApplication();

            Func <double, double> square = x => x * x;

            app.Get("", req => req.Text("Hi!"))
            .Get(
                "text/{x}/{y}",
                req => req.Text(
                    string.Format("x={0}, y={1}",
                                  req.RouteData.Values["x"],
                                  req.RouteData.Values["y"]))
                )
            .Get(
                "json/{x}/{y}",
                req => req.Json(
                    new
            {
                x = req.RouteData.Values["x"],
                y = req.RouteData.Values["y"]
            })
                )
            .Get("math/square/{x}", square)
            .WebService <MathService>("math.svc");

            var port = options.Get("port", 1111);
            var mode = options.Get("mode", "tcp");

            var settings = new HttpServerSettings {
                Port = port
            };

            switch (mode.ToLowerInvariant())
            {
            case "http":
                settings.Mode = HttpServerMode.HttpListener;
                break;

            default:
                settings.Mode = HttpServerMode.TcpListener;
                break;
            }

            using (new HttpServer(app, settings))
            {
                Console.WriteLine("Listening port {0}. Press enter to stop the server.", port);
                Console.ReadLine();
            }
        }
        public void Get(string path)
        {
            var app = new ExpressApplication();

            app.Get<string, string>("{name}", name => name);

            using (new HttpServer(app, _settings))
            using (var client = _createClient())
            {
                var json = client.DownloadString(path);
                var serializer = new JavaScriptSerializer();
                var d = serializer.DeserializeObject(json) as IDictionary<string,object>;
                Assert.AreEqual(path, d["d"]);
            }
        }
        public void Get(string path)
        {
            var app = new ExpressApplication();

            app.Get <string, string>("{name}", name => name);

            using (new HttpServer(app, _settings))
                using (var client = _createClient())
                {
                    var json       = client.DownloadString(path);
                    var serializer = new JavaScriptSerializer();
                    var d          = serializer.DeserializeObject(json) as IDictionary <string, object>;
                    Assert.AreEqual(path, d["d"]);
                }
        }
        public void GetText()
        {
            var app = new ExpressApplication();
            app.Get("test", req => req.Text("test"));

            var request = new Mock<HttpRequestBaseImpl> {CallBase = true};
            request.Setup(x => x.Url).Returns(new Uri("http://localhost/test"));

            var response = new Mock<HttpResponseBase>();

            var ctx = new Mock<HttpContextBase>();
            ctx.Setup(x => x.Request).Returns(request.Object);
            ctx.Setup(x => x.Response).Returns(response.Object);

            Assert.IsTrue(app.Process(ctx.Object));
        }
        public void GetText()
        {
            var app = new ExpressApplication();

            app.Get("test", req => req.Text("test"));

            var request = new Mock <HttpRequestBaseImpl> {
                CallBase = true
            };

            request.Setup(x => x.Url).Returns(new Uri("http://localhost/test"));

            var response = new Mock <HttpResponseBase>();

            var ctx = new Mock <HttpContextBase>();

            ctx.Setup(x => x.Request).Returns(request.Object);
            ctx.Setup(x => x.Response).Returns(response.Object);

            Assert.IsTrue(app.Process(ctx.Object));
        }
示例#6
0
文件: Server.cs 项目: puslmbps/dsharp
    static Server()
    {
        string dbUrl = Node.Process.Arguments[2];

        if (String.IsNullOrEmpty(dbUrl))
        {
            Debug.WriteLine("Usage: node cellar.server.js <database url>");
            Debug.WriteLine("Format of database url:");
            Debug.WriteLine("  mongodb://user:password@host:port/database");
            Debug.WriteLine(String.Empty);
            return;
        }

        CellarApplication cellarApplication = new CellarApplication();

        cellarApplication.Initialize(dbUrl, delegate(Exception initializationError) {
            if (initializationError != null)
            {
                Debug.WriteLine(initializationError.ToString());
                return;
            }

            Debug.WriteLine("Starting web application on port 8080...");
            string path = (string)Script.Literal("__dirname");

            ExpressApplication app = Express.Application();
            app.Use(Express.Static(path + "\\Content"));
            app.Get("/wines/:id", delegate(ExpressServerRequest request, ExpressServerResponse response) {
                WinesController controller = cellarApplication.CreateWinesController();
                controller.LookupWine(request, response);
            })
            .Get("/wines", delegate(ExpressServerRequest request, ExpressServerResponse response) {
                WinesController controller = cellarApplication.CreateWinesController();
                controller.QueryWines(request, response);
            });

            app.Listen(8080);
        });
    }