Browse Source

Using event process request

Henry 6 years ago
parent
commit
b9af522c40
1 changed files with 45 additions and 37 deletions
  1. 45 37
      frameworks/CSharp/beetlex/Benchmarks/Program.cs

+ 45 - 37
frameworks/CSharp/beetlex/Benchmarks/Program.cs

@@ -13,13 +13,8 @@ namespace Benchmarks
     [Controller]
     [Controller]
     class Program
     class Program
     {
     {
-        private static readonly byte[] _helloWorldPayload = Encoding.UTF8.GetBytes("Hello, World!");
-
-        private static StringBytes plaintextResult;
-
         public static void Main(string[] args)
         public static void Main(string[] args)
         {
         {
-            plaintextResult = new StringBytes(_helloWorldPayload);
             var builder = new HostBuilder()
             var builder = new HostBuilder()
                 .ConfigureServices((hostContext, services) =>
                 .ConfigureServices((hostContext, services) =>
                 {
                 {
@@ -27,47 +22,39 @@ namespace Benchmarks
                 });
                 });
             builder.Build().Run();
             builder.Build().Run();
         }
         }
-
-        public object plaintext(IHttpContext context)
-        {
-            return plaintextResult;
-        }
-
-        public object json(IHttpContext context)
-        {
-            return new SpanJsonResult(new JsonMessage { message = "Hello, World!" });
-        }
-        public class JsonMessage
-        {
-            public string message { get; set; }
-        }
     }
     }
 
 
-    public class SpanJsonResult : ResultBase
+
+    public class BeetleXHttpServer : IHostedService
     {
     {
-        public SpanJsonResult(object data)
-        {
-            Data = data;
-        }
 
 
-        public object Data { get; set; }
+        private static readonly byte[] _helloWorldPayload = Encoding.UTF8.GetBytes("Hello, World!");
 
 
-        public override IHeaderItem ContentType => ContentTypes.JSON;
+        public static StringBytes plaintextResult;
 
 
-        public override bool HasBody => true;
+        private HttpApiServer mApiServer;
 
 
-        public override void Write(PipeStream stream, HttpResponse response)
+        public void OnRequesting(object sender, EventHttpRequestArgs e)
         {
         {
-            JsonSerializer.NonGeneric.Utf8.SerializeAsync(Data, stream);
+            if (e.Request.BaseUrl == "/plaintext")
+            {
+                e.Response.Result(plaintextResult);
+            }
+            else if (e.Request.BaseUrl == "/json")
+            {
+                var json = new SpanJsonResult(new JsonMessage { message = "Hello, World!" });
+                e.Response.Result(json);
+            }
+            else
+            {
+                e.Response.Result(new NotFoundResult("url not found!"));
+            }
+            e.Cancel = true;
         }
         }
-    }
-
-    public class BeetleXHttpServer : IHostedService
-    {
-        private HttpApiServer mApiServer;
 
 
         public virtual Task StartAsync(CancellationToken cancellationToken)
         public virtual Task StartAsync(CancellationToken cancellationToken)
         {
         {
+            plaintextResult = new StringBytes(_helloWorldPayload);
             mApiServer = new HttpApiServer();
             mApiServer = new HttpApiServer();
             mApiServer.Register(typeof(Program).Assembly);
             mApiServer.Register(typeof(Program).Assembly);
             mApiServer.Options.Port = 8080;
             mApiServer.Options.Port = 8080;
@@ -77,10 +64,8 @@ namespace Benchmarks
             mApiServer.Options.UrlIgnoreCase = false;
             mApiServer.Options.UrlIgnoreCase = false;
             mApiServer.Options.LogLevel = BeetleX.EventArgs.LogType.Off;
             mApiServer.Options.LogLevel = BeetleX.EventArgs.LogType.Off;
             mApiServer.Options.LogToConsole = true;
             mApiServer.Options.LogToConsole = true;
+            mApiServer.HttpRequesting += OnRequesting;
             mApiServer.Open();
             mApiServer.Open();
-            Console.WriteLine("BeetleX FastHttpApi server");
-            Console.WriteLine($"ServerGC:{System.Runtime.GCSettings.IsServerGC}");
-            Console.Write(mApiServer.BaseServer);
             return Task.CompletedTask;
             return Task.CompletedTask;
         }
         }
 
 
@@ -90,4 +75,27 @@ namespace Benchmarks
             return Task.CompletedTask;
             return Task.CompletedTask;
         }
         }
     }
     }
+    public class JsonMessage
+    {
+        public string message { get; set; }
+    }
+
+    public class SpanJsonResult : ResultBase
+    {
+        public SpanJsonResult(object data)
+        {
+            Data = data;
+        }
+
+        public object Data { get; set; }
+
+        public override IHeaderItem ContentType => ContentTypes.JSON;
+
+        public override bool HasBody => true;
+
+        public override void Write(PipeStream stream, HttpResponse response)
+        {
+            JsonSerializer.NonGeneric.Utf8.SerializeAsync(Data, stream);
+        }
+    }
 }
 }