diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 7a67ea2..a41c788 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -53,17 +53,30 @@ build-bin: - target/release/vade - vade-linux-x64.tar.gz + +build-docker-bin: + stage: build + image: 'rust:latest' + script: + - rustup target add x86_64-unknown-linux-musl + - apt update && apt install -y musl-tools musl-dev + - update-ca-certificates + - cargo build --target x86_64-unknown-linux-musl --release + artifacts: + paths: + - target/x86_64-unknown-linux-musl/release/vade + build-docker: dependencies: - - build-bin + - build-docker-bin stage: build-docker image: docker:latest script: - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY - docker build -t $CI_REGISTRY/vade/vade-mecum . - docker push $CI_REGISTRY/vade/vade-mecum - rules: - - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH + # rules: + # - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH deploy-binaries: dependencies: diff --git a/Cargo.lock b/Cargo.lock index fad972b..aab47cd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -852,22 +852,6 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2df960f5d869b2dd8532793fde43eb5427cceb126c929747a26823ab0eeb536" -[[package]] -name = "core-foundation" -version = "0.9.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6888e10551bb93e424d8df1d07f1a8b4fceb0001a3a4b048bfc47554946f47b3" -dependencies = [ - "core-foundation-sys", - "libc", -] - -[[package]] -name = "core-foundation-sys" -version = "0.8.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5827cebf4670468b8772dd191856768aedcb1b0278a04f989f7766351917b9dc" - [[package]] name = "cpufeatures" version = "0.2.1" @@ -1012,15 +996,6 @@ dependencies = [ "syn", ] -[[package]] -name = "fastrand" -version = "1.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c3fcf0cee53519c866c09b5de1f6c56ff9d647101f81c1964fa632e148896cdf" -dependencies = [ - "instant", -] - [[package]] name = "firestorm" version = "0.5.0" @@ -1057,21 +1032,6 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" -[[package]] -name = "foreign-types" -version = "0.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1" -dependencies = [ - "foreign-types-shared", -] - -[[package]] -name = "foreign-types-shared" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" - [[package]] name = "form_urlencoded" version = "1.0.1" @@ -1082,6 +1042,12 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "fs_extra" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2022715d62ab30faffd124d40b76f4134a550a87792276512b18d63272333394" + [[package]] name = "fuchsia-zircon" version = "0.3.3" @@ -1426,6 +1392,27 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1aab8fc367588b89dcee83ab0fd66b72b50b72fa1904d7095045ace2b0c81c35" +[[package]] +name = "jemalloc-sys" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d3b9f3f5c9b31aa0f5ed3260385ac205db665baa41d49bb8338008ae94ede45" +dependencies = [ + "cc", + "fs_extra", + "libc", +] + +[[package]] +name = "jemallocator" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43ae63fcfc45e99ab3d1b29a46782ad679e98436c3169d15a167a1108a724b69" +dependencies = [ + "jemalloc-sys", + "libc", +] + [[package]] name = "jobserver" version = "0.1.24" @@ -1690,24 +1677,6 @@ dependencies = [ "winapi 0.3.9", ] -[[package]] -name = "native-tls" -version = "0.2.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "48ba9f7719b5a0f42f338907614285fb5fd70e53858141f69898a1fb7203b24d" -dependencies = [ - "lazy_static", - "libc", - "log", - "openssl", - "openssl-probe", - "openssl-sys", - "schannel", - "security-framework", - "security-framework-sys", - "tempfile", -] - [[package]] name = "net2" version = "0.2.37" @@ -1811,39 +1780,6 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" -[[package]] -name = "openssl" -version = "0.10.38" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0c7ae222234c30df141154f159066c5093ff73b63204dcda7121eb082fc56a95" -dependencies = [ - "bitflags", - "cfg-if 1.0.0", - "foreign-types", - "libc", - "once_cell", - "openssl-sys", -] - -[[package]] -name = "openssl-probe" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf" - -[[package]] -name = "openssl-sys" -version = "0.9.72" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7e46109c383602735fa0a2e48dd2b7c892b048e1bf69e5c3b1d804b7d9c203cb" -dependencies = [ - "autocfg", - "cc", - "libc", - "pkg-config", - "vcpkg", -] - [[package]] name = "ouroboros" version = "0.14.0" @@ -2163,15 +2099,6 @@ version = "0.6.25" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b" -[[package]] -name = "remove_dir_all" -version = "0.5.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7" -dependencies = [ - "winapi 0.3.9", -] - [[package]] name = "resolv-conf" version = "0.7.0" @@ -2261,6 +2188,19 @@ dependencies = [ "semver 1.0.4", ] +[[package]] +name = "rustls" +version = "0.19.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "35edb675feee39aec9c99fa5ff985081995a06d594114ae14cbe797ad7b7a6d7" +dependencies = [ + "base64", + "log", + "ring", + "sct", + "webpki", +] + [[package]] name = "rustversion" version = "1.0.6" @@ -2282,22 +2222,22 @@ dependencies = [ "winapi-util", ] -[[package]] -name = "schannel" -version = "0.1.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f05ba609c234e60bee0d547fe94a4c7e9da733d1c962cf6e59efa4cd9c8bc75" -dependencies = [ - "lazy_static", - "winapi 0.3.9", -] - [[package]] name = "scopeguard" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" +[[package]] +name = "sct" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b362b83898e0e69f38515b82ee15aa80636befe47c3b6d3d89a911e78fc228ce" +dependencies = [ + "ring", + "untrusted", +] + [[package]] name = "sea-orm" version = "0.5.0" @@ -2384,29 +2324,6 @@ dependencies = [ "syn", ] -[[package]] -name = "security-framework" -version = "2.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3fed7948b6c68acbb6e20c334f55ad635dc0f75506963de4464289fbd3b051ac" -dependencies = [ - "bitflags", - "core-foundation", - "core-foundation-sys", - "libc", - "security-framework-sys", -] - -[[package]] -name = "security-framework-sys" -version = "2.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a57321bf8bc2362081b2599912d2961fe899c0efadf1b4b2f8d48b3e253bb96c" -dependencies = [ - "core-foundation-sys", - "libc", -] - [[package]] name = "semver" version = "0.9.0" @@ -2659,6 +2576,7 @@ dependencies = [ "parking_lot", "percent-encoding", "rust_decimal", + "rustls", "serde", "serde_json", "sha2", @@ -2670,6 +2588,8 @@ dependencies = [ "tokio-stream", "url", "uuid", + "webpki", + "webpki-roots", ] [[package]] @@ -2699,10 +2619,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8061cbaa91ee75041514f67a09398c65a64efed72c90151ecd47593bad53da99" dependencies = [ "actix-rt 2.6.0", - "native-tls", "once_cell", "tokio 1.16.1", - "tokio-native-tls", + "tokio-rustls", ] [[package]] @@ -2790,20 +2709,6 @@ dependencies = [ "unicode-xid", ] -[[package]] -name = "tempfile" -version = "3.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5cdb1ef4eaeeaddc8fbd371e5017057064af0911902ef36b39801f67cc6d79e4" -dependencies = [ - "cfg-if 1.0.0", - "fastrand", - "libc", - "redox_syscall", - "remove_dir_all", - "winapi 0.3.9", -] - [[package]] name = "thiserror" version = "1.0.30" @@ -2963,13 +2868,14 @@ dependencies = [ ] [[package]] -name = "tokio-native-tls" -version = "0.3.0" +name = "tokio-rustls" +version = "0.22.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f7d995660bd2b7f8c1568414c1126076c13fbb725c40112dc0120b78eb9b717b" +checksum = "bc6844de72e57df1980054b38be3a9f4702aba4858be64dd700181a8a6d0e1b6" dependencies = [ - "native-tls", + "rustls", "tokio 1.16.1", + "webpki", ] [[package]] @@ -3256,6 +3162,7 @@ dependencies = [ "base64", "bcrypt", "chrono", + "jemallocator", "jsonwebtoken", "mime_guess", "rand 0.8.4", @@ -3263,6 +3170,7 @@ dependencies = [ "sea-orm", "serde", "serde_json", + "sqlx", "tracing", "tracing-actix-web", "tracing-subscriber", @@ -3375,6 +3283,25 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "webpki" +version = "0.21.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8e38c0608262c46d4a56202ebabdeb094cef7e560ca7a226c6bf055188aa4ea" +dependencies = [ + "ring", + "untrusted", +] + +[[package]] +name = "webpki-roots" +version = "0.21.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aabe153544e473b775453675851ecc86863d2a81d786d741f6b76778f2a48940" +dependencies = [ + "webpki", +] + [[package]] name = "widestring" version = "0.4.3" diff --git a/Cargo.toml b/Cargo.toml index ab67d7c..0eaf39b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,7 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -sea-orm = { version = "0.5.0", features = [ "sqlx-sqlite", "runtime-actix-native-tls", "macros", "mock"], default-features = false } +sea-orm = { version = "0.5.0", features = [ "sqlx-sqlite", "runtime-actix-rustls", "macros", "mock"], default-features = false } tracing = "0.1.30" tracing-unwrap = "0.9.2" tracing-subscriber = { version = "0.3.8", features = ["fmt"] } @@ -26,3 +26,7 @@ actix-web-httpauth = "0.6.0-beta.7" jsonwebtoken = "8.0.1" rand = "0.8.4" base64 = "0.13.0" +sqlx = { version = "^0.5", features=["sqlite", "migrate"] } + +[target.'cfg(all(target_env = "musl", target_pointer_width = "64"))'.dependencies.jemallocator] +version = "0.3" diff --git a/Dockerfile b/Dockerfile index 7fc2cc3..f794244 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,3 +1,7 @@ -FROM alpine:latest -COPY target/release/vade /app/vade -CMD ["/app/vade"] +FROM SCRATCH +COPY target/x84-64-unknown-linux-musl/release/vade /app/vade +EXPOSE 8080 +WORKDIR app +RUN touch data.db +RUN ls -lh +CMD ["./vade"] diff --git a/src/error.rs b/src/error.rs index 0a55177..71e334b 100644 --- a/src/error.rs +++ b/src/error.rs @@ -68,6 +68,23 @@ impl From for Error { } } +impl From for Error { + fn from(e: sqlx::Error) -> Self { + Self { + code: ErrorCode::DatabaseError, + message: e.to_string(), + } + } +} + +impl From for Error { + fn from(e: sqlx::migrate::MigrateError) -> Self { + Self { + code: ErrorCode::DatabaseError, + message: e.to_string(), + } + } +} impl std::fmt::Display for Error { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}", self.message) diff --git a/src/main.rs b/src/main.rs index d6fe073..7deac07 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ use actix_web::{get, web, App, HttpResponse, HttpServer}; use rust_embed::RustEmbed; use sea_orm::{Database, DatabaseConnection}; +use sqlx::SqliteConnection; use tracing::{info, instrument}; use tracing_subscriber::prelude::*; @@ -9,6 +10,9 @@ mod auth; mod entity; mod error; +// Use Jemalloc only for musl-64 bits platforms +#[cfg(all(target_env = "musl", target_pointer_width = "64"))] +#[global_allocator] #[derive(Debug)] pub struct AppState { pub db: DatabaseConnection, @@ -21,7 +25,7 @@ async fn main() { .pretty() .with_writer(std::io::stdout) .with_ansi(true) - .with_filter(tracing_subscriber::filter::LevelFilter::DEBUG), + .with_filter(tracing_subscriber::filter::LevelFilter::TRACE), ); tracing::subscriber::set_global_default(subscriber).expect("Unable to set a global collector"); @@ -33,6 +37,7 @@ async fn main() { HttpServer::new(move || { App::new() .app_data(state.clone()) + .wrap(tracing_actix_web::TracingLogger::default()) .service(api::routes()) .service(dist) }) @@ -64,7 +69,10 @@ async fn dist(path: web::Path) -> HttpResponse { .body(body) } -#[instrument] -async fn setup_database() -> Result { - Database::connect("sqlite://data.db").await +use sqlx::Connection; +async fn setup_database() -> error::Result { + let pool = sqlx::SqlitePool::connect("sqlite://data.db").await?; + sqlx::migrate!("./migrations").run(&pool).await?; + tracing::info!("Database migrated"); + Ok(Database::connect("sqlite://data.db").await?) }