ToB企服应用市场:ToB评测及商务社交产业平台

标题: 15. 从零开始编写一个类nginx工具, 如果将nginx.conf转成yaml,toml,json会 [打印本页]

作者: 勿忘初心做自己    时间: 2023-11-10 13:41
标题: 15. 从零开始编写一个类nginx工具, 如果将nginx.conf转成yaml,toml,json会
wmproxy

wmproxy将用Rust实现http/https代理, socks5代理, 反向代理, 静态文件服务器,后续将实现websocket代理, 内外网穿透等, 会将实现过程分享出来, 感兴趣的可以一起造个轮子法
项目 ++wmproxy++

gite: https://gitee.com/tickbh/wmproxy
github: https://github.com/tickbh/wmproxy
了解三种格式

Json

它流行度极高,基本上每个程序员都和他打过交道。
多层级时,对齐和缩进不好控制,容易出错
Yaml

与JSON及TOML相比,结构比较紧凑
但相对用空格缩近,编写及拷贝时出错的机率比JSON及TOML高许多
Toml

TOML 旨在成为一个语义明显且易于阅读的最小化配置文件格式。
TOML 应该能很容易地被解析成各种语言中的数据结构。
它易于阅读和编写,语法灵活
与JSON配置相比,TOML在简洁性方面远远胜出;
与YAML配置相比,TOML在简洁性以及语法灵活性方面远远胜出。
三种格式测试数据的对比

我们用Rust的项目配置文件来做对比,为了展示所有的类型,格式有所变更。它以Toml来做配置文件,我们首先先展示toml的格式
内容包含创建者,创建时间,项目名称,项目依赖等信息,如果我们将其转化成可配置的JSON格式时
toml
  1. create="tickbh"
  2. create_time=2023-09-08T10:30:00Z
  3. [project]
  4. # 项目名称
  5. name="wmproxy"
  6. version="1.1"
  7. editor=2022
  8. # 项目依赖
  9. [project.dependencies]
  10. wenmeng={version = "0.1.21", default-features = false, features = ["std", "tokio"]}
  11. webparse={version = "0.1", default-features = false}
复制代码
行数12行,注释两行,全部顶格开头,原生支持时间格式
json
  1. {
  2.   "create": "tickbh",
  3.   "create_time": "2023-09-08T10:30:00.000Z",
  4.   "project": {
  5.     "name": "wmproxy",
  6.     "version": "1.1",
  7.     "editor": 2022,
  8.     "dependencies": {
  9.       "wenmeng": {
  10.         "version": "0.1.21",
  11.         "default-features": false,
  12.         "features": [
  13.           "std",
  14.           "tokio"
  15.         ]
  16.       },
  17.       "webparse": {
  18.         "version": "0.1",
  19.         "default-features": false
  20.       }
  21.     }
  22.   }
  23. }
复制代码
行数23行,层次的递进比较多,不容易对齐,无法注释,不支持时间格式
yaml
  1. create: tickbh
  2. create_time: 2023-09-08T10:30:00.000Z
  3. project:
  4.   # 项目名称
  5.   name: wmproxy
  6.   version: "1.1"
  7.   editor: 2022
  8.   # 项目依赖
  9.   dependencies:
  10.     wenmeng:
  11.       version: 0.1.21
  12.       default-features: false
  13.       features:
  14.         - std
  15.         - tokio
  16.     webparse:
  17.       version: "0.1"
  18.       default-features: false
复制代码
行数18行,注释两行,原生支持时间格式,到features这级行,深度相对较高,但是一眼看上去比json清晰
相对来说JSON比较不适合做比较复杂的配置文件,但VSCODE使用的拓展的JSON以支持注释功能。
接下来尝试将nginx.conf格式做转换

以下尝试的将
  1. http {
  2.     gzip on;
  3.     server {
  4.         listen 80;  #监听80的服务端口
  5.         server_name wm-proxy.com;  #监听的域名
  6.       
  7.         location /products {
  8.             proxy_pass http://127.0.0.1:8090/proxy;
  9.             add_header 'Access-Control-Allow-Credentials' 'true';
  10.             add_header 'Access-Control-Allow-Origin' '*';
  11.         }
  12.         
  13.         location / {
  14.             root wmproxy;
  15.             index index.html index.htm;
  16.         }
  17.     }
  18. }
复制代码
我们也模仿类似的结构,但是对于toml,yaml,json来说,都没有一个key两个值的,要么我们只能用对应的数组,此时我来先来初步重构类似的结构。以下我们以toml结构为例,我们分析table的级数有三级,最外层为http,中间层为server为数组,最内层为location也为数组,headers我们用之前提到过的mappings,用proxy开头来表示重写Reqeust,其它的来表示重写Response,文件系统我们用上节提到的file_server。
我们先定义http的table,他只有一个属性gzip为on
  1. [http]
  2. gzip="on"
复制代码
其次server为一个数组,那么我们可以如下定义,有绑定地址和server_name
  1. [[http.server]]
  2. bind_addr="127.0.0.1:80"
  3. server_name="wm-proxy.com"
复制代码
再然后location也为一个数组,定义如下
  1. [[http.server.location]]
  2. rule = "/products"
  3. reverse_proxy = "http://127.0.0.1:8090/proxy"
  4. headers = [
  5.   ["+", "Access-Control-Allow-Credentials", "true"],
  6.   ["+", "Access-Control-Allow-Origin", "*"]
  7. ]
  8. [[http.server.location]]
  9. rule = "/"
  10. file_server = { root="wmproxy", browse = true, index=["index.html", "index.htm"] }
复制代码
那么,最终的结构为如下:
  1. [http]
  2. gzip="on"[[http.server]]
  3. bind_addr="127.0.0.1:80"
  4. server_name="wm-proxy.com"[[http.server.location]]
  5. rule = "/products"
  6. reverse_proxy = "http://127.0.0.1:8090/proxy"
  7. headers = [
  8.   ["+", "Access-Control-Allow-Credentials", "true"],
  9.   ["+", "Access-Control-Allow-Origin", "*"]
  10. ]
  11. [[http.server.location]]
  12. rule = "/"
  13. file_server = { root="wmproxy", browse = true, index=["index.html", "index.htm"] }
复制代码
而yaml的格式结构如下:
  1. http:
  2.   gzip: on
  3.   server:
  4.     - bind_addr: 127.0.0.1:80
  5.       server_name: wm-proxy.com
  6.       location:
  7.         - rule: /products
  8.           reverse_proxy: http://127.0.0.1:8090/proxy
  9.           headers:
  10.             - - +
  11.               - Access-Control-Allow-Credentials
  12.               - "true"
  13.             - - +
  14.               - Access-Control-Allow-Origin
  15.               - "*"
  16.         - rule: /
  17.           file_server:
  18.             root: wmproxy
  19.             browse: true
  20.             index:
  21.               - index.html
  22.               - index.htm
复制代码
而json的格式结构如下:
  1. {
  2.   "http": {
  3.     "gzip": "on",
  4.     "server": [
  5.       {
  6.         "bind_addr": "127.0.0.1:80",
  7.         "server_name": "wm-proxy.com",
  8.         "location": [
  9.           {
  10.             "rule": "/products",
  11.             "reverse_proxy": "http://127.0.0.1:8090/proxy",
  12.             "headers": [
  13.               [
  14.                 "+",
  15.                 "Access-Control-Allow-Credentials",
  16.                 "true"
  17.               ],
  18.               [
  19.                 "+",
  20.                 "Access-Control-Allow-Origin",
  21.                 "*"
  22.               ]
  23.             ]
  24.           },
  25.           {
  26.             "rule": "/",
  27.             "file_server": {
  28.               "root": "wmproxy",
  29.               "browse": true,
  30.               "index": [
  31.                 "index.html",
  32.                 "index.htm"
  33.               ]
  34.             }
  35.           }
  36.         ]
  37.       }
  38.     ]
  39.   }
  40. }
复制代码
结语

在不考虑自建格式的情况下,如nginx的nginx.conf,如caddy的Caddyfile,将会同时兼容toml及yaml格式的配置文件。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4