@@ -46,7 +46,6 @@ local ngx = ngx
4646local re_find = ngx .re .find
4747local process = require (" ngx.process" )
4848local worker_id = ngx .worker .id
49- local apisix_yaml_path = profile :yaml_path (" apisix" )
5049local created_obj = {}
5150local shared_dict
5251local status_report_shared_dict_name = " status-report"
@@ -56,6 +55,9 @@ local _M = {
5655 local_conf = config_local .local_conf ,
5756 clear_local_cache = config_local .clear_cache ,
5857
58+ -- yaml or json
59+ file_type = " yaml" ,
60+
5961 ERR_NO_SHARED_DICT = " failed prepare standalone config shared dict, this will degrade " ..
6062 " to event broadcasting, and if a worker crashes, the configuration " ..
6163 " cannot be restored from other workers and shared dict"
@@ -69,10 +71,68 @@ local mt = {
6971 end
7072}
7173
72-
7374local apisix_yaml
7475local apisix_yaml_mtime
7576
77+ local config_yaml = {
78+ path = profile :yaml_path (" apisix" ),
79+ type = " yaml" ,
80+ parse = function (self )
81+ local f , err = io.open (self .path , " r" )
82+ if not f then
83+ return nil , " failed to open file " .. self .path .. " : " .. err
84+ end
85+
86+ f :seek (' end' , - 10 )
87+ local end_flag = f :read (" *a" )
88+ local found_end_flag = re_find (end_flag , [[ #END\s*$]] , " jo" )
89+
90+ if not found_end_flag then
91+ f :close ()
92+ return nil , " missing valid end flag in file " .. self .path
93+ end
94+
95+ f :seek (' set' )
96+ local raw_config = f :read (" *a" )
97+ f :close ()
98+
99+ return yaml .load (raw_config ), nil
100+ end
101+ }
102+
103+ local config_json = {
104+ -- `-5` to remove the "yaml" suffix
105+ path = config_yaml .path :sub (1 , - 5 ) .. " json" ,
106+ type = " json" ,
107+ parse = function (self )
108+ local f , err = io.open (self .path , " r" )
109+ if not f then
110+ return nil , " failed to open file " .. self .path .. " : " .. err
111+ end
112+ local raw_config = f :read (" *a" )
113+ f :close ()
114+
115+ local config , err = json .decode (raw_config )
116+ if err then
117+ return nil , " failed to decode json: " .. err
118+ end
119+ return config , nil
120+ end
121+ }
122+
123+ local config_file_table = {
124+ yaml = config_yaml ,
125+ json = config_json
126+ }
127+
128+
129+ local config_file = setmetatable ({}, {
130+ __index = function (_ , key )
131+ return config_file_table [_M .file_type ][key ]
132+ end
133+ })
134+
135+
76136local function sync_status_to_shdict (status )
77137 if process .type () ~= " worker" then
78138 return
@@ -112,13 +172,13 @@ local function is_use_admin_api()
112172end
113173
114174
115- local function read_apisix_yaml (premature , pre_mtime )
175+ local function read_apisix_config (premature , pre_mtime )
116176 if premature then
117177 return
118178 end
119- local attributes , err = lfs .attributes (apisix_yaml_path )
179+ local attributes , err = lfs .attributes (config_file . path )
120180 if not attributes then
121- log .error (" failed to fetch " , apisix_yaml_path , " attributes: " , err )
181+ log .error (" failed to fetch " , config_file . path , " attributes: " , err )
122182 return
123183 end
124184
@@ -127,36 +187,15 @@ local function read_apisix_yaml(premature, pre_mtime)
127187 return
128188 end
129189
130- local f , err = io.open (apisix_yaml_path , " r" )
131- if not f then
132- log .error (" failed to open file " , apisix_yaml_path , " : " , err )
133- return
134- end
135-
136- f :seek (' end' , - 10 )
137- local end_flag = f :read (" *a" )
138- -- log.info("flag: ", end_flag)
139- local found_end_flag = re_find (end_flag , [[ #END\s*$]] , " jo" )
140-
141- if not found_end_flag then
142- f :close ()
143- log .warn (" missing valid end flag in file " , apisix_yaml_path )
144- return
145- end
146-
147- f :seek (' set' )
148- local yaml_config = f :read (" *a" )
149- f :close ()
150-
151- local apisix_yaml_new = yaml .load (yaml_config )
152- if not apisix_yaml_new then
153- log .error (" failed to parse the content of file " .. apisix_yaml_path )
190+ local config_new , err = config_file :parse ()
191+ if err then
192+ log .error (" failed to parse the content of file " , config_file .path , " : " , err )
154193 return
155194 end
156195
157- update_config (apisix_yaml_new , last_modification_time )
196+ update_config (config_new , last_modification_time )
158197
159- log .warn (" config file " , apisix_yaml_path , " reloaded." )
198+ log .warn (" config file " , config_file . path , " reloaded." )
160199end
161200
162201
@@ -171,7 +210,7 @@ local function sync_data(self)
171210 else
172211 if not apisix_yaml_mtime then
173212 log .warn (" wait for more time" )
174- return nil , " failed to read local file " .. apisix_yaml_path
213+ return nil , " failed to read local file " .. config_file . path
175214 end
176215 conf_version = apisix_yaml_mtime
177216 end
@@ -395,15 +434,15 @@ local function _automatic_fetch(premature, self)
395434 local ok , ok2 , err = pcall (sync_data , self )
396435 if not ok then
397436 err = ok2
398- log .error (" failed to fetch data from local file " .. apisix_yaml_path .. " : " ,
437+ log .error (" failed to fetch data from local file " .. config_file . path .. " : " ,
399438 err , " , " , tostring (self ))
400439 ngx_sleep (3 )
401440 break
402441
403442 elseif not ok2 and err then
404443 if err ~= " timeout" and err ~= " Key not found"
405444 and self .last_err ~= err then
406- log .error (" failed to fetch data from local file " .. apisix_yaml_path .. " : " ,
445+ log .error (" failed to fetch data from local file " .. config_file . path .. " : " ,
407446 err , " , " , tostring (self ))
408447 end
409448
@@ -477,7 +516,7 @@ function _M.new(key, opts)
477516 end
478517
479518 if err then
480- log .error (" failed to fetch data from local file " , apisix_yaml_path , " : " ,
519+ log .error (" failed to fetch data from local file " , config_file . path , " : " ,
481520 err , " , " , key )
482521 end
483522
@@ -517,7 +556,7 @@ function _M.init()
517556 return true
518557 end
519558
520- read_apisix_yaml ()
559+ read_apisix_config ()
521560 return true
522561end
523562
@@ -531,7 +570,7 @@ function _M.init_worker()
531570 end
532571
533572 -- sync data in each non-master process
534- ngx .timer .every (1 , read_apisix_yaml )
573+ ngx .timer .every (1 , read_apisix_config )
535574
536575 return true
537576end
0 commit comments