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
use http_req::request;
use serde_json::Value;
use std::collections::HashMap;

const LAMBDA_API_PREFIX: &str = "http://127.0.0.1:3002/api";
const LAMBDA_ENTRY_URL: &str = "http://127.0.0.1:8094/lambda";

extern "C" {
    fn is_listening() -> i32;
    fn get_flows_user(p: *mut u8) -> i32;
    fn get_flow_id(p: *mut u8) -> i32;
    fn get_event_query_length() -> i32;
    fn get_event_query(p: *mut u8) -> i32;
    fn get_event_body_length() -> i32;
    fn get_event_body(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);
    // fn redirect_to(p: *const u8, len: i32);
}

pub fn request_received() -> Option<(HashMap<String, Value>, Vec<u8>)> {
    unsafe {
        match is_listening() {
            // Calling register
            1 => {
                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", LAMBDA_API_PREFIX, flows_user, flow_id),
                    &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!("Lambda endpoint: {}/{}", LAMBDA_ENTRY_URL, l_key);
                        set_output(output.as_ptr(), output.len() as i32);
                    }
                    false => {
                        set_error_log(writer.as_ptr(), writer.len() as i32);
                    }
                }

                None
            }
            _ => message_from_request(),
        }
    }
}

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);
    }
}

fn message_from_request() -> Option<(HashMap<String, Value>, Vec<u8>)> {
    unsafe {
        let l = get_event_query_length();
        let mut event_query = Vec::<u8>::with_capacity(l as usize);
        let c = get_event_query(event_query.as_mut_ptr());
        assert!(c == l);
        event_query.set_len(c as usize);
        let event_query = serde_json::from_slice(&event_query).unwrap();

        let l = get_event_body_length();
        let mut event_body = Vec::<u8>::with_capacity(l as usize);
        let c = get_event_body(event_body.as_mut_ptr());
        assert!(c == l);
        event_body.set_len(c as usize);

        Some((event_query, event_body))
    }
}