Skip to content

relay join hub and create service #407

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions common/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub struct Config {
pub storage: StorageConfig,
pub monorepo: MonoConfig,
pub pack: PackConfig,
pub relay: RelayConfig,
pub ztm: ZTMConfig,
}

impl Config {
Expand Down Expand Up @@ -137,16 +137,18 @@ impl Default for PackConfig {
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct RelayConfig {
pub struct ZTMConfig {
pub ca: String,
pub hub: String,
pub agent: String,
}

impl Default for RelayConfig {
impl Default for ZTMConfig {
fn default() -> Self {
Self {
ca: String::from("127.0.0.1:9999"),
hub: String::from("127.0.0.1:8888"),
agent: String::from("127.0.0.1:7777"),
}
}
}
25 changes: 20 additions & 5 deletions gateway/src/relay_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::str::FromStr;

use axum::body::Body;
use axum::extract::{Query, State};
use axum::http::{Response, StatusCode, Uri};
use axum::http::{Request, Response, StatusCode, Uri};
use axum::routing::get;
use axum::Router;
use clap::Args;
Expand Down Expand Up @@ -74,8 +74,8 @@ pub async fn app(config: Config, host: String, port: u16) -> Router {
)
.route(
"/*path",
get(get_method_router), // .post(post_method_router)
// .put(put_method_router),
get(get_method_router).post(post_method_router),
// .put(put_method_router),
)
.layer(ServiceBuilder::new().layer(CorsLayer::new().allow_origin(Any)))
.layer(TraceLayer::new_for_http())
Expand All @@ -88,11 +88,26 @@ async fn get_method_router(
Query(params): Query<RelayGetParams>,
uri: Uri,
) -> Result<Response<Body>, (StatusCode, String)> {
let relay_config = state.context.config.relay.clone();
let ztm_config = state.context.config.ztm.clone();
if Regex::new(r"/hello$").unwrap().is_match(uri.path()) {
return gemini::http::handler::hello_gemini(params).await;
} else if Regex::new(r"/certificate$").unwrap().is_match(uri.path()) {
return gemini::ztm::handler::get_ztm_certificate(relay_config, params).await;
return gemini::http::handler::certificate(ztm_config, params).await;
}
Err((
StatusCode::NOT_FOUND,
String::from("Operation not supported\n"),
))
}

async fn post_method_router(
state: State<AppState>,
uri: Uri,
req: Request<Body>,
) -> Result<Response<Body>, (StatusCode, String)> {
let ztm_config = state.context.config.ztm.clone();
if Regex::new(r"/relay_init$").unwrap().is_match(uri.path()) {
return gemini::http::handler::init(ztm_config, req, state.0.port).await;
}
Err((
StatusCode::NOT_FOUND,
Expand Down
79 changes: 76 additions & 3 deletions gemini/src/http/handler.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,88 @@
use axum::{
body::Body,
http::{Response, StatusCode},
body::{to_bytes, Body},
http::{Request, Response, StatusCode},
};
use common::config::ZTMConfig;

use crate::RelayGetParams;
use crate::{
ztm::{
ZTMUserPermit, {connect_ztm_hub, create_ztm_certificate, create_ztm_service},
},
RelayGetParams,
};

pub async fn hello_gemini(_params: RelayGetParams) -> Result<Response<Body>, (StatusCode, String)> {
Ok(Response::builder()
.body(Body::from("hello gemini"))
.unwrap())
}

pub async fn certificate(
config: ZTMConfig,
params: RelayGetParams,
) -> Result<Response<Body>, (StatusCode, String)> {
if params.name.is_none() {
return Err((StatusCode::BAD_REQUEST, "not enough paras".to_string()));
}
let name = params.name.unwrap();
let permit = match create_ztm_certificate(config, name.clone()).await {
Ok(p) => p,
Err(e) => {
return Err((StatusCode::INTERNAL_SERVER_ERROR, e));
}
};

let permit_json = serde_json::to_string(&permit).unwrap();
tracing::info!("new permit [{name}]: {permit_json}");

Ok(Response::builder()
.header("Content-Type", "application/json")
.body(Body::from(permit_json))
.unwrap())
}

pub async fn init(
config: ZTMConfig,
req: Request<Body>,
relay_port: u16,
) -> Result<Response<Body>, (StatusCode, String)> {
// transfer body to json
let body_bytes = match to_bytes(req.into_body(), usize::MAX).await {
Ok(b) => b,
Err(e) => {
return Err((StatusCode::BAD_REQUEST, e.to_string()));
}
};

let permit: ZTMUserPermit = match serde_json::from_slice(&body_bytes) {
Ok(p) => p,
Err(e) => {
return Err((StatusCode::BAD_REQUEST, e.to_string()));
}
};

// 1. connect to ZTM hub (join a mesh)
let mesh = match connect_ztm_hub(config.clone(), permit).await {
Ok(m) => m,
Err(e) => {
return Err((StatusCode::INTERNAL_SERVER_ERROR, e));
}
};

// 2. create a ZTM service
let response_text =
match create_ztm_service(config, mesh.agent.id, "relay".to_string(), relay_port).await {
Ok(m) => m,
Err(e) => {
return Err((StatusCode::INTERNAL_SERVER_ERROR, e));
}
};

Ok(Response::builder()
.header("Content-Type", "application/json")
.body(Body::from(response_text))
.unwrap())
}

#[cfg(test)]
mod tests {}
91 changes: 0 additions & 91 deletions gemini/src/ztm/handler.rs

This file was deleted.

Loading
Loading