AlinsRan commented on code in PR #12179:
URL: https://github.com/apache/apisix/pull/12179#discussion_r2067774568


##########
apisix/admin/standalone.lua:
##########
@@ -0,0 +1,221 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local type         = type
+local pairs        = pairs
+local ipairs       = ipairs
+local tonumber     = tonumber
+local tostring     = tostring
+local str_lower    = string.lower
+local ngx          = ngx
+local get_method   = ngx.req.get_method
+local shared_dict  = ngx.shared["standalone-config"]
+local table_insert = table.insert
+local table_new    = require("table.new")
+local yaml         = require("lyaml")
+local events       = require("apisix.events")
+local core         = require("apisix.core")
+local config_yaml  = require("apisix.core.config_yaml")
+local check_schema = require("apisix.core.schema").check
+
+local EVENT_UPDATE = "standalone-api-configuration-update"
+
+local _M = {}
+
+
+local function update_and_broadcast_config(apisix_yaml, conf_version)
+    local config = core.json.encode({
+        conf = apisix_yaml,
+        conf_version = conf_version,
+    })
+
+    if shared_dict then
+        -- the worker that handles Admin API calls is responsible for writing 
the shared dict
+        local ok, err = shared_dict:set("config", config)
+        if not ok then
+            return nil, "failed to save config to shared dict: " .. err
+        end
+        ngx.log(ngx.NOTICE, "standalone config updated: ", config)
+    else
+        core.log.crit(config_yaml.ERR_NO_SHARED_DICT)
+    end
+    return events:post(EVENT_UPDATE, EVENT_UPDATE)
+end
+
+
+local function update(ctx)
+    local content_type = core.request.header(nil, "content-type") or 
"application/json"
+
+    local conf_version
+    if ctx.var.arg_conf_version then
+        conf_version = tonumber(ctx.var.arg_conf_version)
+        if not conf_version then
+            return core.response.exit(400, {error_msg = "invalid conf_version: 
"
+                                            .. ctx.var.arg_conf_version
+                                            .. ", should be a integer" })
+        end
+    else
+        conf_version = ngx.time()
+    end
+    -- check if conf_version greater than the current version
+    local _, ver = config_yaml._get_config()
+    if conf_version <= ver then
+        return core.response.exit(400, {error_msg = "invalid conf_version: 
conf_version ("
+                                        .. conf_version
+                                        .. ") should be greater than the 
current version ("
+                                        .. ver .. ")"})
+    end
+
+    -- read the request body
+    local req_body, err = core.request.get_body()
+    if err then
+        return core.response.exit(400, {error_msg = "invalid request body: " 
.. err})
+    end
+
+    if not req_body or #req_body <= 0 then
+        return core.response.exit(400, {error_msg = "invalid request body: 
empty request body"})
+    end
+
+    -- parse the request body
+    if req_body then
+        local data, err
+        if core.string.has_prefix(content_type, "application/yaml") then
+            data = yaml.load(req_body, { all = false })
+            if not data or type(data) ~= "table" then
+                err = "invalid yaml request body"
+            end
+        else
+            data, err = core.json.decode(req_body)
+        end
+        if err then
+            core.log.error("invalid request body: ", req_body, " err: ", err)
+            core.response.exit(400, {error_msg = "invalid request body: " .. 
err,
+                                     req_body = req_body})
+        end
+
+        req_body = data
+    end
+
+    -- check input by jsonschema
+    local apisix_yaml = {}
+    local created_objs = config_yaml.fetch_all_created_obj()
+    for key, obj in pairs(created_objs) do
+        if req_body[key] and #req_body[key] > 0 then
+            apisix_yaml[key] = table_new(1, 0)
+            local item_schema = obj.item_schema
+            local item_checker = obj.checker
+
+            for index, item in ipairs(req_body[key]) do
+                local valid, err
+                -- need to recover to 0-based subscript
+                local err_prefix = "invalid " .. key .. " at index " .. (index 
- 1) .. ", err: "
+                if item_schema then
+                    valid, err = check_schema(obj.item_schema, item)
+                    if not valid then
+                        core.log.error(err_prefix, err)
+                        core.response.exit(400, {error_msg = err_prefix .. err,
+                                                req_body = req_body})
+                    end
+                end
+                if item_checker then
+                    valid, err = item_checker(item)
+                    if not valid then
+                        core.log.error(err_prefix, err)
+                        core.response.exit(400, {error_msg = err_prefix .. err,
+                                                req_body = req_body})
+                    end
+                end
+                table_insert(apisix_yaml[key], item)
+            end
+        end
+    end
+
+    local ok, err = update_and_broadcast_config(apisix_yaml, conf_version)
+    if not ok then
+        core.response.exit(500, err)
+    end
+
+    core.response.set_header("X-APISIX-Conf-Version", tostring(conf_version))

Review Comment:
   How about using `X-APISIX-Config-Version`.



-- 
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: notifications-unsubscr...@apisix.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to