Program.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. namespace Benchmarks
  8. {
  9. [BeetleX.FastHttpApi.Controller]
  10. class Program
  11. {
  12. public static void Main(string[] args)
  13. {
  14. var builder = new HostBuilder()
  15. .ConfigureServices((hostContext, services) =>
  16. {
  17. services.AddHostedService<BeetleXHttpServer>();
  18. });
  19. builder.Build().Run();
  20. }
  21. public object plaintext(IHttpContext context)
  22. {
  23. context.Response.Header[HeaderTypeFactory.DATE] = DateTime.Now.ToUniversalTime().ToString("r");
  24. return new TextResult("Hello, World!");
  25. }
  26. public object json(IHttpContext context)
  27. {
  28. context.Response.Header[HeaderTypeFactory.DATE] = DateTime.Now.ToUniversalTime().ToString("r");
  29. return new JsonResult(new JsonMessage { message = "Hello, World!" });
  30. }
  31. public class JsonMessage
  32. {
  33. public string message { get; set; }
  34. }
  35. }
  36. public class BeetleXHttpServer : IHostedService
  37. {
  38. private HttpApiServer mApiServer;
  39. public virtual Task StartAsync(CancellationToken cancellationToken)
  40. {
  41. mApiServer = new HttpApiServer();
  42. mApiServer.Register(typeof(Program).Assembly);
  43. mApiServer.ServerConfig.Port = 8080;
  44. mApiServer.ServerConfig.MaxConnections = 100000;
  45. mApiServer.ServerConfig.UrlIgnoreCase = false;
  46. mApiServer.ServerConfig.LogLevel = BeetleX.EventArgs.LogType.Warring;
  47. mApiServer.ServerConfig.LogToConsole = true;
  48. mApiServer.Open();
  49. Console.WriteLine("BeetleX FastHttpApi server");
  50. Console.WriteLine($"ServerGC:{System.Runtime.GCSettings.IsServerGC}");
  51. Console.Write(mApiServer.BaseServer);
  52. return Task.CompletedTask;
  53. }
  54. public virtual Task StopAsync(CancellationToken cancellationToken)
  55. {
  56. mApiServer.BaseServer.Dispose();
  57. return Task.CompletedTask;
  58. }
  59. }
  60. }