6
6
using System . Collections . Generic ;
7
7
using System . Threading . Tasks ;
8
8
using Agile . Config . Protocol ;
9
- using Microsoft . Extensions . Logging ;
10
- using Newtonsoft . Json ;
9
+ using AgileConfig . Server . Apisite . Models . Mapping ;
11
10
using AgileConfig . Server . Common . EventBus ;
12
11
using AgileConfig . Server . Event ;
13
12
@@ -23,32 +22,23 @@ public class RegisterCenterController : Controller
23
22
private readonly IRegisterCenterService _registerCenterService ;
24
23
private readonly IServiceInfoService _serviceInfoService ;
25
24
private readonly ITinyEventBus _tinyEventBus ;
26
- private readonly ILogger < RegisterCenterController > _logger ;
27
25
28
26
public RegisterCenterController ( IRegisterCenterService registerCenterService
29
27
, IServiceInfoService serviceInfoService ,
30
- ILoggerFactory loggerFactory ,
31
28
ITinyEventBus tinyEventBus
32
29
)
33
30
{
34
31
_registerCenterService = registerCenterService ;
35
32
_serviceInfoService = serviceInfoService ;
36
33
_tinyEventBus = tinyEventBus ;
37
- _logger = loggerFactory . CreateLogger < RegisterCenterController > ( ) ;
38
34
}
39
35
40
36
[ HttpPost ]
41
37
public async Task < RegisterResultVM > Register ( [ FromBody ] RegisterServiceInfoVM model )
42
38
{
43
- var entity = new ServiceInfo ( ) ;
44
- entity . ServiceId = model . ServiceId ;
45
- entity . ServiceName = model . ServiceName ;
46
- entity . Ip = model . Ip ;
47
- entity . Port = model . Port ;
48
- entity . CheckUrl = model . CheckUrl ;
49
- entity . AlarmUrl = model . AlarmUrl ;
50
- entity . HeartBeatMode = model . HeartBeatMode ;
51
- entity . MetaData = model . MetaData is null ? "[]" : JsonConvert . SerializeObject ( model . MetaData ) ;
39
+ ArgumentNullException . ThrowIfNull ( model ) ;
40
+
41
+ var entity = model . ToServiceInfo ( ) ;
52
42
entity . RegisterWay = RegisterWay . Auto ;
53
43
54
44
var id = await _registerCenterService . RegisterAsync ( entity ) ;
@@ -94,10 +84,7 @@ public async Task<ActionResult<RegisterResultVM>> UnRegister(string id, [FromBod
94
84
[ HttpPost ( "heartbeat" ) ]
95
85
public async Task < ActionResult < HeartbeatResultVM > > Heartbeat ( [ FromBody ] HeartbeatParam param )
96
86
{
97
- if ( param == null )
98
- {
99
- throw new ArgumentNullException ( nameof ( param ) ) ;
100
- }
87
+ ArgumentNullException . ThrowIfNull ( param ) ;
101
88
102
89
bool serviceHeartbeatResult = false ;
103
90
if ( ! string . IsNullOrEmpty ( param . UniqueId ) )
@@ -120,89 +107,44 @@ public async Task<ActionResult<HeartbeatResultVM>> Heartbeat([FromBody] Heartbea
120
107
}
121
108
122
109
[ HttpGet ( "services" ) ]
123
- public async Task < List < ServiceInfoVM > > AllServices ( )
110
+ public async Task < List < ApiServiceInfoVM > > AllServices ( )
124
111
{
125
112
var services = await _serviceInfoService . GetAllServiceInfoAsync ( ) ;
126
- var vms = new List < ServiceInfoVM > ( ) ;
113
+ var vms = new List < ApiServiceInfoVM > ( ) ;
127
114
foreach ( var serviceInfo in services )
128
115
{
129
- var vm = new ServiceInfoVM
130
- {
131
- ServiceId = serviceInfo . ServiceId ,
132
- ServiceName = serviceInfo . ServiceName ,
133
- Ip = serviceInfo . Ip ,
134
- Port = serviceInfo . Port ,
135
- MetaData = new List < string > ( ) ,
136
- Status = serviceInfo . Status
137
- } ;
138
- try
139
- {
140
- vm . MetaData = JsonConvert . DeserializeObject < List < string > > ( serviceInfo . MetaData ) ;
141
- }
142
- catch ( Exception e )
143
- {
144
- _logger . LogError ( e , $ "deserialize meta data error, serviceId:{ serviceInfo . ServiceId } ") ;
145
- }
116
+ var vm = serviceInfo . ToApiServiceInfoVM ( ) ;
117
+
146
118
vms . Add ( vm ) ;
147
119
}
148
120
149
121
return vms ;
150
122
}
151
123
152
124
[ HttpGet ( "services/online" ) ]
153
- public async Task < List < ServiceInfoVM > > OnlineServices ( )
125
+ public async Task < List < ApiServiceInfoVM > > OnlineServices ( )
154
126
{
155
127
var services = await _serviceInfoService . GetOnlineServiceInfoAsync ( ) ;
156
- var vms = new List < ServiceInfoVM > ( ) ;
128
+ var vms = new List < ApiServiceInfoVM > ( ) ;
157
129
foreach ( var serviceInfo in services )
158
130
{
159
- var vm = new ServiceInfoVM
160
- {
161
- ServiceId = serviceInfo . ServiceId ,
162
- ServiceName = serviceInfo . ServiceName ,
163
- Ip = serviceInfo . Ip ,
164
- Port = serviceInfo . Port ,
165
- MetaData = new List < string > ( ) ,
166
- Status = serviceInfo . Status
167
- } ;
168
- try
169
- {
170
- vm . MetaData = JsonConvert . DeserializeObject < List < string > > ( serviceInfo . MetaData ) ;
171
- }
172
- catch ( Exception e )
173
- {
174
- _logger . LogError ( e , $ "deserialize meta data error, serviceId:{ serviceInfo . ServiceId } ") ;
175
- }
131
+ var vm = serviceInfo . ToApiServiceInfoVM ( ) ;
132
+
176
133
vms . Add ( vm ) ;
177
134
}
178
135
179
136
return vms ;
180
137
}
181
138
182
139
[ HttpGet ( "services/offline" ) ]
183
- public async Task < List < ServiceInfoVM > > OfflineServices ( )
140
+ public async Task < List < ApiServiceInfoVM > > OfflineServices ( )
184
141
{
185
142
var services = await _serviceInfoService . GetOfflineServiceInfoAsync ( ) ;
186
- var vms = new List < ServiceInfoVM > ( ) ;
143
+ var vms = new List < ApiServiceInfoVM > ( ) ;
187
144
foreach ( var serviceInfo in services )
188
145
{
189
- var vm = new ServiceInfoVM
190
- {
191
- ServiceId = serviceInfo . ServiceId ,
192
- ServiceName = serviceInfo . ServiceName ,
193
- Ip = serviceInfo . Ip ,
194
- Port = serviceInfo . Port ,
195
- MetaData = new List < string > ( ) ,
196
- Status = serviceInfo . Status
197
- } ;
198
- try
199
- {
200
- vm . MetaData = JsonConvert . DeserializeObject < List < string > > ( serviceInfo . MetaData ) ;
201
- }
202
- catch ( Exception e )
203
- {
204
- _logger . LogError ( e , $ "deserialize meta data error, serviceId:{ serviceInfo . ServiceId } ") ;
205
- }
146
+ var vm = serviceInfo . ToApiServiceInfoVM ( ) ;
147
+
206
148
vms . Add ( vm ) ;
207
149
}
208
150
0 commit comments