-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Visualization: added configuration page
- Loading branch information
1 parent
ad24a9c
commit 962d906
Showing
25 changed files
with
281 additions
and
59 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 |
---|---|---|
|
@@ -8,4 +8,5 @@ target | |
*credentials* | ||
.DS_Store | ||
.idea | ||
|
||
*.log | ||
*.iml |
1 change: 1 addition & 0 deletions
1
examples/binance_demo_new/migrations/20220706135152_settings.down.sql
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 @@ | ||
DROP TABLE settings; |
6 changes: 6 additions & 0 deletions
6
examples/binance_demo_new/migrations/20220706135152_settings.up.sql
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,6 @@ | ||
CREATE TABLE settings | ||
( | ||
code text NOT NULL | ||
CONSTRAINT settings_pk PRIMARY KEY, | ||
content text | ||
); |
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
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,81 @@ | ||
use crate::services::settings::{SettingCodes, SettingsService}; | ||
use actix_web::web::Data; | ||
use actix_web::{get, post, put, web, Error, HttpResponse}; | ||
use serde::{Deserialize, Serialize}; | ||
use std::sync::Arc; | ||
use toml::Value; | ||
|
||
#[derive(Deserialize)] | ||
pub struct ConfigPayload { | ||
config: String, | ||
} | ||
|
||
#[derive(Serialize)] | ||
pub struct GetConfigResponse { | ||
config: Option<String>, | ||
} | ||
|
||
#[get("")] | ||
pub async fn get(settings_service: Data<Arc<SettingsService>>) -> Result<HttpResponse, Error> { | ||
let configuration = settings_service | ||
.get_settings(SettingCodes::Configuration) | ||
.await; | ||
match configuration { | ||
Ok(configuration) => Ok(HttpResponse::Ok().json(GetConfigResponse { | ||
config: configuration.content, | ||
})), | ||
Err(e) => match e { | ||
sqlx::Error::RowNotFound => { | ||
Ok(HttpResponse::Ok().json(GetConfigResponse { config: None })) | ||
} | ||
_ => { | ||
log::error!("Get config error: {:?}", e); | ||
Ok(HttpResponse::InternalServerError().finish()) | ||
} | ||
}, | ||
} | ||
} | ||
|
||
#[put("")] | ||
pub async fn save( | ||
payload: web::Json<ConfigPayload>, | ||
settings_service: Data<Arc<SettingsService>>, | ||
) -> Result<HttpResponse, Error> { | ||
if toml::from_str::<Value>(&payload.config).is_err() { | ||
return Ok(HttpResponse::BadRequest().finish()); | ||
} | ||
match settings_service | ||
.save_setting(SettingCodes::Configuration, &payload.config) | ||
.await | ||
{ | ||
Ok(()) => Ok(HttpResponse::Ok().finish()), | ||
Err(e) => { | ||
log::error!("Save config error: {:?}. Config {}", e, &payload.config); | ||
Ok(HttpResponse::InternalServerError().finish()) | ||
} | ||
} | ||
} | ||
|
||
#[derive(Serialize)] | ||
pub struct ValidateResponse { | ||
valid: bool, | ||
error: Option<String>, | ||
} | ||
|
||
#[post("/validate")] | ||
pub async fn validate( | ||
payload: web::Json<ConfigPayload>, | ||
) -> Result<web::Json<ValidateResponse>, Error> { | ||
let response = match toml::from_str::<Value>(&payload.config) { | ||
Ok(_) => ValidateResponse { | ||
valid: true, | ||
error: None, | ||
}, | ||
Err(e) => ValidateResponse { | ||
valid: false, | ||
error: Some(e.to_string()), | ||
}, | ||
}; | ||
|
||
Ok(web::Json(response)) | ||
} |
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,3 +1,4 @@ | ||
pub mod account; | ||
pub mod configuration; | ||
pub mod liquidity; | ||
pub mod ws; |
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
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 |
---|---|---|
|
@@ -2,4 +2,5 @@ pub mod account; | |
pub mod auth; | ||
pub mod liquidity; | ||
pub mod market_settings; | ||
pub mod settings; | ||
pub mod token; |
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,38 @@ | ||
use serde::{Deserialize, Serialize}; | ||
use sqlx::{Pool, Postgres}; | ||
|
||
#[derive(Clone)] | ||
pub struct SettingsService { | ||
pool: Pool<Postgres>, | ||
} | ||
|
||
#[derive(Debug)] | ||
pub enum SettingCodes { | ||
Configuration, | ||
} | ||
|
||
#[derive(sqlx::FromRow, Serialize, Deserialize, Clone)] | ||
pub struct Setting { | ||
pub content: Option<String>, | ||
} | ||
|
||
impl SettingsService { | ||
pub fn new(pool: Pool<Postgres>) -> Self { | ||
Self { pool } | ||
} | ||
pub async fn get_settings(&self, code: SettingCodes) -> Result<Setting, sqlx::Error> { | ||
sqlx::query_as::<Postgres, Setting>(include_str!("sql/get_settings_by_code.sql")) | ||
.bind(format!("{:?}", code)) | ||
.fetch_one(&self.pool) | ||
.await | ||
} | ||
|
||
pub async fn save_setting(&self, code: SettingCodes, content: &str) -> Result<(), sqlx::Error> { | ||
sqlx::query(include_str!("sql/merge_settings.sql")) | ||
.bind(format!("{:?}", code)) | ||
.bind(content) | ||
.execute(&self.pool) | ||
.await?; | ||
Ok(()) | ||
} | ||
} |
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,3 @@ | ||
SELECT content | ||
FROM settings | ||
WHERE code = $1 |
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,3 @@ | ||
INSERT INTO settings(code, content) | ||
VALUES ($1, $2) | ||
ON CONFLICT (code) DO UPDATE SET content = $2 |
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
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
Oops, something went wrong.