Pārlūkot izejas kodu

添加系统接口(gover):移除离线接口

lith 4 gadi atpakaļ
vecāks
revīzija
012c852bf3

+ 23 - 0
dotnet/ServiceCenter/App.Gover.Gateway/wwwroot/_gover_/ApiDoc.html

@@ -44,6 +44,11 @@
         <a href="#" onclick="openPostmanWeb(true)">打开postmanWeb并加载当前api</a>
         <br />
         markdown文档:<a id="mdDownload" href="#" title="download markdown document File">下载markdown文档</a>
+        <br />
+
+        操作:
+        <a href="#" onclick="removeOfflineApiService()">移除离线接口</a> 
+
     </div>
 
     <div id="test-editormd-view" style="width: 95%;">
@@ -111,6 +116,24 @@
         $('#mdDownload').click(function () {
             sers.SaveFile(mdDoc, 'SersApiDoc(' + new Date().toJSON().slice(0, 10) + ').md');
         });
+
+
+
+
+        function removeOfflineApiService() {
+
+            if (!confirm('移除离线接口,移除后不可恢复,确定继续移除吗?')) return;
+
+            sers.apiClient.apiDesc_removeOffline(function (apiRet) {
+                if (!apiRet.success) {
+                    alert('操作失败,请重试。' + apiRet.error?.errorMessage);
+                    return;
+                }
+                location.reload();
+            });
+        }
+
+
     </script>
 
 </body>

+ 3 - 0
dotnet/ServiceCenter/App.Gover.Gateway/wwwroot/_gover_/Scripts/Sers/sers.apiClient.js

@@ -105,6 +105,9 @@
         obj.ajax({ api: '/_gover_/apiDesc/getAll', type: 'GET', data: arg, onSuc: onSuc }); 
     };
 
+    obj.apiDesc_removeOffline = function (onSuc) {
+        obj.ajax({ api: '/_gover_/apiDesc/removeOffline', type: 'GET', onSuc: onSuc });
+    };
 
 
     obj.serviceStation_getAll = function (onSuc) {

+ 1 - 1
dotnet/ServiceCenter/App.Gover.Gateway/wwwroot/_gover_/index.html

@@ -25,7 +25,7 @@
 <body>
     login: <a href="index.html?user=admin_123456">login</a> <br />
     1.ServiceCenterHealth:<a target="_blank" href="ServiceCenterHealth.html">ServiceCenterHealth</a> <br />
-    2.ApiDoc: <a target="_blank" href="ActiveApiDoc.html">ActiveApiDoc</a> <a target="_blank" href="ApiDoc.html">ApiDoc</a> <br />
+    2.ApiDoc: <a target="_blank" href="ActiveApiDoc.html?r=_gover_">ActiveApiDoc</a> <a target="_blank" href="ApiDoc.html?r=_gover_">ApiDoc</a> <br />
 
     3.ApiStationMng: <a target="_blank" href="ApiStationMng.html">ApiStationMng</a>  <br />
     4.ServiceStationMng:<a target="_blank" href="ServiceStationMng.html">ServiceStationMng</a> <br />

+ 59 - 6
dotnet/ServiceCenter/Sers.ServiceCenter/Sers.Gover/Base/ApiStationMng.cs

@@ -19,6 +19,7 @@ namespace Sers.Gover.Base
     {
         [JsonProperty]
         ConcurrentDictionary<string, ApiStationData> apiStations;
+
         GoverApiCenterService goverManage;
         public ApiStationMng()
         {            
@@ -111,12 +112,6 @@ namespace Sers.Gover.Base
             }
             return true;
         }
-        
-
-
-
-
-
 
         ApiStationData ApiStation_Get(string route)
         {
@@ -147,6 +142,60 @@ namespace Sers.Gover.Base
 
 
 
+        #region ApiService
+        /// <summary>
+        /// 移除离线的ApiService
+        /// </summary>
+        public void ApiService_RemoveOffline()
+        {
+            List<ApiStationData> changedApiStationList;
+ 
+            lock (this)
+            {
+                var apiServiceItems = (from apiStation in apiStations.Values
+                                   from apiService in apiStation.apiServices.Values
+                                   where apiService.apiNodeCount==0
+                                   select (apiStation,apiService)).ToList();
+
+                foreach (var (apiStation, apiService) in apiServiceItems)
+                {
+                    apiStation.ApiService_Remove(apiService.apiDesc.ServiceKeyGet());
+                }
+
+
+                changedApiStationList = apiServiceItems.Select(m => m.apiStation).Distinct().ToList();
+
+                foreach (var apiStation in changedApiStationList)
+                {
+                    if (apiStation.apiServiceCount == 0) 
+                    {
+                        ApiStation_Remove(apiStation);
+                    } 
+                }
+            }
+
+            #region 持久化对应ApiStation中的所有ApiDesc(异步执行)
+            Task.Run(() =>
+            {
+                try
+                {
+                    foreach (var apiStation in changedApiStationList)
+                    {
+                        Persistence_ApiDesc.ApiDesc_SaveApiStationToJsonFile(apiStation);
+                    }
+                }
+                catch (Exception ex)
+                {
+                    Logger.Error(ex);
+                }
+            });
+            #endregion
+
+        }
+        #endregion
+
+
+
         #region ApiNode
 
         static bool Config_ApiRegistEvent_Print = (false != ConfigurationManager.Instance.GetByPath<bool?>("Sers.ServiceCenter.ApiRegistEvent_Print"));
@@ -207,6 +256,10 @@ namespace Sers.Gover.Base
         #endregion
 
 
+
+
+
+
         #region ServiceStation        
 
         #region ServiceStation_Add

+ 8 - 1
dotnet/ServiceCenter/Sers.ServiceCenter/Sers.Gover/Base/Model/ApiStationData.cs

@@ -91,7 +91,14 @@ namespace Sers.Gover.Base.Model
             apiService.counter.ReportTo(counter);
             return apiService;
         }
-         
+
+
+        public ApiService ApiService_Remove(string serviceKey)
+        {
+            if (apiServices.TryRemove(serviceKey, out var apiService)) return apiService;
+            return null;         
+        }
+
 
         public ApiService ApiService_Get(string serviceKey)
         {

+ 11 - 4
dotnet/ServiceCenter/Sers.ServiceCenter/Sers.Gover/Controllers/ApiControllers/ApiDescController.cs

@@ -18,7 +18,6 @@ namespace Sers.Gover.Controllers.ApiControllers
        
         [SsRoute("apiDesc/getActive")]
         [SsCallerSource(ECallerSource.Internal)]
-        //[CallFromGover]
         [SsName("获取所有可调用api")]
         [SsDescription("获取所有可调用api。返回ApiDesc列表")]
         public ApiReturn<List<SsApiDesc>> GetActive([SsDescription("route前缀(不指定则返回所有),例如 \"_gover_\"、\"/demo/a.html\" "),SsExample("_gover_")]string r)
@@ -35,7 +34,6 @@ namespace Sers.Gover.Controllers.ApiControllers
 
         [SsRoute("apiDesc/getAll")]
         [SsCallerSource(ECallerSource.Internal)]
-        //[CallFromGover]
         [SsName("获取所有api")]
         [SsDescription("获取所有api。返回ApiDesc列表")]
         public ApiReturn<List<SsApiDesc>> GetAll([SsDescription("route前缀(不指定则返回所有),例如 \"_gover_\"、\"/demo/a.html\" "), SsExample("_gover_")]string r)
@@ -49,8 +47,17 @@ namespace Sers.Gover.Controllers.ApiControllers
             return new ApiReturn<List<SsApiDesc>> { data = apiDescs.ToList() }; 
         }
 
- 
- 
+
+        [SsRoute("apiDesc/removeOffline")]
+        [SsCallerSource(ECallerSource.Internal)]
+        [SsName("移除离线的api")]
+        [SsDescription("移除离线的api")]
+        public ApiReturn RemoveOffline()
+        {
+            GoverApiCenterService.Instance.apiStationMng.ApiService_RemoveOffline();
+            return true;
+        }
+
 
 
 

+ 13 - 5
dotnet/ServiceCenter/Sers.ServiceCenter/Sers.Gover/Persistence/Persistence_ApiDesc.cs

@@ -29,16 +29,24 @@ namespace Sers.Gover.Persistence
         {
             try
             {
-                var apiDescs = data?.apiServices.Values.Select(m => m.apiDesc).ToList();
-
                 var filePath = GetFilePath(data.stationName + ".json");
 
-                var fileContent = apiDescs.SerializeToBytes();
+                var apiDescs = data.apiServices.Values.Select(m => m.apiDesc).ToList();
+
+                if (apiDescs.Count == 0) 
+                {
+                    if(File.Exists(filePath))
+                        File.Delete(filePath);
+                }
+                else
+                {
 
-                Directory.CreateDirectory(Path.GetDirectoryName(filePath));
+                    var fileContent = apiDescs.SerializeToBytes();
 
+                    Directory.CreateDirectory(Path.GetDirectoryName(filePath));
 
-                File.WriteAllBytes(filePath, fileContent);          
+                    File.WriteAllBytes(filePath, fileContent);
+                }
             }
             catch (System.Exception ex)
             {