|
|
|
@ -1,6 +1,8 @@
|
|
|
|
|
using Application.Models;
|
|
|
|
|
using Infrastructure.Extensions;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
using System;
|
|
|
|
@ -12,37 +14,38 @@ namespace IoT.Shared.Controllers
|
|
|
|
|
{
|
|
|
|
|
public class BaseDeviceController : Controller
|
|
|
|
|
{
|
|
|
|
|
private readonly IHttpClientFactory _httpClientFactory;
|
|
|
|
|
protected readonly IServiceProvider applicationService;
|
|
|
|
|
|
|
|
|
|
public BaseDeviceController(IHttpClientFactory httpClientFactory)
|
|
|
|
|
public BaseDeviceController(IServiceProvider applicationServices)
|
|
|
|
|
{
|
|
|
|
|
this._httpClientFactory = httpClientFactory;
|
|
|
|
|
this.applicationService = applicationServices;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[ApiExplorerSettings(IgnoreApi = true)]
|
|
|
|
|
public string GetApiJson()
|
|
|
|
|
{
|
|
|
|
|
var url = new UriBuilder
|
|
|
|
|
{
|
|
|
|
|
Host = Request.Host.Host,
|
|
|
|
|
Port = Request.Host.Port ?? 80,
|
|
|
|
|
Path = "/swagger/v1/swagger.json"
|
|
|
|
|
}.ToString();
|
|
|
|
|
var prefix = $"/{RouteData.Values["controller"]}/";
|
|
|
|
|
var hc = this._httpClientFactory.CreateClient();
|
|
|
|
|
var result = hc.GetStringAsync(url).Result;
|
|
|
|
|
var json = JsonConvert.DeserializeObject(result) as JObject;
|
|
|
|
|
var paths = json.Properties().FirstOrDefault(o => o.Name == "paths").Value as JObject;
|
|
|
|
|
var names = paths.Properties().Select(o => o.Name).ToList();
|
|
|
|
|
foreach (var item in names)
|
|
|
|
|
using (var scope = this.applicationService.CreateScope())
|
|
|
|
|
{
|
|
|
|
|
if (!item.StartsWith(prefix))
|
|
|
|
|
var serviceProvider = scope.ServiceProvider;
|
|
|
|
|
var cfg = serviceProvider.GetService<IConfiguration>();
|
|
|
|
|
var port = cfg["server.urls"].Split(':')[2];
|
|
|
|
|
var url = $"http://localhost:{port}/swagger/v1/swagger.json";
|
|
|
|
|
var hc = serviceProvider.GetService<IHttpClientFactory>().CreateClient();
|
|
|
|
|
var result = hc.GetStringAsync(url).Result;
|
|
|
|
|
var json = JsonConvert.DeserializeObject(result) as JObject;
|
|
|
|
|
var paths = json.Properties().FirstOrDefault(o => o.Name == "paths").Value as JObject;
|
|
|
|
|
var names = paths.Properties().Select(o => o.Name).ToList();
|
|
|
|
|
foreach (var item in names)
|
|
|
|
|
{
|
|
|
|
|
paths.Remove(item);
|
|
|
|
|
if (!item.StartsWith(prefix))
|
|
|
|
|
{
|
|
|
|
|
paths.Remove(item);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
var realResult = JsonConvert.SerializeObject(json);
|
|
|
|
|
return realResult;
|
|
|
|
|
}
|
|
|
|
|
var realResult = JsonConvert.SerializeObject(json);
|
|
|
|
|
return realResult;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ApiResponse AsyncAction(Action action)
|
|
|
|
|