Program.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using Microsoft.AspNetCore.Builder;
  2. using Sers.Core.Util.ConfigurationManager;
  3. using Sers.Core.Common.WebHost;
  4. using Sers.Core.Module.Log;
  5. using System;
  6. using Sers.ServiceStation;
  7. using System.IO;
  8. using Sers.Gateway;
  9. using Newtonsoft.Json.Linq;
  10. namespace Main
  11. {
  12. public class Program
  13. {
  14. public static void Main(string[] args)
  15. {
  16. try
  17. {
  18. #region (x.1)初始化ServiceStation
  19. ServiceStation.Init();
  20. //ServiceStation.Discovery(typeof(Program).Assembly);
  21. if (!ServiceStation.Start())
  22. {
  23. Logger.Info("无法连接服务中心。站点关闭...");
  24. return;
  25. }
  26. #endregion
  27. #region (x.2)初始化GatewayHelp
  28. var gatewayHelp = new GatewayHelp();
  29. #region (x.x.1)构建 Api Event BeforeCallApi
  30. var BeforeCallApi = Sers.Core.Station.Module.Api.ApiEvent.BeforeCallApi.EventBuilder.LoadEvent(ConfigurationManager.Instance.GetByPath<JArray>("Sers.Api.BeforeCallApi"));
  31. if (BeforeCallApi != null) gatewayHelp.BeforeCallApi += BeforeCallApi;
  32. #endregion
  33. //(x.x.2)从配置文件加载 服务限流配置
  34. gatewayHelp.rateLimitMng.LoadFromConfiguration();
  35. #endregion
  36. #region (x.3)初始化WebHost
  37. RunArg arg = new RunArg { allowAnyOrigin = true };
  38. arg.urls = ConfigurationManager.Instance.GetByPath<string[]>("Sers.Gateway.WebHost.urls");
  39. var wwwroot = ConfigurationManager.Instance.GetByPath<string>("Sers.Gateway.WebHost.wwwroot");
  40. if ("" == wwwroot)
  41. wwwroot = Path.Combine(AppContext.BaseDirectory, "wwwroot");
  42. if (Directory.Exists(wwwroot))
  43. {
  44. arg.wwwrootPath = wwwroot;
  45. }
  46. arg.OnConfigure = (app, env) =>
  47. {
  48. app.Run(gatewayHelp.Bridge);
  49. };
  50. arg.RunAsync = true;
  51. Logger.Info("[WebHost]will listening on: " + string.Join(',', arg.urls));
  52. Logger.Info("[WebHost]wwwroot : " + wwwroot);
  53. Sers.Core.Common.WebHost.Startup.Run(arg);
  54. #endregion
  55. ServiceStation.RunAwait();
  56. }
  57. catch (Exception ex)
  58. {
  59. Logger.Error(ex);
  60. Console.WriteLine("Exception:" + ex.Message);
  61. }
  62. }
  63. }
  64. }