The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EndpointHelper.cs 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using Microsoft.AspNetCore.Builder;
  2. using Microsoft.AspNetCore.Hosting;
  3. using Microsoft.AspNetCore.Routing;
  4. using Microsoft.Extensions.Logging;
  5. using Newtonsoft.Json;
  6. using Newtonsoft.Json.Linq;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Dynamic;
  10. using System.IO;
  11. using System.Linq;
  12. namespace Teknik.Utilities.Routing
  13. {
  14. public static class EndpointHelper
  15. {
  16. public static void BuildEndpoints(this IEndpointRouteBuilder endpoints, string fullHost, string shortHost)
  17. {
  18. // Get the endpoint mappings
  19. var mappings = GetEndpointMappings();
  20. // Create a subdomain endpoint for each mapping
  21. foreach (var mapping in mappings)
  22. {
  23. var domains = GetDomainsFromHostTypes(fullHost, shortHost, mapping.HostTypes);
  24. var defaultObj = new ExpandoObject();
  25. if (mapping.Defaults != null)
  26. {
  27. var defaults = mapping.Defaults as JObject;
  28. foreach (var defaultVal in defaults)
  29. {
  30. defaultObj.TryAdd(defaultVal.Key, defaultVal.Value.ToObject<object>());
  31. }
  32. }
  33. defaultObj.TryAdd("area", mapping.Area);
  34. endpoints.MapSubdomainEndpoint(
  35. name: mapping.Name,
  36. domains: domains,
  37. subDomains: mapping.SubDomains,
  38. pattern: mapping.Pattern,
  39. area: mapping.Area,
  40. defaults: defaultObj,
  41. adjustPattern: mapping.AdjustPattern ?? true
  42. );
  43. }
  44. }
  45. public static EndpointMapping GetEndpointMapping(string name)
  46. {
  47. var mappings = GetEndpointMappings();
  48. return mappings.FirstOrDefault(m => m.Name == name);
  49. }
  50. public static List<EndpointMapping> GetEndpointMappings()
  51. {
  52. string dataDir = (string)AppDomain.CurrentDomain.GetData("DataDirectory");
  53. string file = Path.Combine(dataDir, Constants.ENDPOINT_MAPPING_PATH);
  54. if (File.Exists(file))
  55. {
  56. JsonSerializer serializer = new JsonSerializer();
  57. using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read))
  58. using (var sr = new StreamReader(fs))
  59. using (var jsonTextReader = new JsonTextReader(sr))
  60. {
  61. return (List<EndpointMapping>)serializer.Deserialize(jsonTextReader, typeof(List<EndpointMapping>));
  62. }
  63. }
  64. return new List<EndpointMapping>();
  65. }
  66. private static List<string> GetDomainsFromHostTypes(string fullHost, string shortHost, List<HostType> hostTypes)
  67. {
  68. var domains = new List<string>();
  69. if (hostTypes != null)
  70. {
  71. foreach (var hostType in hostTypes)
  72. {
  73. switch (hostType)
  74. {
  75. case HostType.Full:
  76. if (!string.IsNullOrEmpty(fullHost) &&
  77. !domains.Contains(fullHost))
  78. domains.Add(fullHost);
  79. break;
  80. case HostType.Short:
  81. if (!string.IsNullOrEmpty(shortHost) &&
  82. !domains.Contains(shortHost))
  83. domains.Add(shortHost);
  84. break;
  85. }
  86. }
  87. }
  88. return domains;
  89. }
  90. }
  91. }