使用TypeScriptToLua+openresty-lua-types+docker-compose 开发nginx 模块服务
以下核心是一个演示,对于是如何集成的,以前大致有介绍过,通过此主要别大家介绍下如果集成使用
运行环境准备
需要先安装nodejs,docker-compose
- 参考结构
具体的可以查看github 源码,以下只是示例
- 运行环境简单说明
version: '3'
services:
op:
build: ./
volumes:
- ./nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf // nginx 配置
- ./lua_code/:/opt/lua/ // 通过ts 编译的lua 代码
ports:
- 80:80
- dockerfile
比较简单,独立出来是方便后续安装一些lua 模块(opm 或者luarocks),初始时候只包含基础镜像
FROM openresty/openresty:1.21.4.1rc3-1-alpine-fat
- nginx 配置文件
比较简单,只博阿含核心一些配置,其他的没有
user root;
master_process off;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type text/html;
lua_code_cache off;
lua_package_path '/opt/lua/?.lua;;';
real_ip_header X-Forwarded-For;
resolver 114.114.114.114;
server {
listen 80;
charset utf-8;
default_type text/html;
location / {
content_by_lua_file /opt/lua/indexpage.lua; // 引用我们编译的lua 文件
}
}
}
nodejs TypeScriptToLua 环境
- package.json
{
name: op-nginx-lua,
version: 1.0.0,
main: index.js,
license: MIT,
scripts: {
build: rm -rf lua_code && tstl,
dev: tstl --watch
},
devDependencies: {
openresty-lua-types: ^1.0.4, // 引用openresty-lua-types 加速lua 模块编写
typescript: ^4.6.4,
typescript-to-lua: ^1.4.3 // 编译转换工具
}
}
tsconfig.json
{
include: [
src/*/*
],
compilerOptions: {
outDir: ./lua_code,
target:ESNext,
types: [openresty-lua-types],
lib: [esnext,DOM],
moduleResolution: node,
allowSyntheticDefaultImports: true,
strict: true
},
tstl: {
noHeader: true, // 去掉头
noImplicitSelf:true, // 禁用self
buildMode: library,
luaTarget: JIT
}
}
参考使用
对于基于ts 开发的模块走在src 目录下,编译之后会放到lua_code 中
- indexpage.ts 开发
比如我们开发一个简单的输出
indexpage.ts
ngx.say(dalongdemo)
自动提示效果
- 运行时效果
docker-compose up -d
修改代码查看效果
注意运行命令调整下
yarn dev
说明
以上时简单的集成TypeScriptToLua+openresty-lua-types+docker 进行openresty lua 模块的开发,实际上我们时需要很对的类型定义,才能更加流利的编写
lua 模块,当然也必须了解openresty 的机制,以及提供的能力,但是基于此进行openresty lua 模块的开发,可以很大程度确保我们代码的可靠性同时,可以
在编码的时候提高效率,很好的实现lua 模块的共享(可以利用npm 包实现共享),越来越多的人共享openresty 模块的types定义,我们开发效率就越高
参考资料
https://github.com/openresty/test-nginx
https://github.com/TypeScriptToLua/TypeScriptToLua
https://github.com/andrei-markeev/openresty-lua-types
https://github.com/openresty/lua-nginx-module
https://github.com/rongfengliang/typescript-to-lua-openresty-lua-types-docker-compose