-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: HAOYUatHZ <37070449+HAOYUatHZ@users.noreply.github.com>
- Loading branch information
1 parent
cf19a45
commit 44a902e
Showing
5 changed files
with
89 additions
and
21 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 |
---|---|---|
@@ -1,32 +1,64 @@ | ||
use actix_web::{App, HttpServer}; | ||
use paperclip::actix::{ | ||
api_v2_operation, | ||
web::{self, Json}, | ||
Apiv2Schema, OpenApiExt, | ||
}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Serialize, Deserialize, Apiv2Schema)] | ||
struct Pet { | ||
name: String, | ||
id: Option<i64>, | ||
} | ||
|
||
#[api_v2_operation] | ||
async fn echo_pet(body: Json<Pet>) -> Result<Json<Pet>, actix_web::Error> { | ||
Ok(body) | ||
} | ||
use dingir_exchange::openapi::user::get_user; | ||
use dingir_exchange::restapi::state::{AppCache, AppState}; | ||
use fluidex_common::non_blocking_tracing; | ||
use paperclip::actix::web::{self}; | ||
use paperclip::actix::{api_v2_operation, OpenApiExt}; | ||
use sqlx::postgres::Postgres; | ||
use sqlx::Pool; | ||
use std::collections::HashMap; | ||
use std::convert::TryFrom; | ||
use std::sync::Mutex; | ||
|
||
#[actix_web::main] | ||
async fn main() -> std::io::Result<()> { | ||
HttpServer::new(|| { | ||
dotenv::dotenv().ok(); | ||
let _guard = non_blocking_tracing::setup(); | ||
|
||
let db_url = dingir_exchange::config::Settings::new().db_history; | ||
log::debug!("Prepared DB connection: {}", &db_url); | ||
|
||
let config = dingir_exchange::restapi::config::Settings::default(); | ||
let manage_channel = if let Some(ep_str) = &config.manage_endpoint { | ||
log::info!("Connect to manage channel {}", ep_str); | ||
Some( | ||
tonic::transport::Endpoint::try_from(ep_str.clone()) | ||
.ok() | ||
.unwrap() | ||
.connect() | ||
.await | ||
.unwrap(), | ||
) | ||
} else { | ||
None | ||
}; | ||
|
||
let user_map = web::Data::new(AppState { | ||
user_addr_map: Mutex::new(HashMap::new()), | ||
manage_channel, | ||
db: Pool::<Postgres>::connect(&db_url).await.unwrap(), | ||
config, | ||
}); | ||
|
||
HttpServer::new(move || { | ||
App::new() | ||
.app_data(user_map.clone()) | ||
.app_data(AppCache::new()) | ||
.wrap_api() | ||
.service(web::resource("/pets").route(web::post().to(echo_pet))) | ||
.service( | ||
web::scope("/openapi") | ||
.route("/ping", web::get().to(ping)) | ||
.route("/user/{l1addr_or_l2pubkey}", web::get().to(get_user)), | ||
) | ||
.with_json_spec_at("/api/spec") | ||
.build() | ||
}) | ||
.bind("0.0.0.0:50054")? | ||
.run() | ||
.await | ||
} | ||
|
||
#[api_v2_operation] | ||
async fn ping() -> Result<&'static str, actix_web::Error> { | ||
Ok("pong") | ||
} |
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 @@ | ||
pub mod user; |
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,33 @@ | ||
use crate::models::{tablenames::ACCOUNT, AccountDesc}; | ||
use crate::restapi::errors::RpcError; | ||
use crate::restapi::state::AppState; | ||
use paperclip::actix::api_v2_operation; | ||
use paperclip::actix::web::{self, HttpRequest, Json}; | ||
|
||
#[api_v2_operation] | ||
pub async fn get_user(req: HttpRequest, data: web::Data<AppState>) -> Result<Json<AccountDesc>, actix_web::Error> { | ||
let user_id: &str = req.match_info().get("l1addr_or_l2pubkey").unwrap(); | ||
let mut user_map = data.user_addr_map.lock().unwrap(); | ||
if user_map.contains_key(user_id) { | ||
let user_info = &*user_map.get(user_id).unwrap(); | ||
return Ok(Json(user_info.clone())); | ||
} | ||
|
||
let sql_query = format!("select * from {} where l1_address = $1 OR l2_pubkey = $1", ACCOUNT); | ||
let user: AccountDesc = sqlx::query_as(&sql_query).bind(user_id).fetch_one(&data.db).await.map_err(|e| { | ||
log::error!("{:?}", e); | ||
RpcError::bad_request("invalid user id or address") | ||
})?; | ||
|
||
// update cache | ||
user_map.insert( | ||
user.l1_address.clone(), | ||
AccountDesc { | ||
id: user.id, | ||
l1_address: user.l1_address.clone(), | ||
l2_pubkey: user.l2_pubkey.clone(), | ||
}, | ||
); | ||
|
||
Ok(Json(user)) | ||
} |
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