1. <dd id="erndk"></dd>
                1. 使用TypeScriptToLua+openresty-lua-types+docker-compose nginx resty.ipmatcher 集成使用

                  互聯網 2022/5/2 7:16:18

                  還是基于現有的擴展開發的,主要是嘗試下 主要內容 包含類型定義以及,安裝擴展使用 環境準備 詳細介紹可以參考 https://www.cnblogs.com/rongfengliang/p/16210941.html 類型定義 resty-ipmatcher.d.tsdeclare module "resty.ipmatcher" {interface IpMatche…

                  還是基于現有的擴展開發的,主要是嘗試下

                  主要內容

                  包含類型定義以及,安裝擴展使用

                  環境準備

                  詳細介紹可以參考 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

                  隨時隨地學軟件編程-關注百度小程序和微信小程序
                  關于找一找教程網

                  本站文章僅代表作者觀點,不代表本站立場,所有文章非營利性免費分享。
                  本站提供了軟件編程、網站開發技術、服務器運維、人工智能等等IT技術文章,希望廣大程序員努力學習,讓我們用科技改變世界。
                  [使用TypeScriptToLua+openresty-lua-types+docker-compose nginx resty.ipmatcher 集成使用]http://www.yachtsalesaustralia.com/tech/detail-318706.html

                  贊(0)
                  關注微信小程序
                  程序員編程王-隨時隨地學編程

                  掃描二維碼或查找【程序員編程王】

                  可以隨時隨地學編程啦!

                  技術文章導航 更多>
                  国产在线拍揄自揄视频菠萝

                        1. <dd id="erndk"></dd>