Docker容器

使用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

使用

  • 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 / {
           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 ")
    }
}
  • ts 配置
{
   "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

这篇关于使用TypeScriptToLua+openresty-lua-types+docker-compose nginx resty.ipmatcher 集成使用的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!