{"id":22771044,"url":"https://github.com/hiproxy/hiproxy-plugin-example","last_synced_at":"2025-03-30T12:13:22.147Z","repository":{"id":74229162,"uuid":"93947327","full_name":"hiproxy/hiproxy-plugin-example","owner":"hiproxy","description":"A hiproxy plugin example","archived":false,"fork":false,"pushed_at":"2017-06-14T03:26:59.000Z","size":117,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-05T13:48:36.997Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hiproxy.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-06-10T15:14:49.000Z","updated_at":"2021-11-19T10:31:26.000Z","dependencies_parsed_at":null,"dependency_job_id":"905b4966-df39-499d-b66f-56a65043075a","html_url":"https://github.com/hiproxy/hiproxy-plugin-example","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hiproxy%2Fhiproxy-plugin-example","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hiproxy%2Fhiproxy-plugin-example/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hiproxy%2Fhiproxy-plugin-example/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hiproxy%2Fhiproxy-plugin-example/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hiproxy","download_url":"https://codeload.github.com/hiproxy/hiproxy-plugin-example/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246314152,"owners_count":20757463,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-11T16:11:59.328Z","updated_at":"2025-03-30T12:13:22.141Z","avatar_url":"https://github.com/hiproxy.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# hiproxy-plugin-example\n\nhiproxy插件示例。这个插件为hiproxy添加了一个Rewrite指令、一个CLI命令和一个页面路由。\n\n* **名令**：`hello`，执行`hiproxy hello --name \u003cyour-name\u003e --age \u003cyour-age\u003e`\n* **指令**：`add`，在rewrite文件中使用`add $add_value 1600 88;`\n* **路由**：`test`，服务启动之后，访问`http://127.0.0.1:\u003cport\u003e/test`\n\n\u003cbr /\u003e\n\n## 安装\n\n```bash\nnpm install hiproxy/hiproxy-plugin-example -g\n```\n\n\u003e **注意**：必须要把插件安装到全局。hiproxy CLI默认去全局查找插件，所以必须保证安装到`npm root -g`所在的目录中，才能被正确加载。\n\n\u003cbr /\u003e\n\n## 使用\n\n安装完成后，hiproxy会自动加载所有安装的插件并注册插件定义的命令、指令和页面。\n\n\u003cbr /\u003e\n\n### 命令\n\n安装好这个示例插件之后，执行`hiproxy --help`，可以看到如下信息，从中我们能发现，有了`hello`这个命令。\n\n![command-hello](https://github.com/hiproxy/hiproxy-plugin-example/raw/master/screenshot/command-hello.png)\n\n执行`hp hello --help`，可以看到`hello`命令的帮助信息。\n\n![hello-help](https://github.com/hiproxy/hiproxy-plugin-example/raw/master/screenshot/hello-help.png)\n\n\u003cbr /\u003e\n\n### 页面\n\nhiproxy-plugin-example为hiproxy添加了一个页面，url为`/test`，使用`hiproxy start --port 8888`启动服务之后，可以访问\u003chttp://127.0.0.1:8888/test\u003e查看效果。\n\n\u003cbr /\u003e\n\n### 指令\n\nhiproxy-plugin-example为hiproxy添加了一个rewrite指令`add`来做加法运算。指令的作用域为`[global, domain, location]`，这个指令接收三个参数：\n\n* key：属性名称，用于把计算后得到的值赋值给当前作用于的props，比如：`$result`。\n* num1：第一个加数，比如：`12`。\n* num2：第二个加数，比如：`34`。\n\n之后，我们可以使用这个计算出来的值，比如：`set_header Calc-Result $result`。\n\n示例：\n\n```bash\n# rewrite rules\n# need plugin `hiproxy-plugin-example`\n\nset $gvar global-value;\n\ndomain test.hiproxy.io {\n  set $host test.hiproxy.io;\n\n  # hiproxy-plugin-example提供的指令\n  add $addresult 9999 90000;\n\n  location / {\n    proxy_pass http://127.0.0.1:8000/;\n\n    # hiproxy-plugin-example提供的指令\n    add $add_value 1600 88;\n\n    # 使用`add`计算的结果\n    set_header Add_Result $addresult;\n    set_header Test_Add_Value $add_value;\n\n    set_header Global_Var $gvar;    \n  }\n}\n```\n\n\u003cbr /\u003e\n\n## 开发者\n\n开发者开发新插件时，可以参考`hiproxy-plugin-example`。\n\n\u003cbr /\u003e\n\n### 插件结构\n\n插件作为一个独立的模块安装，入口文件需要导出一个对象，包括三个属性：\n\n* **commands**: `\u003cArray\u003e`，用来扩展`hiproxy`的CLI命令，数组中每个对象作为一个命令配置，具体配置见[命令配置](#command-config)。\n\n* **directives**: `\u003cArray\u003e`，用来扩展`hiproxy`的rewrite指令，数组中每个对象作为一个指令配置，具体配置见[指令配置](#directive-config)。\n\n* **routes**: `\u003cArray\u003e`，用来扩展`hiproxy`的页面路由，数组中每个对象作为一个路由配置，具体配置见[路由配置](#route-config)。\n\n\u003cbr /\u003e\n\n\u003ca name=\"command-config\"\u003e\u003c/a\u003e\n### 命令配置\n\n命令可以配置的内容为：`命令名称`、`描述`、`使用方法`、`处理函数`和`命令选项参数`。对应的字段为：\n\n* 命令名称（command）：`\u003cString\u003e`，比如：`'hello'`。\n* 描述信息（describe）：`\u003cString\u003e`，简单介绍命令的作用以及其他的信息，比如：`'A test command that say hello to you.'`。\n* 使用方法（usage）：`\u003cString\u003e`，命令的使用方法提示信息，比如：`'hello [--name \u003cname\u003e] [-xodD]'`。\n* 处理函数（fn）：`\u003cFunction\u003e`，执行命令时，调用的函数。函数调用时`this`值为命令行参数解析后的对象。\n* 命令选项（option）：`\u003cObject\u003e`，命令对应的选项，`key:value`形式。可以参考\u003chttps://github.com/hemsl/hemsl\u003e。\n\n一个完整的命令示例如下：\n\n```js\n{\n  command: 'hello',\n  describe: 'A test command that say hello to you.',\n  usage: 'hello [--name \u003cname\u003e] [-xodD]',\n  fn: function () {\n    var cliArgs = this;\n\n    console.log('Hi, welcome to use hiproxy example plugin');\n    if (cliArgs.name ) {\n      console.log('your name is', cliArgs.name.green);\n    }\n\n    if (cliArgs.age ) {\n      console.log('your are', cliArgs.age.green, 'years old');\n    }\n  },\n  options: {\n    'name \u003cname\u003e': {\n      alias: 'n',\n      describe: 'your name'\n    },\n    'age': {\n      alias: 'a',\n      describe: 'your age'\n    }\n  }\n}\n```\n\n\u003cbr /\u003e\n\n\u003ca name=\"directive-config\"\u003e\u003c/a\u003e\n\n### 指令配置\n\n命令可以配置的内容为：`指令名称`、`作用域`和`处理函数`。对应的字段为：\n\n* 指令名称（name）：`\u003cString\u003e`，比如：`'add'`。\n* 作用域（scope）：`\u003cArray\u003e`，指令对应的作用域，只有在这里指定的作用域里面才会执行。可选择的作用域为：`global`、`domain`、`location`、`request`和`response`。\n* 处理函数（fn）：`\u003cFunction\u003e`，执行指令时，调用的函数。\n\n一个完整的指令示例如下：\n\n```js\n{\n  name: 'add',\n  scope: ['global', 'domain', 'location'],\n  fn: function (key, a, b) {\n    var props = this.props;\n    var value = Number(a) + Number(b);\n\n    this.props[key] = value;\n  }\n}\n```\n\n\u003cbr /\u003e\n\n\u003ca name=\"route-config\"\u003e\u003c/a\u003e\n\n### 路由配置\n\n路由可以配置的内容为：`路由规则`和`渲染函数`。对应的字段为：\n\n* 路由规则（name）：`\u003cString\u003e`，比如：`'add'`。\n* 渲染函数（render）：`\u003cFunction\u003e`，渲染页面，接收三个参数：`(route, request, response)`，`route`为url匹配后的对象，细节可以查看\u003chttps://www.npmjs.com/package/url-pattern\u003e。\n\n一个完整的指令示例如下：\n\n```js\n{\n  route: '/test(/:pageName)',\n  render: function (route, request, response) {\n    response.writeHead(200, {\n      'Content-Type': 'text/html',\n      'Powder-By': 'hiproxy-plugin-example'\n    });\n\n    var serverInfo = {\n      route: route,\n      pageID: route.pageName,\n      time: new Date(),\n      serverState: {\n        http_port: global.hiproxyServer.httpPort,\n        https_port: global.hiproxyServer.httpsPort,\n        cliArgs: global.args,\n        process_id: process.pid\n      }\n    };\n\n    response.end('\u003cpre\u003e' + JSON.stringify(serverInfo, null, 4) + '\u003c/pre\u003e');\n  }\n}\n```\n\n\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhiproxy%2Fhiproxy-plugin-example","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhiproxy%2Fhiproxy-plugin-example","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhiproxy%2Fhiproxy-plugin-example/lists"}