#router #atc #dsl

atc-router

Versatile DSL based rule matching engine used by the Kong API Gateway

8 stable releases

1.6.0 Jan 24, 2024
1.5.1 Jan 22, 2024
1.4.1 Dec 27, 2023
1.3.1 Nov 2, 2023
1.0.5 Apr 23, 2023

#208 in Database interfaces

Download history 50/week @ 2023-10-30 4/week @ 2023-11-06 7/week @ 2023-11-13 9/week @ 2023-11-20 12/week @ 2023-11-27 4/week @ 2023-12-04 4/week @ 2023-12-18 32/week @ 2023-12-25 7/week @ 2024-01-01 2/week @ 2024-01-08 31/week @ 2024-01-15 71/week @ 2024-01-22 30/week @ 2024-01-29 4/week @ 2024-02-05 40/week @ 2024-02-12

145 downloads per month

Apache-2.0

83KB
2K SLoC

Rust 1.5K SLoC // 0.0% comments Lua 358 SLoC // 0.0% comments Pest 34 SLoC

Name

ATC Router library for Kong.

Table of Contents

Semantics

At the core of the library, ATC Router is a DSL that supports simple predicate and logical combinations between the predicates.

Each data referred in the DSL has a type, the type can be one of the following:

  • "String" - a UTF-8 string value
  • IpCidr - an IP address range in CIDR format
  • IpAddr - a single IP address that can be checked against an IpCidr
  • Int - an 64-bit signed integer

Please refer to the documentation on Kong website for how the language is used in practice.

APIs

resty.router.schema

new

syntax: s = schema.new()

context: any

Create a new schema instance that can later be used by router and context.

Back to TOC

add_field

syntax: res, err = s:add_field(field, field_type)

context: any

Adds the field named field into the schema. Type can be one of the ones mentioned in the Semantics section above.

If an error occurred, nil and a string describing the error will be returned.

Back to TOC

get_field_type

syntax: typ, err = s:get_field_type(field)

context: any

Gets the field type from the schema.

If an error occurred, nil and a string describing the error will be returned.

Back to TOC

resty.router.router

new

syntax: r = router.new(schema)

context: any

Create a new router instance that can later be used for performing matches. schema must refer to an existing schema instance.

Back to TOC

add_matcher

syntax: res, err = r:add_matcher(priority, uuid, atc)

context: any

Add a matcher to the router. priority is a 64-bit unsigned integer that instructs the priority for which the matchers should be evaluated. uuid is the string representation of the UUID of the matcher which will be used later for match results. atc is the matcher written in ATC DSL syntax.

If an error occurred or the matcher has syntax/semantics errors, nil and a string describing the error will be returned.

Back to TOC

remove_matcher

syntax: res, err = r:remove_matcher(uuid)

context: any

Remove matcher with uuid from the router.

Returns true if the matcher has successfully been removed. false if the matcher does not exist.

Back to TOC

execute

syntax: res, err = r:execute(context)

context: any

Executes the router against value provided inside the context instance.

context must use the same schema as the router, otherwise Lua error will be thrown.

Returns true if at least one matcher produced a valid match. false if the none of the matcher matched.

Back to TOC

get_fields

syntax: res = r:get_fields()

context: any

Returns the currently used field names by all matchers inside the router as an Lua array. It can help reduce unnecessarily producing values that are not actually used by the user supplied matchers.

Back to TOC

validate

syntax: fields, err = router.validate(schema, expr)

context: any

Validates an expression against a given schema.

Returns the fields used in the provided expression when the expression is valid. If the expression is invalid, nil and a string describing the reason will be returned.

Back to TOC

resty.router.context

new

syntax: c = context.new(schema)

context: any

Create a new context instance that can later be used for storing contextual information. for router matches. schema must refer to an existing schema instance.

Back to TOC

add_value

syntax: res, err = c:add_value(field, value)

context: any

Provides value for field inside the context.

Returns true if field exists and value has successfully been provided.

If an error occurred, nil and a string describing the error will be returned.

Back to TOC

get_result

syntax: uuid, matched_value, captures = c:get_result(matched_field)

context: any

After a successful router match, gets the match result from the context.

If matched_field is provided, then matched_value will be returned with the value matched by the specified field. If matched_field is nil or field did not match, then nil is returned for matched_value.

If the context did not contain a valid match result, nil is returned.

Otherwise, the string UUID, value matching field matched_field and regex captures from the matched route are returned.

Back to TOC

reset

syntax: c:reset()

context: any

This resets context c without deallocating the underlying memory so the context can be used again as if it was just created.

Back to TOC

Copyright and license

Copyright © 2022-2023 Kong, Inc.

Licensed under the Apache License, Version 2.0.

Files in the project may not be copied, modified, or distributed except according to those terms.

Back to TOC

Dependencies

~4.5–6.5MB
~122K SLoC