Exemple #1
0
        public TestModule()
        {
            Get("/", x => { return("Hello, World!"); });
            Get("/hello", x => { Print("Hello, World!"); return("Hello, World!"); });
            Get("/redirect", _ => this.Response.AsRedirect("~/text"));
            Get("/text", x => { return("Text = " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fffffff")); });
            Get("/time", x => { return("Time = " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fffffff")); });
            Get("/user/{name}", parameters => { return((string)parameters.name); });

            Post("/post", x => { return("POST = " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fffffff")); });
            Post("/post-something", x =>
            {
                var body = new StreamReader(this.Request.Body).ReadToEnd();
                return(body);
            });

            Get("/json", x =>
            {
                var model = new TestModel()
                {
                    This = new TestModel()
                };
                return(this.Response.AsJson(model));
            });

            Get("/xml", x =>
            {
                var model = new TestModel()
                {
                    This = new TestModel()
                };
                return(this.Response.AsXml(model));
            });

            Get("/html", x =>
            {
                string html =
                    @"
                    <html>
                    <head>
                      <title>Hi there</title>
                    </head>
                    <body>
                        This is a page, a simple page.
                    </body>
                    </html>
                    ";
                return(this.Response.AsHtml(html));
            });

            Get("/thread", x =>
            {
                Print("Thread[{0}]", Thread.CurrentThread.GetDescription());

                return(DateTime.Now.ToString(@"yyyy-MM-dd HH:mm:ss.fffffff"));
            });
            Get("/sleep", x =>
            {
                Print("Sleep starts Thread[{0}].",
                      Thread.CurrentThread.GetDescription());

                Thread.Sleep(TimeSpan.FromSeconds(8));

                Print("--------> Sleep ends Thread[{0}].",
                      Thread.CurrentThread.GetDescription());

                return(DateTime.Now.ToString(@"yyyy-MM-dd HH:mm:ss.fffffff"));
            });
            Get("/delay", x =>
            {
                Print("Delay starts Thread[{0}].",
                      Thread.CurrentThread.GetDescription());

                Task.Delay(TimeSpan.FromSeconds(1)).Wait();

                Print("--------> Delay ends Thread[{0}].",
                      Thread.CurrentThread.GetDescription());

                return(DateTime.Now.ToString(@"yyyy-MM-dd HH:mm:ss.fffffff"));
            });
            Get("/exception", x =>
            {
                throw new InvalidOperationException("I want to throw an exception.");
            });
            Get("/while", x =>
            {
                while (true)
                {
                }
            });
        }
Exemple #2
0
        public TestModule()
        {
            Get["/"] = x =>
            {
                return "Hello, World!";
            };

            Get["/redirect"] = _ => this.Response.AsRedirect("~/text");

            Get["/text"] = x =>
            {
                return "text";
            };
            Get["/time"] = x =>
            {
                return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fffffff");
            };

            Post["/post"] = x =>
            {
                return "";
            };

            Post["/post-something"] = x =>
            {
                var body = new StreamReader(this.Request.Body).ReadToEnd();
                return body;
            };

            Get["/json"] = x =>
            {
                var model = new TestModel() { This = new TestModel() };
                return this.Response.AsJson(model);
            };

            Get["/xml"] = x =>
            {
                var model = new TestModel() { This = new TestModel() };
                return this.Response.AsXml(model);
            };

            Get["/user/{name}"] = parameters =>
            {
                return (string)parameters.name;
            };

            Get["/html"] = x =>
            {
                string html =
                    @"
                    <html>
                    <head>
                      <title>Hi there</title>
                    </head>
                    <body>
                        This is a page, a simple page.
                    </body>
                    </html>
                    ";
                return this.Response.AsHtml(html);
            };


            Get["/delay"] = x =>
            {
                Console.WriteLine("[{1}] Delay starts Thread[{0}].",
                    Thread.CurrentThread.ManagedThreadId,
                    DateTime.Now.ToString(@"yyyy-MM-dd HH:mm:ss.fffffff"));
                Thread.Sleep(TimeSpan.FromSeconds(3));
                Console.WriteLine("[{1}] Delay ends Thread[{0}].",
                    Thread.CurrentThread.ManagedThreadId,
                    DateTime.Now.ToString(@"yyyy-MM-dd HH:mm:ss.fffffff"));
                return DateTime.Now.ToString(@"yyyy-MM-dd HH:mm:ss.fffffff");
            };
        }
Exemple #3
0
        public SimpleModule()
        {
            Get("/", x => { return("Hello, World!"); });
            Get("/ping", x => { return("pong"); });
            Get("/hello", x => { return("Hello, World!"); });
            Get("/text", x => { return("Text = " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fffffff")); });
            Get("/time", x => { return("Time = " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fffffff")); });

            Get("/print", _ => { Print("Hello, World!"); return("Hello, World!"); });
            Get("/user/{name}", parameters => { return((string)parameters.name); });
            Get("/redirect", _ => this.Response.AsRedirect("~/text"));

            Post("/post", _ => { return("POST = " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fffffff")); });
            Post("/post-something", _ =>
            {
                var body = new StreamReader(this.Request.Body).ReadToEnd();
                return(body);
            });

            Get("/json", x =>
            {
                var model = new TestModel()
                {
                    This = new TestModel()
                };
                return(this.Response.AsJson(model));
            });

            Get("/xml", x =>
            {
                var model = new TestModel()
                {
                    This = new TestModel()
                };
                return(this.Response.AsXml(model));
            });

            Get("/html", x =>
            {
                string html =
                    @"
                    <html>
                    <head>
                      <title>Hi there</title>
                    </head>
                    <body>
                        This is a page, a simple page.
                    </body>
                    </html>
                    ";
                return(this.Response.AsHtml(html));
            });

            // ---------------------- metrics ----------------------
            //this.MetricForRequestTimeAndResponseSize(typeof(TestModule).Name, "Get", "/");
            //this.MetricForRequestTimeAndResponseSize(typeof(TestModule).Name, "Get", "/ping");
            //this.MetricForRequestTimeAndResponseSize(typeof(TestModule).Name, "Get", "/hello");
            //this.MetricForRequestTimeAndResponseSize(typeof(TestModule).Name, "Get", "/text");
            //this.MetricForRequestTimeAndResponseSize(typeof(TestModule).Name, "Get", "/time");
            //this.MetricForRequestTimeAndResponseSize(typeof(TestModule).Name, "Get", "/user/{name}");
            this.MetricForAllRequests();
        }