Skip to content

Adaptive Radix Trees implemented in Lua for OpenResty

License

Notifications You must be signed in to change notification settings

yang282441848/lua-resty-radixtree

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

35 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Name

This is Lua-Openresty implementation library base on FFI for rax.

Build Status License

Table of Contents

Status

This repository is an experimental.

Synopsys

 location / {
     content_by_lua_block {
        local radix = require("resty.radixtree")
        local rx = radix.new({
            {
                path = "/aa",
                metadata = "metadata /aa",
                host = "foo.com",
                method = {"GET", "POST"},
                remote_addr = "127.0.0.1",
            },
            {
                path = "/bb*",
                metadata = "metadata /bb",
                host = {"*.bar.com", "gloo.com"},
                method = {"GET", "POST", "PUT"},
                remote_addr = "fe80:fe80::/64",
                vars = {"arg_k", "v"},
            }
        })

        -- should hit
        ngx.say(rx:match("/aa", {host = "foo.com",
                                 method = "GET",
                                 remote_addr = "127.0.0.1",
                                 vars = ngx.var}))
     }
 }

Back to TOC

Methods

new

syntax: rx, err = radix:new(routes)

The routes is a array table, like { {...}, {...}, {...} }, Each element in the array is a route, which is a hash table.

The attributes of each element may contain these:

name option description
path required client request uri, the default is a full match. But if the end of the path is *, it means that this is a prefix path. For example /foo*, it'll match /foo/bar or /foo/glo/grey etc.
metadata option Will return this field if using rx:match to match route.
handler option Will call this function using rx:dispatch to match route.
host option Client request host, not only supports normal domain name, but also supports wildcard name, both foo.com and *.foo.com are valid.
remote_addr option Client remote address like 192.168.1.100, and we can use CIDR format, eg 192.168.1.0/24.
methods option It's an array table, we can put one or more method names together. Here is the valid method name: "GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS".
vars option It is a non-empty array, each pair of elements, representing the name and value. eg: {var_name, expect_val, ...}

Back to TOC

match

syntax: metadata = rx:match(path, opts)

  • path: client request uri.
  • opts: a Lua tale (optional).
    • method: optional, method name of client request.
    • host: optional, client request host, not only supports normal domain name, but also supports wildcard name, both foo.com and *.foo.com are valid.
    • remote_addr: optional, client remote address like 192.168.1.100, and we can use CIDR format, eg 192.168.1.0/24.
    • vars: optional, a Lua table to fetch variable, default value is ngx.var to fetch Ningx builtin variable.

Matchs the route by method, path and host, and return metadata if successful.

local metadata = rx:match(ngx.var.uri, {...})

Back to TOC

dispatch

syntax: ok = rx:dispatch(path, opts, ...)

  • path: client request uri.
  • opts: a Lua tale (optional).
    • method: optional, method name of client request.
    • host: optional, client request host, not only supports normal domain name, but also supports wildcard name, both foo.com and *.foo.com are valid.
    • remote_addr: optional, client remote address like 192.168.1.100, and we can use CIDR format, eg 192.168.1.0/24.
    • vars: optional, a Lua table to fetch variable, default value is ngx.var to fetch Ningx builtin variable.

Dispatchs the route by method, path and host, and call handler function if successful.

local metadata = rx:dispatch(ngx.var.uri, {...})

Back to TOC

Install

Compile and install

make install

About

Adaptive Radix Trees implemented in Lua for OpenResty

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C 67.7%
  • Perl 13.7%
  • Lua 12.6%
  • Other 4.9%
  • Makefile 1.1%