kingluo commented on code in PR #9259:
URL: https://github.com/apache/apisix/pull/9259#discussion_r1160514763


##########
apisix/core/config_etcd.lua:
##########
@@ -463,6 +480,76 @@ local function sync_data(self)
         end
 
         self.conf_version = self.conf_version + 1
+
+        if self.key ~= "/apisix/routes" then
+            goto CONTINUE
+        end
+
+        local route
+        if res.value then
+            local status = table.try_read_attr(res, "value", "status")
+            if status and status == 0 then
+                goto CONTINUE
+            end
+
+            local filter_fun, err
+            if res.value.filter_func then
+                filter_fun, err = loadstring(
+                    "return " .. res.value.filter_func,
+                    "router#" .. res.value.id
+                )
+                if not filter_fun then
+                    log.error("failed to load filter function: ", err, " route 
id", res.value.id)
+                        goto CONTINUE
+                end
+
+                filter_fun = filter_fun()
+            end
+
+            route = {
+                id = res.value.id,
+                paths = res.value.uris or res.value.uri,
+                methods = res.value.methods,
+                priority = res.value.priority,
+                hosts = res.value.hosts or res.value.host,
+                remote_addrs = res.value.remote_addrs or res.value.remote_addr,
+                vars = res.value.vars,
+                filter_fun = filter_fun,
+                handler = function(api_ctx, match_opts)
+                    api_ctx.matched_params = nil
+                    api_ctx.matched_route = res
+                    api_ctx.curr_req_matched = match_opts.matched
+                end
+            }
+        end
+
+        local router_opts = {
+            no_param_match = true
+        }
+        local router_mod = require("apisix.router")
+        if op == 2 then
+            log.notice("create routes watched from etcd into radixtree.", 
json.encode(res))
+            err = router_mod.uri_router:add_route(route, router_opts)

Review Comment:
   Please refer to my suggestion below about code refacotring, thanks.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to