Skip to content

Commit

Permalink
refactor: replace orignal restapi with openapi (#305)
Browse files Browse the repository at this point in the history
* Migrates Open API code to REST API.

* Fixes the fmt issue.
  • Loading branch information
silathdiir authored Sep 3, 2021
1 parent 911ad53 commit ec152a7
Show file tree
Hide file tree
Showing 15 changed files with 193 additions and 1,310 deletions.
4 changes: 0 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ tracing-appender = "0.1"
tracing-subscriber = "0.2"
ttl_cache = "0.5.1"

[[bin]]
name = "openapi"
path = "src/bin/openapi.rs"

[[bin]]
name = "restapi"
path = "src/bin/restapi.rs"
Expand Down
97 changes: 0 additions & 97 deletions src/bin/openapi.rs

This file was deleted.

109 changes: 54 additions & 55 deletions src/bin/restapi.rs
Original file line number Diff line number Diff line change
@@ -1,41 +1,28 @@
#![allow(dead_code)]
#![allow(clippy::collapsible_if)]
#![allow(clippy::let_and_return)]
#![allow(clippy::too_many_arguments)]
#![allow(clippy::single_char_pattern)]

use actix_web::{web, App, HttpRequest, HttpResponse, HttpServer, Responder};
use actix_web::{App, HttpServer};
use dingir_exchange::restapi::manage::market;
use dingir_exchange::restapi::personal_history::{my_internal_txs, my_orders};
use dingir_exchange::restapi::public_history::{order_trades, recent_trades};
use dingir_exchange::restapi::state::{AppCache, AppState};
use dingir_exchange::restapi::tradingview::{chart_config, history, search_symbols, symbols, ticker, unix_timestamp};
use dingir_exchange::restapi::user::get_user;
use fluidex_common::non_blocking_tracing;
use paperclip::actix::web::{self, HttpResponse};
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;

use dingir_exchange::restapi;

use restapi::manage::market;
use restapi::personal_history::{my_internal_txs, my_orders};
use restapi::public_history::{order_trades, recent_trades};
use restapi::state::{AppCache, AppState};
use restapi::tradingview::{chart_config, history, search_symbols, symbols, ticker, unix_timestamp};
use restapi::user::get_user;

async fn ping(_req: HttpRequest, _data: web::Data<AppState>) -> impl Responder {
"pong"
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
dotenv::dotenv().ok();
let _guard = non_blocking_tracing::setup();

// TODO: Should add another `config/restapi.yaml` for this target?
let dburl = dingir_exchange::config::Settings::new().db_history;
log::debug!("Prepared db connection: {}", &dburl);

let config = restapi::config::Settings::default();
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(
Expand All @@ -53,47 +40,59 @@ async fn main() -> std::io::Result<()> {
let user_map = web::Data::new(AppState {
user_addr_map: Mutex::new(HashMap::new()),
manage_channel,
db: Pool::<Postgres>::connect(&dburl).await.unwrap(),
db: Pool::<Postgres>::connect(&db_url).await.unwrap(),
config,
});

let workers = user_map.config.workers;

let server = HttpServer::new(move || {
App::new().app_data(user_map.clone()).app_data(AppCache::new()).service(
web::scope("/restapi")
.route("/ping", web::get().to(ping))
.route("/user/{l1addr_or_l2pubkey}", web::get().to(get_user))
.route("/recenttrades/{market}", web::get().to(recent_trades))
.route("/ordertrades/{market}/{order_id}", web::get().to(order_trades))
.route("/closedorders/{market}/{user_id}", web::get().to(my_orders))
.route("/internal_txs/{user_id}", web::get().to(my_internal_txs))
.route("/ticker_{ticker_inv}/{market}", web::get().to(ticker))
.service(
web::scope("/tradingview")
.route("/time", web::get().to(unix_timestamp))
.route("/config", web::get().to(chart_config))
.route("/search", web::get().to(search_symbols))
.route("/symbols", web::get().to(symbols))
.route("/history", web::get().to(history)),
)
.service(if user_map.manage_channel.is_some() {
web::scope("/manage").service(
web::scope("/market")
.route("/reload", web::post().to(market::reload))
.route("/tradepairs", web::post().to(market::add_pair))
.route("/assets", web::post().to(market::add_assets)),
App::new()
.app_data(user_map.clone())
.app_data(AppCache::new())
.wrap_api()
.service(
web::scope("/restapi")
.route("/ping", web::get().to(ping))
.route("/user/{l1addr_or_l2pubkey}", web::get().to(get_user))
.route("/recenttrades/{market}", web::get().to(recent_trades))
.route("/ordertrades/{market}/{order_id}", web::get().to(order_trades))
.route("/closedorders/{market}/{user_id}", web::get().to(my_orders))
.route("/internal_txs/{user_id}", web::get().to(my_internal_txs))
.route("/ticker_{ticker_inv}/{market}", web::get().to(ticker))
.service(
web::scope("/tradingview")
.route("/time", web::get().to(unix_timestamp))
.route("/config", web::get().to(chart_config))
.route("/search", web::get().to(search_symbols))
.route("/symbols", web::get().to(symbols))
.route("/history", web::get().to(history)),
)
} else {
web::scope("/manage")
.service(web::resource("/").to(|| HttpResponse::Forbidden().body(String::from("No manage endpoint"))))
}),
)
.service(if user_map.manage_channel.is_some() {
web::scope("/manage").service(
web::scope("/market")
.route("/reload", web::post().to(market::reload))
.route("/tradepairs", web::post().to(market::add_pair))
.route("/assets", web::post().to(market::add_assets)),
)
} else {
web::scope("/manage")
.service(web::resource("/").to(|| HttpResponse::Forbidden().body(String::from("No manage endpoint"))))
}),
)
.with_json_spec_at("/api/spec")
.build()
});

let server = match workers {
Some(wr) => server.workers(wr),
None => server,
};
server.bind(("0.0.0.0", 50053))?.run().await

server.bind("0.0.0.0:50053")?.run().await
}

#[api_v2_operation]
async fn ping() -> Result<&'static str, actix_web::Error> {
Ok("pong")
}
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ pub mod storage;
pub use storage::{database, models, sqlxextend};
pub mod config;
pub mod message;
pub mod openapi;
pub mod restapi;
pub mod types;
pub mod utils;
100 changes: 0 additions & 100 deletions src/openapi/manage.rs

This file was deleted.

5 changes: 0 additions & 5 deletions src/openapi/mod.rs

This file was deleted.

Loading

0 comments on commit ec152a7

Please sign in to comment.