1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//! Make a flow function triggerable from webhooks in [Flows.network](https://flows.network)
//!
//! # Quick Start
//!
//! To get started, let's write a very tiny flow function.
//!
//! ```rust
//! use webhook_flows::{create_endpoint, request_handler, send_response};
//!
//! #[no_mangle]
//! #[tokio::main(flavor = "current_thread")]
//! pub async fn on_deploy() {
//!     create_endpoint().await;
//! }
//!
//! #[request_handler]
//! async fn handler(_headers: Vec<(String, String)>, _subpath: String, _qry: HashMap<String, Value>, _body: Vec<u8>) {
//!     send_response(
//!         200,
//!         vec![(String::from("content-type"), String::from("text/html"))],
//!         "ok".as_bytes().to_vec(),
//!     );
//! }
//! ```
//!
//! When a new request is received the function `handler` decorated by macro [request_handler] will be called and [send_response()] is used to make the response.

use http_req::request;
use lazy_static::lazy_static;
use serde_json::Value;

pub use webhook_flows_macros::*;

pub use http::Method;

pub mod route;

lazy_static! {
    static ref WEBHOOK_API_PREFIX: String = String::from(
        std::option_env!("WEBHOOK_API_PREFIX").unwrap_or("https://webhook.flows.network/api")
    );
}
const WEBHOOK_ENTRY_URL: &str = "https://code.flows.network/webhook";

extern "C" {
    fn get_flows_user(p: *mut u8) -> i32;
    fn get_flow_id(p: *mut u8) -> i32;
    fn set_error_log(p: *const u8, len: i32);
    fn set_output(p: *const u8, len: i32);
    fn set_response(p: *const u8, len: i32);
    fn set_response_headers(p: *const u8, len: i32);
    fn set_response_status(status: i32);
}

/// Register a callback closure with the query and body of the request for the webhook service.
///
/// The query is formed as a [HashMap]. For example, say the entrypoint of the webhook service is
/// `https://code.flows.network/webhook/6rtSi9SEsC?param=hello`
/// then the query will look like `HashMap("param", Value::String("hello"))`
///
/// The body is the raw bytes of the request body.
pub async fn create_endpoint() {
    unsafe {
        let mut flows_user = Vec::<u8>::with_capacity(100);
        let c = get_flows_user(flows_user.as_mut_ptr());
        flows_user.set_len(c as usize);
        let flows_user = String::from_utf8(flows_user).unwrap();

        let mut flow_id = Vec::<u8>::with_capacity(100);
        let c = get_flow_id(flow_id.as_mut_ptr());
        if c == 0 {
            panic!("Failed to get flow id");
        }
        flow_id.set_len(c as usize);
        let flow_id = String::from_utf8(flow_id).unwrap();

        let mut writer = Vec::new();
        let res = request::get(
            format!(
                "{}/{}/{}/listen?handler_fn={}",
                WEBHOOK_API_PREFIX.as_str(),
                flows_user,
                flow_id,
                "__webhook__on_request_received"
            ),
            &mut writer,
        )
        .unwrap();

        match res.status_code().is_success() {
            true => {
                let listener: Value = serde_json::from_slice(&writer).unwrap();
                let l_key = listener["l_key"].as_str().unwrap();
                let output = format!("Webhook endpoint: {}/{}", WEBHOOK_ENTRY_URL, l_key);
                set_output(output.as_ptr(), output.len() as i32);
            }
            false => {
                set_error_log(writer.as_ptr(), writer.len() as i32);
            }
        }
    }
}

/// Set the response for the webhook service.
pub fn send_response(status: u16, headers: Vec<(String, String)>, body: Vec<u8>) {
    let headers = serde_json::to_string(&headers).unwrap_or_default();
    unsafe {
        set_response_status(status as i32);
        set_response_headers(headers.as_ptr(), headers.len() as i32);
        set_response(body.as_ptr(), body.len() as i32);
    }
}