Divide the MSB source codes into two repos
[msb/apigateway.git] / openresty-ext / src / assembly / resources / openresty / nginx / luaext / lib / utils / svc_util.lua
1 --[[\r
2 \r
3     Copyright (C) 2016 ZTE, Inc. and others. All rights reserved. (ZTE)\r
4 \r
5     Licensed under the Apache License, Version 2.0 (the "License");\r
6     you may not use this file except in compliance with the License.\r
7     You may obtain a copy of the License at\r
8 \r
9             http://www.apache.org/licenses/LICENSE-2.0\r
10 \r
11     Unless required by applicable law or agreed to in writing, software\r
12     distributed under the License is distributed on an "AS IS" BASIS,\r
13     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
14     See the License for the specific language governing permissions and\r
15     limitations under the License.\r
16 \r
17 --]]\r
18 \r
19 local _M = {}\r
20 _M._VERSION = '1.0.0'\r
21 \r
22 local msbConf= require('conf.msbinit')\r
23 local svcConf   =  require('conf.svcconf')\r
24 local log_util  =  require('lib.utils.log_util')\r
25 \r
26 local log = log_util.log\r
27 local ngx_var = ngx.var\r
28 \r
29 local defaultport = msbConf.systemConf.defaultport\r
30 local defaulthttpsport = msbConf.systemConf.defaulthttpsport\r
31 local defaultprefix = msbConf.systemConf.defaultprefix\r
32 local router_subdomain = msbConf.routerConf.subdomain\r
33 local router_defaultprefix = msbConf.routerConf.defaultprefix\r
34 local useconsultemplate = msbConf.systemConf.useconsultemplate\r
35 local urlfieldMap = svcConf.urlfieldMap\r
36 local apiRelatedTypes = svcConf.apiRelatedTypes\r
37 \r
38 function _M.isactive(svcinfo)\r
39         if svcinfo["status"] == "1" then\r
40                 return true\r
41         else\r
42                 return false\r
43         end\r
44 end\r
45 \r
46 function _M.use_own_upstream(svcinfo)\r
47         if useconsultemplate and svcinfo.spec.useOwnUpstream == "1" then\r
48                 log("useOwnUpstream",true)\r
49                 return true\r
50         else\r
51                 return false\r
52         end\r
53 end\r
54 \r
55 function _M.get_url(svcinfo,svc_type)\r
56         return svcinfo.spec[urlfieldMap[svc_type]]\r
57 end\r
58 \r
59 function _M.get_backend_protocol(svcinfo)\r
60         local svc_enable_ssl = svcinfo.spec["enable_ssl"]\r
61         if svc_enable_ssl then\r
62                 return "https"\r
63         else\r
64                 return "http"\r
65         end\r
66 end\r
67 \r
68 function _M.get_key_prefix()\r
69         --now assemble the key prefix according the svc_name and server_port\r
70         local key_prefix = ""\r
71         local server_port = ngx_var.server_port\r
72         local svc_name = ngx_var.svc_name\r
73         if (svc_name == "microservices" or svc_name == "msdiscover") then\r
74                 key_prefix = defaultprefix\r
75         elseif (server_port == defaultport or server_port == defaulthttpsport) then\r
76                 local m, err = ngx.re.match(ngx_var.host, "(?<hostname>.+)\\."..router_subdomain,"o")\r
77                 if m then\r
78                         key_prefix = router_defaultprefix..":"..m["hostname"]\r
79                 else\r
80                         key_prefix = defaultprefix\r
81                 end\r
82         else\r
83                 key_prefix = "msb:"..server_port\r
84         end\r
85         return key_prefix\r
86 end\r
87 \r
88 function _M.is_api_related_types(svc_type)\r
89         if(apiRelatedTypes[svc_type]) then\r
90                 return true\r
91         else\r
92                 return false\r
93         end\r
94 end\r
95 \r
96 return _M