3 个版本
0.1.2 | 2023年3月22日 |
---|---|
0.1.1 | 2023年2月19日 |
0.1.0 | 2023年2月19日 |
10 在 #nginx
每月下载量 30
40KB
1K SLoC
misc-conf
Nginx/Apache 配置的 Nom 解析器
特性
- 不同配置格式的统一 AST
ast::Directive
- 递归解析包含的配置
ast::DirectiveTrait::resolve_include
- 通过特定路径查询节点
ast::Directive::query
- 使用
Directive<S, Literal>
实现零拷贝字符串lexer::Literal
- 支持嵌入 lua 配置到 Nginx
用法
fn main() -> anyhow::Result<()> {
use misc_conf::apache::Apache;
use misc_conf::ast::*;
use misc_conf::nginx::Nginx;
let args = std::env::args().collect::<Vec<_>>();
let f = args[1].as_str();
println!("{f}");
let data = std::fs::read(f)?;
// for nginx configuration
if let Ok(res) = Directive::<Nginx>::parse(&data) {
println!("{res:#?}");
}
// for apache configuration
if let Ok(res) = Directive::<Apache>::parse(&data) {
println!("{res:#?}");
}
Ok(())
}
Nginx 示例
对于这样的 ngnix 配置
http {
server {
listen 80 ssl default_server;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Forwarded-for $remote_addr;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Request-Id $request_id;
proxy_set_header X-Forwarded-Proto $scheme;
# if ($host ~* ^www\.(.*)$) {
# set $host_wo_www $1;
# rewrite / https://${host_wo_www}$request_uri permanent;
# }
location / {
gzip on;
proxy_pass http://localhost:10001;
}
}
}
你将得到这样的 AST
Directive {
name: "http",
args: [],
children: [
Directive {
name: "server",
args: [],
children: [
Directive {
name: "listen",
args: [
"80",
"ssl",
"default_server",
],
},
Directive {
name: "proxy_set_header",
args: [
"Host",
"$host:$server_port",
],
},
Directive {
name: "proxy_set_header",
args: [
"X-Forwarded-for",
"$remote_addr",
],
},
Directive {
name: "proxy_set_header",
args: [
"X-Real-IP",
"$remote_addr",
],
},
Directive {
name: "proxy_set_header",
args: [
"X-Request-Id",
"$request_id",
],
},
Directive {
name: "proxy_set_header",
args: [
"X-Forwarded-Proto",
"$scheme",
],
},
Directive {
name: "location",
args: [
"/",
],
children: [
Directive {
name: "gzip",
args: [
"on",
],
},
Directive {
name: "proxy_pass",
args: [
"https://127.0.0.1:10001",
],
},
],
},
],
},
],
}
Apache 示例
对于这样的 apache 配置
<VirtualHost _default_:443>
SSLEngine on
ServerName localhost:443
SSLCertificateFile "${SRVROOT}/conf/ssl/server.crt"
SSLCertificateKeyFile "${SRVROOT}/conf/ssl/server.key"
DocumentRoot "${SRVROOT}/htdocs"
# DocumentRoot access handled globally in httpd.conf
CustomLog "${SRVROOT}/logs/ssl_request.log" \
"%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"
<Directory "${SRVROOT}/htdocs">
Options Indexes Includes FollowSymLinks
AllowOverride AuthConfig Limit FileInfo
Require all granted
</Directory>
</virtualhost>
你将得到这样的 AST
Directive {
name: "VirtualHost",
args: [
"_default_:443",
],
children: [
Directive {
name: "SSLEngine",
args: [
"on",
],
},
Directive {
name: "ServerName",
args: [
"localhost:443",
],
},
Directive {
name: "SSLCertificateFile",
args: [
"${SRVROOT}/conf/ssl/server.crt",
],
},
Directive {
name: "SSLCertificateKeyFile",
args: [
"${SRVROOT}/conf/ssl/server.key",
],
},
Directive {
name: "DocumentRoot",
args: [
"${SRVROOT}/htdocs",
],
},
Directive {
name: "CustomLog",
args: [
"${SRVROOT}/logs/ssl_request.log",
"\n",
"%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b",
],
},
Directive {
name: "Directory",
args: [
"${SRVROOT}/htdocs",
],
children: [
Directive {
name: "Options",
args: [
"Indexes",
"Includes",
"FollowSymLinks",
],
},
Directive {
name: "AllowOverride",
args: [
"AuthConfig",
"Limit",
"FileInfo",
],
},
Directive {
name: "Require",
args: [
"all",
"granted",
],
},
],
},
],
}
依赖项
~5–15MB
~172K SLoC