Program.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using BeetleX.FastHttpApi;
  2. using Microsoft.Extensions.Hosting;
  3. using System.Threading.Tasks;
  4. using Microsoft.Extensions.DependencyInjection;
  5. using System;
  6. using System.Threading;
  7. using System.Text;
  8. namespace Benchmarks
  9. {
  10. [BeetleX.FastHttpApi.Controller]
  11. class Program
  12. {
  13. private static readonly byte[] _helloWorldPayload = Encoding.UTF8.GetBytes("Hello, World!");
  14. private static StringBytes plaintextResult;
  15. public static void Main(string[] args)
  16. {
  17. plaintextResult = new StringBytes(_helloWorldPayload);
  18. var builder = new HostBuilder()
  19. .ConfigureServices((hostContext, services) =>
  20. {
  21. services.AddHostedService<BeetleXHttpServer>();
  22. });
  23. builder.Build().Run();
  24. }
  25. public object plaintext(HttpResponse response)
  26. {
  27. response.Header[HeaderTypeFactory.DATE] = DateTime.Now.ToString("r");
  28. return plaintextResult;
  29. }
  30. public object json(HttpResponse response)
  31. {
  32. response.Header[HeaderTypeFactory.DATE] = DateTime.Now.ToString("r");
  33. return new JsonResult(new JsonMessage { message = "Hello, World!" });
  34. }
  35. public class JsonMessage
  36. {
  37. public string message { get; set; }
  38. }
  39. }
  40. public class BeetleXHttpServer : IHostedService
  41. {
  42. private HttpApiServer mApiServer;
  43. public virtual Task StartAsync(CancellationToken cancellationToken)
  44. {
  45. mApiServer = new HttpApiServer();
  46. mApiServer.Register(typeof(Program).Assembly);
  47. mApiServer.Options.Port = 8080;
  48. mApiServer.Options.BufferPoolMaxMemory = 500;
  49. mApiServer.Options.MaxConnections = 100000;
  50. mApiServer.Options.UrlIgnoreCase = false;
  51. mApiServer.Options.LogLevel = BeetleX.EventArgs.LogType.Warring;
  52. mApiServer.Options.LogToConsole = true;
  53. mApiServer.Options.Statistical = false;
  54. mApiServer.Open();
  55. Console.WriteLine("BeetleX FastHttpApi server");
  56. Console.WriteLine($"ServerGC:{System.Runtime.GCSettings.IsServerGC}");
  57. Console.Write(mApiServer.BaseServer);
  58. return Task.CompletedTask;
  59. }
  60. public virtual Task StopAsync(CancellationToken cancellationToken)
  61. {
  62. mApiServer.BaseServer.Dispose();
  63. return Task.CompletedTask;
  64. }
  65. }
  66. }