[459] | 1 | using System.Collections.Generic;
|
---|
| 2 | using System.IO;
|
---|
| 3 | using System.Reflection;
|
---|
| 4 | using System.Text;
|
---|
| 5 |
|
---|
| 6 | namespace Webserver.WebAPI {
|
---|
| 7 | public class OpenApiHelpers {
|
---|
| 8 | private const string masterResourceName = "openapi.master.yaml";
|
---|
| 9 | private const string masterDocName = "openapi.yaml";
|
---|
| 10 |
|
---|
| 11 | private const string masterDocRefLineBegin = " - $ref: ";
|
---|
| 12 | private const string masterDocRefLineEnd = "#/paths";
|
---|
| 13 |
|
---|
| 14 | private readonly Dictionary<string, string> specs = new CaseInsensitiveStringDictionary<string> ();
|
---|
| 15 |
|
---|
| 16 | public OpenApiHelpers () {
|
---|
| 17 | loadMainSpec ();
|
---|
| 18 | Web.ServerInitialized += _ => {
|
---|
| 19 | buildMainSpecRefs ();
|
---|
| 20 | };
|
---|
| 21 | }
|
---|
| 22 |
|
---|
| 23 | private void loadMainSpec () {
|
---|
| 24 | Assembly apiAssembly = GetType ().Assembly;
|
---|
| 25 |
|
---|
| 26 | string specText = ResourceHelpers.GetManifestResourceText (apiAssembly, masterResourceName, true);
|
---|
| 27 | if (specText == null) {
|
---|
| 28 | Log.Warning ($"[Web] Failed loading main OpenAPI spec from assembly '{Path.GetFileName (apiAssembly.Location)}'");
|
---|
| 29 | return;
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | specs.Add (masterDocName, specText);
|
---|
| 33 | Log.Out ($"[Web] Loaded main OpenAPI spec");
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | private void buildMainSpecRefs () {
|
---|
| 37 | if (!TryGetOpenApiSpec (null, out string mainSpec)) {
|
---|
| 38 | return;
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | StringBuilder sb = new StringBuilder (mainSpec);
|
---|
| 42 |
|
---|
| 43 | foreach ((string apiSpecName, _) in specs) {
|
---|
| 44 | if (apiSpecName.Equals (masterDocName)) {
|
---|
| 45 | continue;
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | sb.AppendLine ($"{masterDocRefLineBegin}./{apiSpecName}{masterDocRefLineEnd}");
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | specs[masterDocName] = sb.ToString ();
|
---|
| 52 |
|
---|
| 53 | Log.Out ("[Web] OpenAPI preparation done");
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | public void LoadOpenApiSpec (AbsWebAPI _api) {
|
---|
| 57 | Assembly apiAssembly = _api.GetType ().Assembly;
|
---|
| 58 | string apiName = _api.Name;
|
---|
| 59 | string apiSpecName = $"{apiName}.openapi.yaml";
|
---|
| 60 |
|
---|
| 61 | string specText = ResourceHelpers.GetManifestResourceText (apiAssembly, apiSpecName, true);
|
---|
| 62 | if (specText == null) {
|
---|
| 63 | return;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | Log.Out ($"[Web] Loaded OpenAPI spec for '{apiName}'");
|
---|
| 67 | specs.Add (apiSpecName, specText);
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | public bool TryGetOpenApiSpec (string _name, out string _specText) {
|
---|
| 71 | if (string.IsNullOrEmpty (_name)) {
|
---|
| 72 | _name = masterDocName;
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | return specs.TryGetValue (_name, out _specText);
|
---|
| 76 | }
|
---|
| 77 | }
|
---|
| 78 | }
|
---|