Today I’ll demonstrate another cool feature in OpenResty Edge. That is, how to use our rule-based domain-specific language, the Edge language, or Edgelang for short.

Introduction to Edgelang

The user can use Edgelang to write complex gateway rules for custom authentication, request and response rewriting, dynamic upstream routing, and more. The rules usually run much faster than hand-crafted Lua code, for example. The Edgelang compiler emits efficient native code running on gateway servers.

The Edgelang compiler is extremely efficient. It implements many advanced optimization techniques.

It supports combining regular expressions referenced by all the Edgelang rules into a single state machine. This way, it can immediately know which rules and which parts of those rules are matched by scanning the request data only once.

Add a page rule defined using Edgelang

It also supports combining constant string prefix and suffix patterns into single tree data structures across all the Edgelang rules.

Let’s go to a web console of OpenResty Edge. This is our sample deployment of the console. Every user has their own deployment.

Screenshot

We still use our continuing sample Edge application, test-edge.com.

Screenshot

Enter the application.

Screenshot

We have a page rule already defined in a previous tutorial.

Screenshot

This page rule sets up a reverse proxy to an upstream.

Screenshot

We can add our Edgelang code here.

Screenshot

Let’s add our first rule.

Screenshot

First it comes the rule condition part. When the URI is “/foo/”.

Screenshot

Then we use the arrow to complete our condition.

Screenshot

After the arrow, we specify our actions to take when the conditions are met.

Screenshot

This action initiates an HTTP redirect to the “/bar/” page.

Screenshot

Save the rule.

Screenshot

As we can see, the Edgelang specification consists of rules. And each rule comes with two basic parts:

Screenshot

a condition,

Screenshot

and a consequent consisting of actions.

Screenshot

The condition and the consequent are connected by an arrow sequence.

Screenshot

And the whole rule is terminated by a semicolon character.

Screenshot

This is just a simplest rule. We can add as many rules as we like. Each rule can also take many conditions AND’d or OR’d together. And a rule can take many actions in its consequent part.

Embeded image

Let’s make a new configuration release to push out our new changes, as always.

Screenshot

Click this button.

Screenshot

Ship it!

Screenshot

Our new release is now synchronized to all our gateway servers.

Screenshot

Now the new page rule has been pushed to all the gateway clusters and servers.

Embeded video

Embeded video

Embeded video

Our configuration changes do NOT require server reload, restart, or binary upgrade. So it’s very efficient and scalable.

Embeded image

Test

On the terminal, we send an HTTP request to a gateway server resolved by DNS via the curl command-line utility.

curl -I http://test-edge.com/bar/

Screenshot

Note the response header has no redirects.

Screenshot

Next, let’s send an HTTP request with /foo/ URI.

curl -I http://test-edge.com/foo/

Screenshot

Note that the request has been redirected.

Screenshot

It is being redirected to the /bar/ location. Our Edgelang rule works!

Screenshot

Let’s test another URI with the prefix “/foo/”.

Screenshot

The URI takes an extra suffix, “blah/”.

Screenshot

Send the request.

Screenshot

Note that the edgelang rule was not hit. This is because the our Edgelang condition needs to match “/foo/” exactly.

Screenshot

Modify the Edgelang definition of the page rule

Let’s edit the condition in our Edgelang rule to handle such cases properly.

Screenshot

Click the Edit button.

Screenshot

Delete the original condition.

Screenshot

Change it to matching the URI prefix “/foo/”.

Screenshot

Save the rule.

Screenshot

Make a new configuration release again.

Screenshot

Screenshot

Screenshot

Screenshot

Back to the terminal. Let’s send our previous test request again.

Screenshot

It still takes the “blah/” URI suffix.

Screenshot

Send the request.

Screenshot

It triggers a redirect and thus hit our Edgelang rule this time.

Screenshot

Next, let’s send an HTTP request without any extra URI suffixes.

curl -I http://test-edge.com/foo/

Screenshot

It is still a hit.

Screenshot

Define WAF rules using Edgelang

We could use Edgelang in many different places of the web console.

For instance, EdgeLang can be used to add custom rules for WAF, or the Web Application Firewall.

Screenshot

Screenshot

Create a new WAF rule.

Screenshot

Write Edgelang here.

Screenshot

Edgelang user manual

Our self-designed gateway DSL EdgeLang is a very powerful language with many features. It supports calling custom Lua modules and Lua code, or any .so dynamic link library. Its compiler supports cross-rules deep optimization.

Screenshot

Feel free to read our Edgelang User Manual to learn more.

Screenshot

There are many code examples.

Screenshot

And they explain various usage of Edgelang in great detail.

Screenshot

What is OpenResty Edge

OpenResty Edge is our all-in-one gateway software for microservices and distributed traffic architectures. It combines traffic management, private CDN construction, API gateway, security, and more to help you easily build, manage, and protect modern applications. OpenResty Edge delivers industry-leading performance and scalability to meet the demanding needs of high concurrency, high load scenarios. It supports scheduling containerized application traffic such as K8s and manages massive domains, making it easy to meet the needs of large websites and complex applications.

If you like this tutorial, please subscribe to this blog site and/or our YouTube channel. Thank you!

About The Author

Yichun Zhang (Github handle: agentzh), is the original creator of the OpenResty® open-source project and the CEO of OpenResty Inc..

Yichun is one of the earliest advocates and leaders of “open-source technology”. He worked at many internationally renowned tech companies, such as Cloudflare, Yahoo!. He is a pioneer of “edge computing”, “dynamic tracing” and “machine coding”, with over 22 years of programming and 16 years of open source experience. Yichun is well-known in the open-source space as the project leader of OpenResty®, adopted by more than 40 million global website domains.

OpenResty Inc., the enterprise software start-up founded by Yichun in 2017, has customers from some of the biggest companies in the world. Its flagship product, OpenResty XRay, is a non-invasive profiling and troubleshooting tool that significantly enhances and utilizes dynamic tracing technology. And its OpenResty Edge product is a powerful distributed traffic management and private CDN software product.

As an avid open-source contributor, Yichun has contributed more than a million lines of code to numerous open-source projects, including Linux kernel, Nginx, LuaJIT, GDB, SystemTap, LLVM, Perl, etc. He has also authored more than 60 open-source software libraries.