Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Sample gateway written in Ballerina

NotificationsYou must be signed in to change notification settings

sameerajayasoma/app-gateway

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

76 Commits
 
 
 
 
 
 

Repository files navigation

A gateway is a service designed to manage and improve traffic to and from web applications and apis by performing various functions such as load balancing, SSL/TLS offloading, security, traffic shaping, and caching.

This repository includes a sample gateway written in Ballerina that can be customized to route and handle one or more applications and apis. Additionally, it offers the capability to execute policy agents for each request and response, which can be configured as plugins within the gateway. This enables fine-grained control over traffic and the enforcement of policies based on specific attributes of each request and response.

Build and run the gateway

You need to use Ballerina 2201.5.0 (Swan Lake Update 5) or higher version to run the program.

% cd gateway% bal pack;bal push --repository=local% cd ../xyz-gateway% bal runCompiling source        xyz/xyz_gateway:0.1.0Running executable

Sample URLs

Try out the gateway with following sample commands.

curl localhost:9090/api/xyz-entitlement-service/curl localhost:9090/api/xyz-entitlement-service/metricscurl localhost:9090/api/xyz-navbarcurl localhost:9090/app/xyz-resource-cache

Implementation Notes

  • gateway - directory contains theballerinax/gateway library package.
  • xyz-gateway - directory contains a sample gateway written usingballerinax/gateway library.

Gateway API

importballerinax/gateway;importballerina/http;http:ListenerConfigurationlistenerConfig= {};listenerhttp:ListenerhttpListener=checknew (9090,listenerConfig);publicfunction main()returnserror? {gateway:registerPlugin(PLUGIN_ID_ADD_ACCESS_TOKEN,pluginConfig =>newAddAccessTokenPlugin(pluginConfig));gateway:registerPlugin(PLUGIN_ID_ADD_HEADERS,pluginConfig =>newAddHeadersPlugin(pluginConfig));gateway:registerPlugin(PLUGIN_ID_CORS_HEADERS,pluginConfig =>newCORSHeaderPlugin(pluginConfig));gateway:registerPlugin(PLUGIN_ID_NETWORK_CONTROL,pluginConfig =>newNetworkControlPlugin(pluginConfig));gateway:registerPlugin(PLUGIN_ID_REMOVE_HEADERS,pluginConfig =>newRemoveHeadersPlugin(pluginConfig));gateway:registerPlugin(PLUGIN_ID_ALWAYS_ABORT_PLUGIN,pluginConfig =>newAlwaysAbortPlugin(pluginConfig));checkgateway:'start(httpListener);}

Gateway Configuration

This gateway can be configured to support apps and api using aTOML file as follows. There exists a sample file calledConfig.toml inside thexyz-gateway directory. SeeConfigure Ballerina Programs for more details.

# 1) API with no configured plugins[[ballerinax.gateway.apps]]basePath ="/api/xyz-service"endpointUrl ="https://run.mocky.io/v3/84643c67-6ddb-4cf1-8141-f637154c9520"# 2) Application with request plugins[[ballerinax.gateway.apps]]basePath ="/app/xyz-resource-cache"endpointUrl ="https://run.mocky.io/v3/34a9aeba-0b71-4fac-8451-b122c50cce45"[[ballerinax.gateway.apps.requestPlugins]]id ="AddAccessTokenPlugin"# 3) API with request and response plugins[[ballerinax.gateway.apps]]basePath ="/api/xyz-entitlement-service"endpointUrl ="https://run.mocky.io/v3/6613f69c-65cf-44d4-b29c-7887f21cfd59"[[ballerinax.gateway.apps.requestPlugins]]id ="NetworkControlPlugin"[[ballerinax.gateway.apps.responsePlugins]]id ="RemoveHeadersPlugin"# 4) Application with plugins having configs[[ballerinax.gateway.apps]]basePath ="/app/xyz-foo-bar"endpointUrl ="https://run.mocky.io/v3/34a9aeba-0b71-4fac-8451-b122c50cce45"[[ballerinax.gateway.apps.requestPlugins]]id ="NetworkControlPlugin"config = {level ="2"}# 5) Global request and response plugins[[ballerinax.gateway.requestPlugins]]id ="AddHeadersPlugin"config = {appId ="xyz-app"}[[ballerinax.gateway.responsePlugins]]id ="RemoveHeadersPlugin"config = {appId ="xyz-app"}

Java Interoperability

The pluginHazelcastMapClientPlugin is written to communicate with an existing Hazelcast cluster. This sample plugin is written to demonstrate the Java interoperability feature in Ballerina.

bal bindgen --public  -mvn com.hazelcast:hazelcast:5.2.3 --output modules/hazelcast com.hazelcast.client.HazelcastClient com.hazelcast.core.HazelcastInstance com.hazelcast.map.IMap java.util.Set

TODO

  • Refactor the codebase with custom Ballerina error types.
  • Improve error handling and logic througout the codebase.
  • Get rid of concurrency related warnings

About

Sample gateway written in Ballerina

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Ballerina100.0%

[8]ページ先頭

©2009-2025 Movatter.jp