Skip to content

Commit 55777f9

Browse files
committed
CHG cleanup + make updating proxy time async
1 parent 8f64345 commit 55777f9

File tree

1 file changed

+17
-22
lines changed

1 file changed

+17
-22
lines changed

theia/proxy-rs/src/main.rs

+17-22
Original file line numberDiff line numberDiff line change
@@ -96,17 +96,18 @@ fn create_jwt(session_id: &str, net_id: &str) -> Result<String> {
9696
}
9797

9898
async fn ping(jar: CookieJar, Extension(pool): Extension<Arc<MySqlPool>>) -> (StatusCode, String) {
99-
match jar.get("ide") {
100-
Some(cookie) => match authenticate_jwt(cookie.value()) {
101-
Ok(claims) => {
102-
db::update_last_proxy_time(&claims.session_id, &pool)
103-
.await
104-
.unwrap();
105-
}
106-
Err(_) => {}
107-
},
108-
None => {}
109-
};
99+
// Asynchronously update the last proxy time for the session
100+
// if the request contains a valid ide jwt cookie
101+
if let Some(cookie) = jar.get("ide") {
102+
authenticate_jwt(cookie.value()).ok().map(|claims| {
103+
tokio::spawn(async move {
104+
let result = db::update_last_proxy_time(&claims.session_id, &pool).await;
105+
if let Err(e) = result {
106+
tracing::error!("failed to update last proxy time: {}", e);
107+
}
108+
});
109+
});
110+
}
110111

111112
(StatusCode::OK, "pong".to_string())
112113
}
@@ -117,7 +118,7 @@ struct InitializeQueryParams {
117118
}
118119

119120
async fn initialize(params: Query<InitializeQueryParams>, jar: CookieJar) -> impl IntoResponse {
120-
let failed_response = |_reason: &str| {
121+
let failed_response = {
121122
(
122123
StatusCode::PERMANENT_REDIRECT,
123124
jar.clone(),
@@ -129,20 +130,14 @@ async fn initialize(params: Query<InitializeQueryParams>, jar: CookieJar) -> imp
129130
Ok(claims) => claims,
130131
Err(err) => {
131132
tracing::error!("failed to authenticate jwt: {}", err);
132-
return failed_response("Invalid token");
133+
return failed_response;
133134
}
134135
};
135136

136137
let new_token = create_jwt(&token.session_id, &token.net_id).unwrap();
137138
let ide_cookie = Cookie::new("ide", new_token);
138-
139139
let new_jar = jar.add(ide_cookie);
140140

141-
let _domain = match *IS_DEBUG {
142-
true => "localhost".to_string(),
143-
false => "anubis-lms.io".to_string(),
144-
};
145-
146141
(
147142
StatusCode::PERMANENT_REDIRECT,
148143
new_jar,
@@ -195,7 +190,7 @@ async fn handle(
195190
let cluster_address = db::get_cluster_address(&pool, &token.session_id)
196191
.await
197192
.map_err(|e| {
198-
eprintln!("Error: {}", e);
193+
tracing::error!("failed to get cluster address: {}", e);
199194
(
200195
StatusCode::INTERNAL_SERVER_ERROR,
201196
"Internal server error".to_string(),
@@ -263,6 +258,7 @@ async fn main() -> Result<()> {
263258
let app = Router::new()
264259
.route("/ping", get(ping))
265260
.route("/initialize", get(initialize))
261+
// Seems like we need to handle `/` and `/*` seperately ?
266262
.route("/", get(handle))
267263
.route("/*path", get(handle))
268264
.layer(Extension(pool))
@@ -276,8 +272,7 @@ async fn main() -> Result<()> {
276272
.on_response(trace::DefaultOnResponse::new().level(Level::INFO)),
277273
);
278274

279-
tracing::info!("Server started on port {}", PROXY_SERVER_PORT);
280-
275+
tracing::info!("server started on port {}", PROXY_SERVER_PORT);
281276
let listener = tokio::net::TcpListener::bind(format!("0.0.0.0:{}", PROXY_SERVER_PORT))
282277
.await
283278
.unwrap();

0 commit comments

Comments
 (0)