系统相关
首页 > 系统相关> > 使用TypeScriptToLua+openresty-lua-types+docker-compose nginx resty.ipmatcher 集成使用

使用TypeScriptToLua+openresty-lua-types+docker-compose nginx resty.ipmatcher 集成使用

作者:互联网

还是基于现有的扩展开发的,主要是尝试下

主要内容

包含类型定义以及,安装扩展使用

环境准备

详细介绍可以参考 https://www.cnblogs.com/rongfengliang/p/16210941.html

类型定义

resty-ipmatcher.d.ts

declare module  "resty.ipmatcher" {
    interface IpMatcher {
        match(ip:string|any):LuaMultiReturn<[true|false|Record<string,string>, string]>;
        match_bin(ip:any):LuaMultiReturn<[true|false|Record<string,string>, string]>;
    } 
    interface myIpmatcherConstructor {
        new: (this: void,ips:string[]) =>  LuaMultiReturn<[IpMatcher, string]>;
        new_with_value: (this: void,ips:Record<string,any>) => LuaMultiReturn<[IpMatcher, string]>;
        /** @noSelf */
        parse_ipv4(ip:stirng):true|false
        /** @noSelf */
        parse_ipv6(ip:stirng):true|false
    }
    var ipmatcher:myIpmatcherConstructor
    export = {
        ...ipmatcher
    }
}

基础镜像

FROM openresty/openresty:1.21.4.1rc3-1-alpine-fat
RUN /usr/local/openresty/luajit/bin/luarocks install hashids
RUN /usr/local/openresty/luajit/bin/luarocks install lua-resty-ipmatcher

使用

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 / {
           access_by_lua_file /opt/lua/acc.lua;
           content_by_lua_file /opt/lua/indexpage.lua;
        }
    }
}

ts 代码

import ipmatch = require("resty.ipmatcher")
let ips = {
    "192.168.17.101":{
       info:"demo"
    },
    "192.168.17.102": {
        info:"test"
    }
}
let ipv4 = ipmatch.parse_ipv4("127.0.0.1")
console.log(ipv4)
 
let [ip2, err2] = ipmatch.new_with_value(ips)
 
if (err2) {
    ngx.say(err2)
    ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
}
 
let [ok2, errMsg2] = ip2.match("192.168.17.102")
 
if (errMsg2) {
    ngx.say(errMsg2)
    ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
}
if (ok2) {
    if(typeof ok2 === "object") {
        ngx.say("is match ",ok2.info)
    }
    if(typeof ok2 ==="boolean") {
        ngx.say("is match ")
    }
}
{
   "include": [
     "src/*/*"
    ],
   "compilerOptions": {
     "outDir": "./lua_code",
     "types": ["openresty-lua-types","lua-types/jit"],
     "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
   },
   "tstl": {
     "noHeader": true,  
     "noImplicitSelf":true, 
     "buildMode": "library",
     "luaTarget": "JIT",
     "noResolvePaths":["hashids","resty.ipmatcher"]
   }
 }

效果

 

 

说明

TypeScriptToLua+openresty-lua-types 很方便,提供越多的类型定义,我们开发就越简单

参考资料

https://github.com/api7/lua-resty-ipmatcher
https://typescripttolua.github.io/docs/advanced/writing-declarations#declare-keyword
https://github.com/andrei-markeev/openresty-lua-types
https://www.npmjs.com/package/lua-types
https://github.com/rongfengliang/typescript-to-lua-openresty-lua-types-docker-compose

标签:resty,compose,ipmatcher,ngx,lua,openresty,true,types
来源: https://www.cnblogs.com/rongfengliang/p/16212419.html