Program.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using System;
  2. using System.Text.Json;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. using WatsonWebserver;
  6. using WatsonWebserver.Core;
  7. namespace Benchmarks
  8. {
  9. #region Supporting data structures
  10. public class JsonResult
  11. {
  12. public string Message { get; set; }
  13. }
  14. #endregion
  15. public static class Program
  16. {
  17. private static readonly ManualResetEvent WaitEvent = new(false);
  18. public static async Task<int> Main(string[] args)
  19. {
  20. #if DEBUG
  21. var host = "127.0.0.1";
  22. #else
  23. var host = "tfb-server";
  24. #endif
  25. var settings = new WebserverSettings(host, 8080, false);
  26. using var server = new Webserver(settings, DefaultRoute);
  27. server.Routes.PreAuthentication.Static.Add(HttpMethod.GET, "/plaintext", PlaintextRoute);
  28. server.Routes.PreAuthentication.Static.Add(HttpMethod.GET, "/json", JsonRoute);
  29. try
  30. {
  31. AppDomain.CurrentDomain.ProcessExit += (_, __) =>
  32. {
  33. WaitEvent.Set();
  34. };
  35. await server.StartAsync();
  36. WaitEvent.WaitOne();
  37. return 0;
  38. }
  39. catch (Exception e)
  40. {
  41. Console.WriteLine(e);
  42. return -1;
  43. }
  44. }
  45. static async Task DefaultRoute(HttpContextBase ctx)
  46. {
  47. ctx.Response.StatusCode = 404;
  48. await ctx.Response.Send("Not found.");
  49. }
  50. static async Task PlaintextRoute(HttpContextBase ctx)
  51. {
  52. ctx.Response.Headers.Add("Content-Type", "text/plain; charset=UTF-8");
  53. await ctx.Response.Send("Hello, World!");
  54. }
  55. static async Task JsonRoute(HttpContextBase ctx)
  56. {
  57. var response = new JsonResult() { Message = "Hello, World!" };
  58. var serialized = JsonSerializer.Serialize(response);
  59. ctx.Response.Headers.Add("Content-Type", "application/json; charset=UTF-8");
  60. await ctx.Response.Send(serialized);
  61. }
  62. }
  63. }