Connections
In order to build a useful application, Atmo needs to be able to connect to external resources. Currently, Atmo can connect to NATS and Redis, and upcoming releases will include additional types such as databases and more.
To create connections, add a connections section to your Directive. When Atmo starts up, it will establish the connections you've configured, and make them available to your application in a few different ways.

Stream sources

There are two available stream sources (NATS and Kafka) that can be used as sources for your handlers:
1
connections:
2
nats:
3
serverAddress: nats://localhost:4222
4
kafka:
5
brokerAddress: localhost:9092
Copied!
The NATS or Kafka connection is made available as a stream source:
1
- type: stream
2
source: nats
3
resource: user.created
4
steps:
5
- fn: record-signup
Copied!
By setting the source field of the handler, we tell Atmo to listen to that particular connection and handle messages it sends us. The resource field dictates which topic or subject the handler is listening to, which is useful for messaging systems such as NATS and Kafka.
Streams that use an external source can also use the respondTo field to set which topic or subject the response message is sent to:
1
- type: stream
2
source: nats
3
resource: user.login
4
steps:
5
- fn: record-login
6
respondTo: user.send-login-email
Copied!

Data sources

SQL databases and caches can be connected to Atmo to be made available to your Runnables using the Runnable API:
1
connections:
2
database:
3
type: postgresql
4
connectionString: env(DATABASE)
5
redis:
6
serverAddress: localhost:6379
Copied!
SQL database connections of type mysql and postgresql are available, and they are discussed in detail in the next section.
Redis connections are made available to Runnables utilizing the cache capability:
1
use suborbital::runnable::*;
2
use suborbital::req;
3
use suborbital::cache;
4
5
struct CacheGet{}
6
7
impl Runnable for CacheGet {
8
fn run(&self, _: Vec<u8>) -> Result<Vec<u8>, RunErr> {
9
let key = req::url_param("key");
10
11
let val = cache::get(key.as_str()).unwrap_or_default();
12
13
Ok(val)
14
}
15
}
Copied!
Last modified 2mo ago
Export as PDF
Copy link