-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[#147] AWS Lambda용 부팅 옵션 추가
- Loading branch information
Showing
16 changed files
with
469 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,4 +20,6 @@ Cargo.lock | |
|
||
test.json | ||
|
||
.vscode | ||
.vscode | ||
|
||
*.zip |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "rupring" | ||
version = "0.11.0" | ||
version = "0.12.0" | ||
edition = "2021" | ||
license = "MIT" | ||
authors = ["myyrakle <[email protected]>"] | ||
|
@@ -36,4 +36,10 @@ features = [ | |
] | ||
|
||
[target.'cfg(target_os = "linux")'.dependencies] | ||
signal-hook = "0.3.17" | ||
signal-hook = "0.3.17" | ||
|
||
[features] | ||
default = [] | ||
|
||
full = ["aws-lambda"] | ||
aws-lambda = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
use std::{collections::HashMap, str::FromStr}; | ||
|
||
use hyper::Uri; | ||
use serde::Serialize; | ||
|
||
use crate::utils; | ||
|
||
// Reference: https://docs.aws.amazon.com/ko_kr/lambda/latest/dg/runtimes-api.html | ||
fn get_aws_lambda_runtime_api() -> Option<String> { | ||
std::env::var("AWS_LAMBDA_RUNTIME_API").ok() | ||
} | ||
|
||
#[derive(Debug, Default, Clone)] | ||
pub struct LambdaRequestContext { | ||
pub aws_request_id: String, | ||
pub response_body: String, | ||
} | ||
|
||
pub async fn get_request_context() -> anyhow::Result<LambdaRequestContext> { | ||
let aws_lambda_runtime_api = match get_aws_lambda_runtime_api() { | ||
Some(api) => api, | ||
None => return Err(anyhow::anyhow!("AWS_LAMBDA_RUNTIME_API is not set")), | ||
}; | ||
|
||
let url = Uri::from_str( | ||
format!("http://{aws_lambda_runtime_api}/2018-06-01/runtime/invocation/next").as_str(), | ||
)?; | ||
|
||
let mut headers = HashMap::new(); | ||
headers.insert(hyper::header::HOST, "localhost".to_owned()); | ||
|
||
let response = | ||
utils::hyper::send_http_request(url, hyper::Method::GET, headers, "".to_owned()).await?; | ||
|
||
let mut request_context = LambdaRequestContext::default(); | ||
|
||
if let Some(aws_request_id) = response.headers.get("Lambda-Runtime-Aws-Request-Id") { | ||
request_context.aws_request_id = aws_request_id.to_str()?.to_string(); | ||
} | ||
|
||
request_context.response_body = response.body; | ||
|
||
Ok(request_context) | ||
} | ||
|
||
#[derive(Debug, Default, Clone, Serialize)] | ||
pub struct LambdaReponse { | ||
#[serde(rename = "statusCode")] | ||
pub status_code: u16, | ||
#[serde(rename = "headers")] | ||
pub headers: HashMap<String, String>, | ||
#[serde(rename = "body")] | ||
pub body: String, | ||
} | ||
|
||
pub async fn send_response_to_lambda( | ||
aws_request_id: String, | ||
response: LambdaReponse, | ||
) -> anyhow::Result<()> { | ||
let response = serde_json::to_string(&response)?; | ||
|
||
let aws_lambda_runtime_api = match get_aws_lambda_runtime_api() { | ||
Some(api) => api, | ||
None => return Err(anyhow::anyhow!("AWS_LAMBDA_RUNTIME_API is not set")), | ||
}; | ||
|
||
let url = Uri::from_str( | ||
format!( | ||
"http://{aws_lambda_runtime_api}/2018-06-01/runtime/invocation/{aws_request_id}/response" | ||
) | ||
.as_str(), | ||
)?; | ||
|
||
let mut headers = HashMap::new(); | ||
headers.insert(hyper::header::HOST, "localhost".to_owned()); | ||
|
||
let _ = utils::hyper::send_http_request(url, hyper::Method::POST, headers, response).await?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod aws_lambda; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
pub fn compress_with_gzip(body: &[u8]) -> anyhow::Result<Vec<u8>> { | ||
use std::io::Write; | ||
|
||
let mut encoder = flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::default()); | ||
encoder.write_all(body)?; | ||
let compressed = encoder.finish()?; | ||
|
||
Ok(compressed) | ||
} | ||
|
||
pub fn compress_with_deflate(body: &[u8]) -> anyhow::Result<Vec<u8>> { | ||
use std::io::Write; | ||
|
||
let mut encoder = | ||
flate2::write::DeflateEncoder::new(Vec::new(), flate2::Compression::default()); | ||
encoder.write_all(body)?; | ||
let compressed = encoder.finish()?; | ||
|
||
Ok(compressed) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.