Skip to content
This repository has been archived by the owner on Jul 1, 2021. It is now read-only.

Commit

Permalink
review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
MarinPostma committed Apr 26, 2021
1 parent e4bd1bc commit 4bbee4a
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 422 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/create_artifacts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
rust-version: stable
- uses: actions/checkout@v1
- name: Build
run: cargo build --release --locked --features mini-dashboard
run: cargo build --release --locked
- name: Upload binaries to release
uses: svenstaro/upload-release-action@v1-release
with:
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ ENV RUSTFLAGS="-C target-feature=-crt-static"
# Create dummy main.rs files for each workspace member to be able to compile all the dependencies
RUN find . -type d -name "meilisearch-*" | xargs -I{} sh -c 'mkdir {}/src; echo "fn main() { }" > {}/src/main.rs;'
# Use `cargo build` instead of `cargo vendor` because we need to not only download but compile dependencies too
RUN $HOME/.cargo/bin/cargo build --release --features mini-dashboard
RUN $HOME/.cargo/bin/cargo build --release
# Cleanup dummy main.rs files
RUN find . -path "*/src/main.rs" -delete

Expand All @@ -29,7 +29,7 @@ ARG COMMIT_DATE
ENV COMMIT_SHA=${COMMIT_SHA} COMMIT_DATE=${COMMIT_DATE}

COPY . .
RUN $HOME/.cargo/bin/cargo build --release --features mini-dashboard
RUN $HOME/.cargo/bin/cargo build --release

# Run
FROM alpine:3.10
Expand Down
32 changes: 21 additions & 11 deletions meilisearch-http/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ name = "meilisearch"
path = "src/main.rs"

[build-dependencies]
actix-web-static-files = { git = "https://github.com/MarinPostma/actix-web-static-files.git", rev = "6db8c3e" }
anyhow = "*"
cargo_toml = "0.9.0"
hex = "0.4.3"
reqwest = { version = "0.11.3", features = ["blocking", "rustls-tls"], default-features = false}
sha-1 = "0.9.4"
tempfile = "3.1.0"
actix-web-static-files = { git = "https://github.com/MarinPostma/actix-web-static-files.git", rev = "6db8c3e", optional = true }
anyhow = { version = "*", optional = true }
cargo_toml = { version = "0.9.0", optional = true }
hex = { version = "0.4.3", optional = true }
reqwest = { version = "0.11.3", features = ["blocking", "rustls-tls"], default-features = false, optional = true }
sha-1 = { version = "0.9.4", optional = true }
tempfile = { version = "3.1.0", optional = true }
vergen = "3.1.0"
zip = "0.5.12"
zip = { version = "0.5.12", optional = true }

[dependencies]
actix-cors = { git = "https://github.com/MarinPostma/actix-extras.git", rev = "2dac1a4"}
Expand Down Expand Up @@ -71,7 +71,7 @@ tar = "0.4.29"
tempfile = "3.1.0"
thiserror = "1.0.24"
tokio = { version = "1", features = ["full"] }
uuid = "0.8.2"
uuid = { version = "0.8.2", features = ["serde"] }
walkdir = "2.3.2"
obkv = "0.1.1"

Expand Down Expand Up @@ -101,8 +101,18 @@ tempdir = "0.3.7"
urlencoding = "1.1.1"

[features]
mini-dashboard = ["default", "actix-web-static-files"]
default = ["sentry"]
mini-dashboard = [
"actix-web-static-files",
"anyhow",
"cargo_toml",
"hex",
"reqwest",
"sha-1",
"tempfile",
"zip",
]
telemetry = ["sentry"]
default = ["telemetry", "mini-dashboard"]

[target.'cfg(target_os = "linux")'.dependencies]
jemallocator = "0.3.2"
Expand Down
105 changes: 65 additions & 40 deletions meilisearch-http/build.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,4 @@
use std::env;
use std::fs::create_dir_all;
use std::io::Cursor;
use std::path::PathBuf;

use anyhow::Context;
use sha1::{Sha1, Digest};
use reqwest::blocking::get;
use actix_web_static_files::resource_dir;

use vergen::{generate_cargo_keys, ConstantsFlags};
use cargo_toml::Manifest;

fn main() {
// Setup the flags, toggling off the 'SEMVER_FROM_CARGO_PKG' flag
Expand All @@ -19,42 +8,78 @@ fn main() {
// Generate the 'cargo:' key output
generate_cargo_keys(ConstantsFlags::all()).expect("Unable to generate the cargo keys!");

if let Ok(_) = env::var("CARGO_FEATURE_MINI_DASHBOARD") {
setup_mini_dashboard().expect("Could not load mini-dashboard assets");
}
#[cfg(feature = "mini-dashboard")]
mini_dashboard::setup_mini_dashboard().expect("Could not load the mini-dashboard assets");
}

fn setup_mini_dashboard() -> anyhow::Result<()> {
let cargo_manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
let cargo_toml = cargo_manifest_dir.join("Cargo.toml");
#[cfg(feature = "mini-dashboard")]
mod mini_dashboard {
use std::env;
use std::fs::{create_dir_all, File, OpenOptions};
use std::io::{Cursor, Read, Write};
use std::path::PathBuf;

use actix_web_static_files::resource_dir;
use anyhow::Context;
use cargo_toml::Manifest;
use reqwest::blocking::get;
use sha1::{Digest, Sha1};

pub fn setup_mini_dashboard() -> anyhow::Result<()> {
let cargo_manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
let cargo_toml = cargo_manifest_dir.join("Cargo.toml");

let sha1_path = cargo_manifest_dir.join(".mini-dashboard.sha1");
let dashboard_dir = cargo_manifest_dir.join("mini-dashboard");

let manifest = Manifest::from_path(cargo_toml).unwrap();
let manifest = Manifest::from_path(cargo_toml).unwrap();

let meta = &manifest
.package
.as_ref()
.context("package not specified in Cargo.toml")?
.metadata
.as_ref()
.context("no metadata specified in Cargo.toml")?
["mini-dashboard"];
let meta = &manifest
.package
.as_ref()
.context("package not specified in Cargo.toml")?
.metadata
.as_ref()
.context("no metadata specified in Cargo.toml")?["mini-dashboard"];

let url = meta["assets-url"].as_str().unwrap();
// Check if there already is a dashboard built, and if it is up to date.
if sha1_path.exists() && dashboard_dir.exists() {
let mut sha1_file = File::open(&sha1_path)?;
let mut sha1 = String::new();
sha1_file.read_to_string(&mut sha1)?;
if sha1 == meta["sha1"].as_str().unwrap() {
// Nothing to do.
return Ok(())
}
}

let dashboard_assets_bytes = get(url)?
.bytes()?;
let url = meta["assets-url"].as_str().unwrap();

let mut hasher = Sha1::new();
hasher.update(&dashboard_assets_bytes);
let sha1_dashboard = hex::encode(hasher.finalize());
let dashboard_assets_bytes = get(url)?.bytes()?;

assert_eq!(meta["sha1"].as_str().unwrap(), sha1_dashboard);
let mut hasher = Sha1::new();
hasher.update(&dashboard_assets_bytes);
let sha1 = hex::encode(hasher.finalize());

let dashboard_dir = cargo_manifest_dir.join("mini-dashboard");
create_dir_all(&dashboard_dir)?;
let cursor = Cursor::new(&dashboard_assets_bytes);
let mut zip = zip::read::ZipArchive::new(cursor)?;
zip.extract(&dashboard_dir)?;
resource_dir(&dashboard_dir).build()?;
Ok(())
assert_eq!(meta["sha1"].as_str().unwrap(), sha1, "Downloaded mini-dashboard shasum differs from the one specified in the Cargo.toml");

create_dir_all(&dashboard_dir)?;
let cursor = Cursor::new(&dashboard_assets_bytes);
let mut zip = zip::read::ZipArchive::new(cursor)?;
zip.extract(&dashboard_dir)?;
resource_dir(&dashboard_dir).build()?;

// Write the sha1 for the dashboard back to file.
let mut file = OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open(sha1_path)?;

file.write_all(sha1.as_bytes())?;
file.flush()?;

Ok(())
}
}

1 change: 0 additions & 1 deletion meilisearch-http/public/bulma.min.css

This file was deleted.

Loading

0 comments on commit 4bbee4a

Please sign in to comment.