Program.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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(IHttpContext context)
  26. {
  27. context.Response.Header[HeaderTypeFactory.DATE] = DateTime.Now.ToUniversalTime().ToString("r");
  28. return plaintextResult;
  29. }
  30. public object json(IHttpContext context)
  31. {
  32. context.Response.Header[HeaderTypeFactory.DATE] = DateTime.Now.ToUniversalTime().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.Open();
  54. Console.WriteLine("BeetleX FastHttpApi server");
  55. Console.WriteLine($"ServerGC:{System.Runtime.GCSettings.IsServerGC}");
  56. Console.Write(mApiServer.BaseServer);
  57. return Task.CompletedTask;
  58. }
  59. public virtual Task StopAsync(CancellationToken cancellationToken)
  60. {
  61. mApiServer.BaseServer.Dispose();
  62. return Task.CompletedTask;
  63. }
  64. }
  65. }