Skip to content

Commit

Permalink
Merge pull request #182 from myyrakle/fix/#178
Browse files Browse the repository at this point in the history
[#178] 누락된 부분 구현
  • Loading branch information
myyrakle authored Dec 12, 2024
2 parents 067159f + 3d906ac commit 5f8c06a
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 17 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ test.json

.vscode

*.zip
*.zip

*.http
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# rupring

![](https://img.shields.io/badge/language-Rust-red) ![](https://img.shields.io/badge/version-0.12.0-brightgreen) [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/myyrakle/rupring/blob/master/LICENSE)
![](https://img.shields.io/badge/language-Rust-red) ![](https://img.shields.io/badge/version-0.12.1-brightgreen) [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/myyrakle/rupring/blob/master/LICENSE)

Spring Comes to Rust

Expand Down
2 changes: 1 addition & 1 deletion rupring/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rupring"
version = "0.12.0"
version = "0.12.1"
edition = "2021"
license = "MIT"
authors = ["myyrakle <[email protected]>"]
Expand Down
74 changes: 67 additions & 7 deletions rupring/src/core/bootings/aws_lambda.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use std::{collections::HashMap, str::FromStr};

use hyper::Uri;
use serde::Serialize;
use hyper::{
header::{HeaderName, HeaderValue},
HeaderMap, Uri,
};
use serde::{Deserialize, Serialize};

use crate::utils;

Expand All @@ -11,15 +14,72 @@ fn get_aws_lambda_runtime_api() -> Option<String> {
}

#[derive(Debug, Default, Clone)]
pub struct LambdaRequestContext {
pub struct LambdaRequestEvent {
pub aws_request_id: String,
pub trace_id: String,
pub status_code: u16,

pub event_payload: String,
pub event_payload: LambdaRequestPayload,
}

#[derive(Debug, Default, Clone, Deserialize)]
pub struct LambdaRequestHTTP {
#[serde(rename = "method")]
pub method: String,
}

#[derive(Debug, Default, Clone, Deserialize)]
pub struct LambdaRequestContext {
#[serde(rename = "http")]
pub http: LambdaRequestHTTP,
#[serde(rename = "domainName")]
pub domain_name: String,
}

#[derive(Debug, Default, Clone, Deserialize)]
pub struct LambdaRequestPayload {
#[serde(rename = "headers")]
pub headers: HashMap<String, String>,
#[serde(rename = "body")]
pub body: Option<String>,
#[serde(rename = "rawPath")]
pub raw_path: String,
#[serde(rename = "rawQueryString")]
pub raw_query_string: String,
#[serde(rename = "requestContext")]
pub request_context: LambdaRequestContext,
}

impl LambdaRequestPayload {
pub fn to_hyper_headermap(&self) -> HeaderMap {
let mut headers = HeaderMap::new();

for (key, value) in self.headers.iter() {
if let Ok(header_name) = HeaderName::from_str(key) {
if let Ok(value) = HeaderValue::from_str(value) {
headers.insert(header_name, value);
}
}
}

headers
}

pub fn to_full_url(&self) -> String {
let domain = self.request_context.domain_name.as_str();
let path = self.raw_path.as_str();

let query_string = if self.raw_query_string.is_empty() {
"".to_string()
} else {
format!("?{}", self.raw_query_string.as_str())
};

format!("http://{domain}{path}{query_string}")
}
}

pub async fn get_request_context() -> anyhow::Result<LambdaRequestContext> {
pub async fn get_request_context() -> anyhow::Result<LambdaRequestEvent> {
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")),
Expand All @@ -35,7 +95,7 @@ pub async fn get_request_context() -> anyhow::Result<LambdaRequestContext> {
let response =
utils::hyper::send_http_request(url, hyper::Method::GET, headers, "".to_owned()).await?;

let mut request_context = LambdaRequestContext::default();
let mut request_context = LambdaRequestEvent::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();
Expand All @@ -45,7 +105,7 @@ pub async fn get_request_context() -> anyhow::Result<LambdaRequestContext> {
request_context.trace_id = trace_id.to_str()?.to_string();
}

request_context.event_payload = response.body;
request_context.event_payload = serde_json::from_str(&response.body)?;
request_context.status_code = response.status_code;

Ok(request_context)
Expand Down
25 changes: 18 additions & 7 deletions rupring/src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod graceful;
mod parse;

#[cfg(feature = "aws-lambda")]
use bootings::aws_lambda::LambdaRequestContext;
use bootings::aws_lambda::LambdaRequestEvent;

use crate::application_properties;
use crate::application_properties::CompressionAlgorithm;
Expand Down Expand Up @@ -204,7 +204,7 @@ pub async fn run_server_on_aws_lambda(

#[cfg(feature = "aws-lambda")]
pub async fn handle_event_on_aws_lambda(
lambda_request_context: LambdaRequestContext,
mut lambda_request_context: LambdaRequestEvent,
application_properties: Arc<application_properties::ApplicationProperties>,
di_context: Arc<di::DIContext>,
root_module: impl IModule + Clone + Copy + Send + Sync + 'static,
Expand All @@ -219,11 +219,22 @@ pub async fn handle_event_on_aws_lambda(
return Ok(());
}

// TODO: build request from LambdaRequestContext
let hyper_request = hyper::Request::builder()
.method("GET")
.uri("http://localhost/")
.body("".to_string())?;
let hyper_request_builder = hyper::Request::builder()
.method(
lambda_request_context
.event_payload
.request_context
.http
.method
.as_str(),
)
.uri(lambda_request_context.event_payload.to_full_url());

let body = std::mem::take(&mut lambda_request_context.event_payload.body);

let mut hyper_request = hyper_request_builder.body(body.unwrap_or_default())?;

*hyper_request.headers_mut() = lambda_request_context.event_payload.to_hyper_headermap();

// 5. process request
let mut response = process_request(
Expand Down

0 comments on commit 5f8c06a

Please sign in to comment.