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

  • 相关阅读:
    datatable里的元素
    ajax
    myeclipse编译项目Webcontent下不生成classes文件
    oracle忘记密码
    zuul的多版本配置
    ribbon灰度发布极简方式
    ribbon灰度发布
    使用网关zuul完成灰度发布
    mybatis-generator代码生成器使用(二)
    mybatis-generator代码生成器使用(一)
  • 原文地址:https://www.cnblogs.com/rongfengliang/p/16212419.html
Copyright © 2020-2023  润新知