1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace CCT\Kong\Model; |
6
|
|
|
|
7
|
|
|
class Api |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Identification of API |
11
|
|
|
* |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
protected $id; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The API name |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $name; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The number of retries to execute upon failure to proxy. The default is 5. |
25
|
|
|
* |
26
|
|
|
* @var int |
27
|
|
|
*/ |
28
|
|
|
protected $retries = 5; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* A comma-separated list of domain names that point to your API. For example: example.com. |
32
|
|
|
* At least one of hosts, uris, or methods should be specified. |
33
|
|
|
* |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
protected $hosts = []; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* A comma-separated list of URIs prefixes that point to your API. For example: /my-path. |
40
|
|
|
* At least one of hosts, uris, or methods should be specified. |
41
|
|
|
* |
42
|
|
|
* @var array |
43
|
|
|
*/ |
44
|
|
|
protected $uris = []; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* A comma-separated list of HTTP methods that point to your API. |
48
|
|
|
* For example: GET,POST. At least one of hosts, uris, or methods should be specified. |
49
|
|
|
* |
50
|
|
|
* @var array |
51
|
|
|
*/ |
52
|
|
|
protected $methods = []; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* The base target URL that points to your API server. This URL will be used for proxying requests. For example: https://example.com. |
56
|
|
|
* |
57
|
|
|
* @var string |
58
|
|
|
*/ |
59
|
|
|
protected $upstreamUrl; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* When matching an API via one of the uris prefixes, strip that |
63
|
|
|
* matching prefix from the upstream URI to be requested. Default: true. |
64
|
|
|
* |
65
|
|
|
* @var bool |
66
|
|
|
*/ |
67
|
|
|
protected $stripUri = true; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* When matching an API via one of the hosts domain names, make sure the request Host header is |
71
|
|
|
* forwarded to the upstream service. By default, this is false, and the upstream |
72
|
|
|
* Host header will be extracted from the configured upstream_url. |
73
|
|
|
* |
74
|
|
|
* @var bool |
75
|
|
|
*/ |
76
|
|
|
protected $preserveHost = false; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* To be enabled if you wish to only serve an API through HTTPS, |
80
|
|
|
* on the appropriate port (8443 by default). Default: false. |
81
|
|
|
* |
82
|
|
|
* @var bool |
83
|
|
|
*/ |
84
|
|
|
protected $httpsOnly = false; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* The timeout in milliseconds for establishing a connection to your upstream service. Defaults to 60000. |
88
|
|
|
* |
89
|
|
|
* @var int |
90
|
|
|
*/ |
91
|
|
|
protected $upstreamConnectTimeout = 60000; |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* The timeout in milliseconds between two successive write operations |
95
|
|
|
* for transmitting a request to your upstream service Defaults to 60000. |
96
|
|
|
* |
97
|
|
|
* @var int |
98
|
|
|
*/ |
99
|
|
|
protected $upstreamSendTimeout = 60000; |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* The timeout in milliseconds between two successive read operations for transmitting |
103
|
|
|
* a request to your upstream service Defaults to 60000. |
104
|
|
|
* |
105
|
|
|
* @var int |
106
|
|
|
*/ |
107
|
|
|
protected $upstreamReadTimeout = 60000; |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Consider the X-Forwarded-Proto header when enforcing HTTPS only traffic. Default: false. |
111
|
|
|
* |
112
|
|
|
* @var bool |
113
|
|
|
*/ |
114
|
|
|
protected $httpIfTerminated = false; |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* The date when the API was created. |
118
|
|
|
* |
119
|
|
|
* @var \DateTime |
120
|
|
|
*/ |
121
|
|
|
protected $createdAt; |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Gets the API`s identifier. |
125
|
|
|
* |
126
|
|
|
* @return null|string |
127
|
|
|
*/ |
128
|
1 |
|
public function getId(): ?string |
129
|
|
|
{ |
130
|
1 |
|
return $this->id; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Sets the API name. |
135
|
|
|
* |
136
|
|
|
* @param string $name |
137
|
|
|
* |
138
|
|
|
* @return static |
139
|
|
|
*/ |
140
|
1 |
|
public function setName(string $name): self |
141
|
|
|
{ |
142
|
1 |
|
$this->name = $name; |
143
|
|
|
|
144
|
1 |
|
return $this; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Gets the API name. |
149
|
|
|
* |
150
|
|
|
* @return null|string |
151
|
|
|
*/ |
152
|
1 |
|
public function getName(): ?string |
153
|
|
|
{ |
154
|
1 |
|
return $this->name; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Sets the number of attempts before the upon failure to proxy. |
159
|
|
|
* |
160
|
|
|
* @param int $attempts |
161
|
|
|
* |
162
|
|
|
* @return static |
163
|
|
|
*/ |
164
|
1 |
|
public function setRetries(int $attempts): self |
165
|
|
|
{ |
166
|
1 |
|
$this->retries = $attempts; |
167
|
|
|
|
168
|
1 |
|
return $this; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Gets the number of attempts before the upon failure to proxy. |
173
|
|
|
* |
174
|
|
|
* @return int |
175
|
|
|
*/ |
176
|
1 |
|
public function getRetries(): int |
177
|
|
|
{ |
178
|
1 |
|
return $this->retries; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Sets the domain names that point to your API. |
183
|
|
|
* |
184
|
|
|
* @param array $hosts |
185
|
|
|
* |
186
|
|
|
* @return static |
187
|
|
|
*/ |
188
|
1 |
|
public function setHosts(array $hosts = []): self |
189
|
|
|
{ |
190
|
1 |
|
$this->hosts = $hosts; |
191
|
|
|
|
192
|
1 |
|
return $this; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Gets the domain names that point to your API. |
197
|
|
|
* |
198
|
|
|
* @return array |
199
|
|
|
*/ |
200
|
1 |
|
public function getHosts(): array |
201
|
|
|
{ |
202
|
1 |
|
return $this->hosts; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Sets the list of URIs prefixes that point to your API. |
207
|
|
|
* |
208
|
|
|
* @param array $uris |
209
|
|
|
* |
210
|
|
|
* @return static |
211
|
|
|
*/ |
212
|
|
|
public function setUris(array $uris = []): self |
213
|
|
|
{ |
214
|
|
|
$this->uris = $uris; |
215
|
|
|
|
216
|
|
|
return $this; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Sets the list of URIs prefixes that point to your API. |
221
|
|
|
* |
222
|
|
|
* @return array |
223
|
|
|
*/ |
224
|
|
|
public function getUris(): array |
225
|
|
|
{ |
226
|
|
|
return $this->hosts; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Sets the list of HTTP methods that point to your API. |
231
|
|
|
* |
232
|
|
|
* @param array $methods |
233
|
|
|
* |
234
|
|
|
* @return static |
235
|
|
|
*/ |
236
|
1 |
|
public function setMethods(array $methods = []): self |
237
|
|
|
{ |
238
|
1 |
|
$this->methods = $methods; |
239
|
|
|
|
240
|
1 |
|
return $this; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Gets the list of HTTP methods that point to your API. |
245
|
|
|
* |
246
|
|
|
* @return array |
247
|
|
|
*/ |
248
|
1 |
|
public function getMethods(): array |
249
|
|
|
{ |
250
|
1 |
|
return $this->methods; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Sets the base target URL that points to your API server. |
255
|
|
|
* |
256
|
|
|
* @param string $upstreamUrl |
257
|
|
|
* |
258
|
|
|
* @return static |
259
|
|
|
*/ |
260
|
1 |
|
public function setUpstreamUrl(string $upstreamUrl): self |
261
|
|
|
{ |
262
|
1 |
|
$this->upstreamUrl = $upstreamUrl; |
263
|
|
|
|
264
|
1 |
|
return $this; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Gets the base target URL that points to your API server. |
269
|
|
|
* |
270
|
|
|
* @return null|string |
271
|
|
|
*/ |
272
|
1 |
|
public function getUpstreamUrl(): ?string |
273
|
|
|
{ |
274
|
1 |
|
return $this->upstreamUrl; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* Sets if it should strip the URI defined when it matches with an API. |
279
|
|
|
* |
280
|
|
|
* @param bool $stripUri |
281
|
|
|
* |
282
|
|
|
* @return static |
283
|
|
|
*/ |
284
|
1 |
|
public function setStripUri(bool $stripUri): self |
285
|
|
|
{ |
286
|
1 |
|
$this->stripUri = $stripUri; |
287
|
|
|
|
288
|
1 |
|
return $this; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* Checks if it should strip the URI defined when it matches with an API. |
293
|
|
|
* |
294
|
|
|
* @return bool |
295
|
|
|
*/ |
296
|
1 |
|
public function isStripUri(): bool |
297
|
|
|
{ |
298
|
1 |
|
return $this->stripUri; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Sets it the request Host header is forwarded to the upstream service |
303
|
|
|
* when there is a match to an API registered in one of the hosts. |
304
|
|
|
* |
305
|
|
|
* @param bool $preserveHost |
306
|
|
|
* |
307
|
|
|
* @return static |
308
|
|
|
*/ |
309
|
1 |
|
public function setPreserveHost(bool $preserveHost): self |
310
|
|
|
{ |
311
|
1 |
|
$this->preserveHost = $preserveHost; |
312
|
|
|
|
313
|
1 |
|
return $this; |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* Checks it the request Host header is forwarded to the upstream service |
318
|
|
|
* when there is a match to an API registered in one of the hosts. |
319
|
|
|
* |
320
|
|
|
* @return bool |
321
|
|
|
*/ |
322
|
1 |
|
public function isPreserveHost(): bool |
323
|
|
|
{ |
324
|
1 |
|
return $this->preserveHost; |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* Sets if the API should be served just through HTTPS. |
329
|
|
|
* |
330
|
|
|
* @param bool $httpsOnly |
331
|
|
|
* |
332
|
|
|
* @return static |
333
|
|
|
*/ |
334
|
1 |
|
public function setHttpsOnly(bool $httpsOnly): self |
335
|
|
|
{ |
336
|
1 |
|
$this->httpsOnly = $httpsOnly; |
337
|
|
|
|
338
|
1 |
|
return $this; |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* Gets if the API should be served just through HTTPS. |
343
|
|
|
* |
344
|
|
|
* @return bool |
345
|
|
|
*/ |
346
|
1 |
|
public function getHttpsOnly(): bool |
347
|
|
|
{ |
348
|
1 |
|
return $this->httpsOnly; |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* Sets the timeout in milliseconds for establishing a connection to your upstream. |
353
|
|
|
* |
354
|
|
|
* @param int $upstreamConnectTimeout |
355
|
|
|
* |
356
|
|
|
* @return static |
357
|
|
|
*/ |
358
|
1 |
|
public function setUpstreamConnectTimeout(int $upstreamConnectTimeout): self |
359
|
|
|
{ |
360
|
1 |
|
$this->upstreamConnectTimeout = $upstreamConnectTimeout; |
361
|
|
|
|
362
|
1 |
|
return $this; |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* Gets the timeout in milliseconds for establishing a connection to your upstream. |
367
|
|
|
* |
368
|
|
|
* @return int |
369
|
|
|
*/ |
370
|
1 |
|
public function getUpstreamConnectTimeout(): int |
371
|
|
|
{ |
372
|
1 |
|
return $this->upstreamConnectTimeout; |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
/** |
376
|
|
|
* Sets the timeout in milliseconds between two successive write |
377
|
|
|
* operations for transmitting a request to your upstream. |
378
|
|
|
* |
379
|
|
|
* @param int $upstreamSendTimeout |
380
|
|
|
* |
381
|
|
|
* @return static |
382
|
|
|
*/ |
383
|
1 |
|
public function setUpstreamSendTimeout(int $upstreamSendTimeout): self |
384
|
|
|
{ |
385
|
1 |
|
$this->upstreamSendTimeout = $upstreamSendTimeout; |
386
|
|
|
|
387
|
1 |
|
return $this; |
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
/** |
391
|
|
|
* Gets the timeout in milliseconds between two successive write |
392
|
|
|
* operations for transmitting a request to your upstream. |
393
|
|
|
* |
394
|
|
|
* @return int |
395
|
|
|
*/ |
396
|
1 |
|
public function getUpstreamSendTimeout(): int |
397
|
|
|
{ |
398
|
1 |
|
return $this->upstreamSendTimeout; |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
/** |
402
|
|
|
* Gets the timeout in milliseconds between two successive read operations for |
403
|
|
|
* transmitting a request to your upstream service. |
404
|
|
|
* |
405
|
|
|
* @param int $upstreamReadTimeout |
406
|
|
|
* |
407
|
|
|
* @return static |
408
|
|
|
*/ |
409
|
1 |
|
public function setUpstreamReadTimeout(int $upstreamReadTimeout): self |
410
|
|
|
{ |
411
|
1 |
|
$this->upstreamReadTimeout = $upstreamReadTimeout; |
412
|
|
|
|
413
|
1 |
|
return $this; |
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
/** |
417
|
|
|
* Gets the timeout in milliseconds between two successive read operations for |
418
|
|
|
* transmitting a request to your upstream service. |
419
|
|
|
* |
420
|
|
|
* @return int |
421
|
|
|
*/ |
422
|
1 |
|
public function getUpstreamReadTimeout(): int |
423
|
|
|
{ |
424
|
1 |
|
return $this->upstreamReadTimeout; |
425
|
|
|
} |
426
|
|
|
|
427
|
|
|
/** |
428
|
|
|
* @param bool $httpIfTerminated |
429
|
|
|
* |
430
|
|
|
* @return static |
431
|
|
|
*/ |
432
|
1 |
|
public function setHttpIfTerminated(bool $httpIfTerminated): self |
433
|
|
|
{ |
434
|
1 |
|
$this->httpIfTerminated = $httpIfTerminated; |
435
|
|
|
|
436
|
1 |
|
return $this; |
437
|
|
|
} |
438
|
|
|
|
439
|
|
|
/** |
440
|
|
|
* @return bool |
441
|
|
|
*/ |
442
|
1 |
|
public function isHttpIfTerminated(): bool |
443
|
|
|
{ |
444
|
1 |
|
return $this->httpIfTerminated; |
445
|
|
|
} |
446
|
|
|
|
447
|
|
|
/** |
448
|
|
|
* Gets the date which the API was created. |
449
|
|
|
* |
450
|
|
|
* @return \DateTime|null |
451
|
|
|
*/ |
452
|
1 |
|
public function getCreatedAt(): ?\DateTime |
453
|
|
|
{ |
454
|
1 |
|
return $this->createdAt; |
455
|
|
|
} |
456
|
|
|
} |
457
|
|
|
|