Browse Source

Embed maker/taker frontend into binary

Note: we need to check-in `frontend/dist` directories as otherwise the build will fail.
If you want to embed the files into the binaries you will need to build them first (`yarn run build`).
no-contract-setup-message
Philipp Hoenisch 3 years ago
parent
commit
6ec95eef7a
No known key found for this signature in database GPG Key ID: E5F8E74C672BC666
  1. 56
      Cargo.lock
  2. 2
      daemon/Cargo.toml
  3. 4
      daemon/src/maker.rs
  4. 35
      daemon/src/routes_maker.rs
  5. 35
      daemon/src/routes_taker.rs
  6. 4
      daemon/src/taker.rs
  7. 1
      frontend/.gitignore
  8. 4
      frontend/dist/maker/.gitignore
  9. 4
      frontend/dist/taker/.gitignore

56
Cargo.lock

@ -419,6 +419,7 @@ dependencies = [
"rand 0.6.5",
"rocket",
"rocket_db_pools",
"rust-embed",
"rust_decimal",
"rust_decimal_macros",
"serde",
@ -427,7 +428,6 @@ dependencies = [
"sha2",
"sqlx",
"tempfile",
"thiserror",
"tokio",
"tokio-util",
"uuid",
@ -1679,6 +1679,40 @@ dependencies = [
"uncased",
]
[[package]]
name = "rust-embed"
version = "6.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1be44a6694859b7cfc955699935944a6844aa9fe416aeda5d40829e3e38dfee6"
dependencies = [
"rust-embed-impl",
"rust-embed-utils",
"walkdir",
]
[[package]]
name = "rust-embed-impl"
version = "6.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f567ca01565c50c67b29e535f5f67b8ea8aeadaeed16a88f10792ab57438b957"
dependencies = [
"proc-macro2",
"quote",
"rust-embed-utils",
"syn",
"walkdir",
]
[[package]]
name = "rust-embed-utils"
version = "7.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6116e7ab9ea963f60f2f20291d8fcf6c7273192cdd7273b3c80729a9605c97b2"
dependencies = [
"sha2",
"walkdir",
]
[[package]]
name = "rust_decimal"
version = "1.16.0"
@ -1739,6 +1773,15 @@ version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e"
[[package]]
name = "same-file"
version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502"
dependencies = [
"winapi-util",
]
[[package]]
name = "scoped-tls"
version = "1.0.0"
@ -2411,6 +2454,17 @@ version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5fecdca9a5291cc2b8dcf7dc02453fee791a280f3743cb0905f8822ae463b3fe"
[[package]]
name = "walkdir"
version = "2.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "808cf2735cd4b6866113f648b791c6adc5714537bc222d9347bb203386ffda56"
dependencies = [
"same-file",
"winapi 0.3.9",
"winapi-util",
]
[[package]]
name = "want"
version = "0.3.0"

2
daemon/Cargo.toml

@ -13,6 +13,7 @@ hkdf = "0.11"
rand = "0.6"
rocket = { git = "https://github.com/SergioBenitez/Rocket", features = ["json"] }
rocket_db_pools = { git = "https://github.com/SergioBenitez/Rocket", features = ["sqlx_sqlite"] }
rust-embed = "6.2"
rust_decimal = { version = "1.16", features = ["serde-float", "serde-arbitrary-precision"] }
rust_decimal_macros = "1.15"
serde = { version = "1", features = ["derive"] }
@ -20,7 +21,6 @@ serde_json = "1"
serde_with = { version = "1", features = ["macros"] }
sha2 = "0.9"
sqlx = { version = "0.5", features = ["offline"] }
thiserror = "1"
tokio = { version = "1", features = ["rt-multi-thread", "macros", "sync", "net"] }
tokio-util = { version = "0.6", features = ["codec"] }
uuid = { version = "0.8", features = ["serde", "v4"] }

4
daemon/src/maker.rs

@ -162,6 +162,10 @@ async fn main() -> Result<()> {
routes_maker::get_health_check
],
)
.mount(
"/",
rocket::routes![routes_maker::dist, routes_maker::index],
)
.launch()
.await?;

35
daemon/src/routes_maker.rs

@ -4,11 +4,15 @@ use crate::model::{Usd, WalletInfo};
use crate::to_sse_event::ToSseEvent;
use anyhow::Result;
use rocket::response::status;
use rocket::http::{ContentType, Status};
use rocket::response::stream::EventStream;
use rocket::response::{status, Responder};
use rocket::serde::json::Json;
use rocket::State;
use rust_embed::RustEmbed;
use serde::Deserialize;
use std::ffi::OsStr;
use std::path::PathBuf;
use tokio::select;
use tokio::sync::{mpsc, watch};
@ -106,3 +110,32 @@ pub fn get_health_check() {}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct RetrieveCurrentOrder;
#[derive(RustEmbed)]
#[folder = "../frontend/dist/maker"]
struct Asset;
#[rocket::get("/assets/<file..>")]
pub fn dist<'r>(file: PathBuf) -> impl Responder<'r, 'static> {
let filename = format!("assets/{}", file.display().to_string());
Asset::get(&filename).map_or_else(
|| Err(Status::NotFound),
|d| {
let ext = file
.as_path()
.extension()
.and_then(OsStr::to_str)
.ok_or_else(|| Status::new(400))?;
let content_type = ContentType::from_extension(ext).ok_or_else(|| Status::new(400))?;
Ok((content_type, d.data))
},
)
}
#[rocket::get("/<_paths..>")]
pub fn index<'r>(_paths: PathBuf) -> impl Responder<'r, 'static> {
Asset::get("index.html").map_or_else(
|| Err(Status::NotFound),
|d| Ok((ContentType::HTML, d.data)),
)
}

35
daemon/src/routes_taker.rs

@ -3,11 +3,15 @@ use crate::model::{Leverage, Usd, WalletInfo};
use crate::taker_cfd_actor;
use crate::to_sse_event::ToSseEvent;
use bdk::bitcoin::Amount;
use rocket::response::status;
use rocket::http::{ContentType, Status};
use rocket::response::stream::EventStream;
use rocket::response::{status, Responder};
use rocket::serde::json::Json;
use rocket::State;
use rust_embed::RustEmbed;
use serde::{Deserialize, Serialize};
use std::ffi::OsStr;
use std::path::PathBuf;
use tokio::select;
use tokio::sync::{mpsc, watch};
@ -101,3 +105,32 @@ pub fn margin_calc(
Ok(status::Accepted(Some(Json(MarginResponse { margin }))))
}
#[derive(RustEmbed)]
#[folder = "../frontend/dist/maker"]
struct Asset;
#[rocket::get("/assets/<file..>")]
pub fn dist<'r>(file: PathBuf) -> impl Responder<'r, 'static> {
let filename = format!("assets/{}", file.display().to_string());
Asset::get(&filename).map_or_else(
|| Err(Status::NotFound),
|d| {
let ext = file
.as_path()
.extension()
.and_then(OsStr::to_str)
.ok_or_else(|| Status::new(400))?;
let content_type = ContentType::from_extension(ext).ok_or_else(|| Status::new(400))?;
Ok((content_type, d.data))
},
)
}
#[rocket::get("/<_paths..>")]
pub fn index<'r>(_paths: PathBuf) -> impl Responder<'r, 'static> {
Asset::get("index.html").map_or_else(
|| Err(Status::NotFound),
|d| Ok((ContentType::HTML, d.data)),
)
}

4
daemon/src/taker.rs

@ -171,6 +171,10 @@ async fn main() -> Result<()> {
routes_taker::margin_calc,
],
)
.mount(
"/",
rocket::routes![routes_taker::dist, routes_taker::index],
)
.launch()
.await?;

1
frontend/.gitignore

@ -1,5 +1,4 @@
node_modules
.DS_Store
dist
dist-ssr
*.local

4
frontend/dist/maker/.gitignore

@ -0,0 +1,4 @@
# Ignore everything in this directory
*
# Except this file
!.gitignore

4
frontend/dist/taker/.gitignore

@ -0,0 +1,4 @@
# Ignore everything in this directory
*
# Except this file
!.gitignore
Loading…
Cancel
Save