drasi_source_sdk/
lib.rs

1// Copyright 2024 The Drasi Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::{env, error::Error};
16
17pub use async_stream::stream;
18use axum::async_trait;
19
20mod dapr_publisher;
21mod dapr_statestore;
22mod debug_publisher;
23mod memory_statestore;
24mod models;
25mod proxy;
26mod reactivator;
27mod telemetry;
28
29pub use debug_publisher::DebugPublisher;
30pub use memory_statestore::MemoryStateStore;
31pub use models::*;
32pub use proxy::*;
33pub use reactivator::*;
34use tokio::signal;
35
36#[async_trait]
37pub trait Publisher {
38    async fn publish(&self, change: SourceChange) -> Result<(), Box<dyn Error + Send + Sync>>;
39}
40
41#[async_trait]
42pub trait StateStore {
43    async fn get(&self, id: &str) -> Result<Option<Vec<u8>>, Box<dyn Error + Send + Sync>>;
44    async fn put(&self, id: &str, data: Vec<u8>) -> Result<(), Box<dyn Error + Send + Sync>>;
45    async fn delete(&self, id: &str) -> Result<(), Box<dyn Error + Send + Sync>>;
46}
47
48pub fn get_config_value(key: &str) -> Option<String> {
49    match env::var(key) {
50        Ok(s) => Some(s),
51        Err(_) => None,
52    }
53}
54
55async fn shutdown_signal() {
56    let ctrl_c = async {
57        signal::ctrl_c()
58            .await
59            .expect("failed to install Ctrl+C handler");
60    };
61
62    let terminate = async {
63        signal::unix::signal(signal::unix::SignalKind::terminate())
64            .expect("failed to install signal handler")
65            .recv()
66            .await;
67    };
68
69    let interrupt = async {
70        signal::unix::signal(signal::unix::SignalKind::interrupt())
71            .expect("failed to install signal handler")
72            .recv()
73            .await;
74    };
75
76    tokio::select! {
77        _ = ctrl_c => {},
78        _ = terminate => {},
79        _ = interrupt => {}
80    }
81}