forked from hoangthienan/util_core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathService.php
139 lines (115 loc) · 3.82 KB
/
Service.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
namespace go1\util;
use function defined;
use function getenv;
class Service
{
const VERSION = 'v19.02.22.0';
public static function cacheOptions($root)
{
return (getenv('CACHE_BACKEND') && 'memcached' === getenv('CACHE_BACKEND'))
? ['backend' => 'memcached', 'host' => getenv('CACHE_HOST'), 'port' => getenv('CACHE_PORT')]
: ['backend' => 'filesystem', 'directory' => $root . '/cache'];
}
public static function queueOptions()
{
return [
'host' => getenv('QUEUE_HOST') ?: '172.31.11.129',
'port' => getenv('QUEUE_PORT') ?: '5672',
'user' => getenv('QUEUE_USER') ?: 'go1',
'pass' => getenv('QUEUE_PASSWORD') ?: 'go1',
];
}
public static function accountsName(string $env): string
{
if ($configured = getenv('ACCOUNTS_NAME')) {
return $configured;
}
switch ($env) {
case 'production':
case 'staging':
return 'accounts.gocatalyze.com';
default:
return 'accounts-dev.gocatalyze.com';
}
}
public static function urls(array $names, string $env, string $pattern = null): array
{
foreach ($names as $name) {
$urls["{$name}_url"] = static::url($name, $env, $pattern);
}
return !empty($urls) ? $urls : [];
}
public static function url(string $name, string $env, string $pattern = null): string
{
// K8S Service discovery using ENV variables
$k8sName = strtoupper(str_replace(["-", "."], "_", $name));
$k8sHost = getenv("{$k8sName}_SERVICE_HOST");
$k8sPort = getenv("{$k8sName}_SERVICE_PORT");
if ($k8sHost && $k8sPort) {
return "http://{$k8sHost}:{$k8sPort}";
}
$pattern = $pattern ?: 'http://SERVICE.ENVIRONMENT.go1.service';
// There are some services don't have staging instance yet.
if (in_array($name, ['rules'])) {
$env = 'production';
}
return str_replace(['SERVICE', 'ENVIRONMENT'], [$name, $env], $pattern);
}
/**
* This method is only for dev environment for now.
*
* The container's /etc/resolver.conf, change nameserver to
*
* nameserver 172.31.10.148
*
* @param string $env
* @param string $name
* @return string[]
*/
public static function ipPort(string $env, string $name)
{
$records = dns_get_record("$env.$name.service.consul", DNS_SRV);
if ($records) {
$service = &$records[0];
return [$service['target'], $service['port']];
}
}
public static function elasticSearchIndex(): string
{
!defined('ES_INDEX') && define('ES_INDEX', getenv('ES_INDEX') ?: 'go1_dev');
return ES_INDEX;
}
public static function isLocalIp(): bool
{
$localIps = getenv('LOCAL_IPS');
if ($localIps) {
$ip = isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : (isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : null);
if ($ip) {
$localIps = explode(',', $localIps);
foreach ($localIps as $localIp) {
if (false !== strpos($ip, $localIp)) {
return true;
}
}
}
}
return false;
}
/**
* @param $env
* @param bool $public
* @return string
*/
public static function gatewayUrl($env, $public = false)
{
if (!empty($url = getenv('GATEWAY_URL'))) {
return $url;
}
if ($public) {
$suffix = 'production' === $env ? '' : "-{$env}";
return "https://api{$suffix}.go1.co";
}
return self::url('gateway', $env);
}
}