• 使用TypeScriptToLua+openrestyluatypes+dockercompose 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

  • 相关阅读:
    Git fetch和git pull的区别
    git add 命令详解
    第1章——算法在计算机中的作用
    Mysql数据库中的计数器表实时更新
    Windows 7 搭建 nodejs、npm、express 环境
    设计模式之工厂模式
    设计模式之单例模式(Singleton Pattern)
    java实现合并两个已经排序的列表
    Spring+SpringMVC+Mybatis+Maven+CXF+WebService整合之服务端
    sqlserver乱码问题解决
  • 原文地址:https://www.cnblogs.com/rongfengliang/p/16212419.html
Copyright © 2020-2023  润新知