Quantcast
Viewing all articles
Browse latest Browse all 103

Answer by mikyll98 for Integrate Python plugin in APISIX Instance

About the Error

The error you're facing, as stated in the log message, is due to bad indenting in the configuration file (config.yaml). Most likely you missed some space or tab, so you should look for that. It's difficult to know without the actual content of the file.

About the Python Plugin Runner

NB: the project is still experimental and probably not maintained (see this issue from 10/02/2024 which got no replies, and the latest commit is from September 2022). From the GitHub README:

This project is currently in the experimental stage and it is not recommended to be used in a production environment.

However, if you still want to use it, you can follow the steps in the official documentation.

Example

  1. Clone the Git repository

    git clone https://github.com/apache/apisix-python-plugin-runner.gitcd apisix-python-plugin-runner
  2. Install APISIX Python Plugin Runner:

    make setupmake install
  3. Run APISIX Python Plugin Runner:

    make dev
  4. Config APISIX to start the Python Plugin Runner by using ext-plugin field. In file config.yaml add these lines:

    ext-plugin:    cmd: [ "python3", "/path/to/apisix-python-plugin-runner/bin/py-runner", "start" ] 
  5. Reload APISIX:

    apisix reload
  6. Write the plugin code by implementing the PluginBase parent class. Example from the repository:

    from typing import Anyfrom apisix.runner.http.request import Requestfrom apisix.runner.http.response import Responsefrom apisix.runner.plugin.core import PluginBaseclass Stop(PluginBase):    def name(self) -> str:"""        The name of the plugin registered in the runner        :return:"""        return "stop"    def config(self, conf: Any) -> Any:"""        Parse plugin configuration        :param conf:        :return:"""        return conf    def filter(self, conf: Any, request: Request, response: Response):"""        The plugin executes the main function        :param conf:            plugin configuration after parsing        :param request:            request parameters and information        :param response:            response parameters and information        :return:"""        # print plugin configuration        print(conf)        # Fetch request nginx variable `host`        host = request.get_var("host")        print(host)        # Fetch request body        body = request.get_body()        print(body)        # Set response headers        response.set_header("X-Resp-A6-Runner", "Python")        # Set response body        response.set_body("Hello, Python Runner of APISIX")        # Set response status code        response.set_status_code(201)
  7. Use the Admin APIs to enable the plugin on a certain ApisixRoute (example using httpbin.org service, without admin API key):

    curl -s -X PUT -i "http://localhost:9180/apisix/admin/routes/python" -d '{"uri": "/httpbin/test/python-runner/*","plugins": {"proxy-rewrite": {"regex_uri": ["^/httpbin/test/python-runner/(.*)","/get"            ],"method": "GET"        },"ext-plugin-pre-req": {"conf": [                { "name": "stop", "value": "{\"body\":\"hello\"}"                }            ]        }    },"upstream": {"type": "roundrobin","nodes": {"httpbin.org": 1        }    }}'
  8. Test the route:

    curl -s -i "localhost:9080/httpbin/test/python-runner/test"

    You should get something like this:

    Date: Tue, 06 Aug 2024 07:23:17 GMTContent-Type: text/plain; charset=utf-8Transfer-Encoding: chunkedConnection: keep-alivex-resp-a6-runner: PythonServer: APISIX/3.9.1Hello, Python Runner of APISIX

Viewing all articles
Browse latest Browse all 103

Trending Articles