使用TypeScriptToLua+openresty-lua-types+docker-compose nginx hashids 集成使用

以前我简单说明过hashids 的类型定义,以下是集成使用## 环境准备
详细介绍可以参考 https://www.cnblogs.com/rongfengliang/p/16210941.html

集成hashids

  • dockerfile 安装hashid
    使用luarocks
 
FROM openresty/openresty:1.21.4.1rc3-1-alpine-fat
RUN /usr/local/openresty/luajit/bin/luarocks install hashids

代码集成

  • 添加类型定义
    hashids.d.ts
 
declare module "hashids" {
    interface Hashids {
        encode(...args: number[]):string
        decode(hashid:string):number[]
    }
 
    interface myHashidsConstructor {
        new: (this: void, saltKey: string,length?:number) => Hashids;
    }
    var id: myHashidsConstructor
    export = {
         ...id
    }
 
}
  • ts 配置调整
"compilerOptions": {
     "outDir": "./lua_code",
     "types": ["openresty-lua-types"],
     "lib": ["esnext","DOM"],
     "module":"commonjs",
     "target": "esnext",
     "skipLibCheck": true,                                /* Skip type checking all .d.ts files. */
     "esModuleInterop": true,
     "moduleResolution": "node",
     "forceConsistentCasingInFileNames": true,   
     "allowSyntheticDefaultImports": true,
     "strict": true
   },
  • 代码集成
    还是以前的添加了hashids 生成的
    indexpage.ts
 
import myid = require("hashids")
let hashids = myid.new("dalongdemo",15)
ngx.say(hashids.encode(100,200,200))

运行

  • docker build
docker-compose build
  • 编译效果

 

 

  • 构建
yarn dev
  • 启动
docker-compose up -d
  • 效果

 

 

参考资料

https://luarocks.org/modules/leihog/hashids
https://www.cnblogs.com/rongfengliang/p/16210941.html
https://github.com/rongfengliang/typescript-to-lua-openresty-lua-types-docker-compose

posted on 2022-04-30 21:00  荣锋亮  阅读(56)  评论(0编辑  收藏  举报

导航