Program.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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.ServerConfig.Port = 8080;
  48. mApiServer.ServerConfig.MaxConnections = 100000;
  49. mApiServer.ServerConfig.UrlIgnoreCase = false;
  50. mApiServer.ServerConfig.LogLevel = BeetleX.EventArgs.LogType.Warring;
  51. mApiServer.ServerConfig.LogToConsole = true;
  52. mApiServer.Open();
  53. Console.WriteLine("BeetleX FastHttpApi server");
  54. Console.WriteLine($"ServerGC:{System.Runtime.GCSettings.IsServerGC}");
  55. Console.Write(mApiServer.BaseServer);
  56. return Task.CompletedTask;
  57. }
  58. public virtual Task StopAsync(CancellationToken cancellationToken)
  59. {
  60. mApiServer.BaseServer.Dispose();
  61. return Task.CompletedTask;
  62. }
  63. }
  64. }