Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new package bearmark-api #16

Merged
merged 1 commit into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 4 additions & 62 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
[package]
name = "bearmark"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[workspace]
members = [".", "bearmark-ql"]
members = ["bearmark-api", "bearmark-ql"]
resolver = "2"

[workspace.dependencies]
# allocation
Expand All @@ -17,58 +12,5 @@ tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
# initialization
ctor = "0.2.8"

[dependencies]
rocket = { version = "0.5.1", features = ["json"] }
rocket_db_pools = "0.2.0"

# database
diesel = { version = "2.2.0", features = ["postgres", "time"] }
diesel-async = { version = "0.5.0", features = [
"async-connection-wrapper",
"postgres",
"deadpool",
] }
diesel_migrations = { version = "2.2.0", features = ["postgres"] }

#####################
# search enhancement
#####################
bearmark-ql = { path = "bearmark-ql" }
# allocation
bumpalo.workspace = true

##################
# other utilities
##################
serde = { version = "1.0.203", features = ["derive"] }
rand = "0.8.5"
itertools = "0.13.0"
time = { version = "0.3.36", features = ["local-offset", "macros", "serde"] }
percent-encoding = "2.3.1"
# read settings from the dotenv file
dotenvy = "0.15.7"
# logging
tracing.workspace = true
tracing-appender.workspace = true
tracing-subscriber.workspace = true
# async runtime
tokio = { version = "1.38.0", features = ["rt", "macros", "sync"] }
futures = "0.3.30"
# error handling
anyhow = "1.0.86"
thiserror = "1.0.63"

[dev-dependencies]
# setup for tests
ctor.workspace = true

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

[lints.rust]
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(tarpaulin_include)'] }

[target.x86_64-unknown-linux-musl.dependencies]
openssl-sys = { version = "0.9.102", features = ["vendored"] }
pq-sys = { version = "0.6.1", features = ["bundled"] }
# members
bearmark-ql = { path = "./bearmark-ql" }
60 changes: 60 additions & 0 deletions bearmark-api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
[package]
name = "bearmark-api"
version = "0.1.0"
edition = "2021"

[dependencies]
rocket = { version = "0.5.1", features = ["json"] }
rocket_db_pools = "0.2.0"

# database
diesel = { version = "2.2.0", features = ["postgres", "time"] }
diesel-async = { version = "0.5.0", features = [
"async-connection-wrapper",
"postgres",
"deadpool",
] }
diesel_migrations = { version = "2.2.0", features = ["postgres"] }

#####################
# search enhancement
#####################
bearmark-ql.workspace = true
# allocation
bumpalo.workspace = true

##################
# other utilities
##################
serde = { version = "1.0.203", features = ["derive"] }
rand = "0.8.5"
itertools = "0.13.0"
time = { version = "0.3.36", features = ["local-offset", "macros", "serde"] }
percent-encoding = "2.3.1"
# read settings from the dotenv file
dotenvy = "0.15.7"
# logging
tracing.workspace = true
tracing-appender.workspace = true
tracing-subscriber.workspace = true
# async runtime
tokio = { version = "1.38.0", features = ["rt", "macros", "sync"] }
futures = "0.3.30"
# error handling
anyhow = "1.0.86"
thiserror = "1.0.63"

[dev-dependencies]
# setup for tests
ctor.workspace = true

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

[lints.rust]
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(tarpaulin_include)'] }

[target.x86_64-unknown-linux-musl.dependencies]
openssl-sys = { version = "0.9.102", features = ["vendored"] }
pq-sys = { version = "0.6.1", features = ["bundled"] }
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
10 changes: 5 additions & 5 deletions src/bin/serve.rs → bearmark-api/src/bin/serve.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#[macro_use]
extern crate rocket;

use bearmark::api::configs::Config;
use bearmark::api::fairings::db::Db;
use bearmark::api::{bookmark, folder, tag};
use bearmark_api::api::configs::Config;
use bearmark_api::api::fairings::db::Db;
use bearmark_api::api::{bookmark, folder, tag};

use rocket::fairing::AdHoc;
use rocket::fs::FileServer;
Expand All @@ -12,8 +12,8 @@ use rocket_db_pools::Database;
#[launch]
#[cfg(not(tarpaulin_include))]
async fn rocket() -> _ {
bearmark::utils::logging::setup_console_log();
bearmark::db::connection::run_migrations().await;
bearmark_api::utils::logging::setup_console_log();
bearmark_api::db::connection::run_migrations().await;

rocket::build()
.mount("/", FileServer::from("./static"))
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/db/connection.rs → bearmark-api/src/db/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub async fn run_migrations() {
use diesel_async::async_connection_wrapper::AsyncConnectionWrapper;
use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};

const MIGRATIONS: EmbeddedMigrations = embed_migrations!("./migrations");
const MIGRATIONS: EmbeddedMigrations = embed_migrations!("../migrations");
let conn = establish().await;

let mut async_wrapper: AsyncConnectionWrapper<AsyncPgConnection> =
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
199 changes: 103 additions & 96 deletions scripts/cli.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,117 +4,124 @@ set -o errexit
set -o nounset
set -o pipefail
if [[ "${TRACE-0}" == "1" ]]; then
set -o xtrace
set -o xtrace
fi

help() {
echo "Usage: ./cli.sh <action>"
echo "Actions:"
echo " -h, --help: Display this help message"
echo ""
echo " lint: Run clippy"
echo ""
echo " setup: Setup the project development environment"
echo " teardown: Teardown the project development environment"
echo " db-console: Open the database console"
echo " serve: Run the api server"
echo " test: Run tests"
echo " coverage: Run tests with coverage"
echo ""
echo " install-deps: Install all dependencies"
echo " install-diesel: Install diesel_cli"
echo " install-tarpaulin: Install tarpaulin"
echo "Usage: ./cli.sh <action>"
echo "Actions:"
echo " -h, --help: Display this help message"
echo ""
echo " lint: Run clippy"
echo ""
echo " setup: Setup the project development environment"
echo " teardown: Teardown the project development environment"
echo " db-console: Open the database console"
echo " serve: Run the api server"
echo " test: Run tests"
echo " coverage: Run tests with coverage"
echo ""
echo " install-deps: Install all dependencies"
echo " install-diesel: Install diesel_cli"
echo " install-tarpaulin: Install tarpaulin"
}

cleanup_profraw_files() {
find . -type f -name "*.profraw" -delete
find . -type f -name "*.profraw" -delete
}

cd "$(dirname "$0")"/../

tarpaulin_args="--workspace --include-tests --skip-clean --out html -- --show-output --test-threads 1"
tarpaulin_xml_args="--workspace --include-tests --skip-clean --out html -- --show-output --test-threads 1"

main() {
action=${1-}
action=${1-}

# shift if length of arguments is greater than 0
[[ $# -gt 0 ]] && shift 1
# shift if length of arguments is greater than 0
[[ $# -gt 0 ]] && shift 1

case $action in
"" | "-h" | "--help")
help
exit
;;
"lint")
echo ">>> Running clippy"
cargo clippy --all-features "$@"
cleanup_profraw_files
;;
"setup")
if [[ -z "${CI-}" ]]; then
echo ">>> Setting up the project development environment"
docker compose up -d --wait
case $action in
"" | "-h" | "--help")
help
exit
;;
"lint")
echo ">>> Running clippy"
cargo clippy --all-features "$@"
cleanup_profraw_files
;;
"setup")
if [[ -z "${CI-}" ]]; then
echo ">>> Setting up the project development environment"
docker compose up -d --wait

echo "DATABASE_URL=postgres://postgres:example@${POSTGRES_HOST-localhost}:${POSTGRES_PORT-5432}/${POSTGRES_DB-bearmark}" >.env
echo ">>> Setting up database"
./scripts/bin/diesel migration run
else
echo ">>> Skip setting up the project development environment"
echo "DATABASE_URL=postgres://postgres:example@${POSTGRES_HOST-localhost}:${POSTGRES_PORT-5432}/${POSTGRES_DB-bearmark}" >.env
echo ">>> Setting up database"
./scripts/bin/diesel migration run
else
echo ">>> Skip setting up the project development environment"

echo "DATABASE_URL=postgres://postgres:example@${POSTGRES_HOST-db}:${POSTGRES_PORT-5432}/${POSTGRES_DB-bearmark}" >.env
echo ">>> Setting up database"
diesel migration run
fi
echo ">>> Done"
;;
"teardown")
echo ">>> Tearing down the project development environment"
docker compose down
echo ">>> Done"
;;
"db-console")
docker compose exec db psql -Upostgres ${POSTGRES_DB-bearmark}
;;
"serve")
echo ">>> Running the api server"
cargo run --bin serve
;;
"test")
echo ">>> Running tests"
cargo test -- --show-output --test-threads 1 "$@"
cleanup_profraw_files
;;
"coverage")
echo ">>> Running tests with coverage"
if [[ -z "${CI-}" ]]; then
./scripts/bin/cargo-tarpaulin --out html --skip-clean -- --show-output --test-threads 1 "$@"
else
cargo tarpaulin --out html --skip-clean -- --show-output --test-threads 1 "$@"
fi
echo "open file ./tarpaulin-report.html to see coverage report"
cleanup_profraw_files
;;
"coverage-xml")
echo ">>> Running tests with coverage"
cargo tarpaulin --out xml --skip-clean -- --show-output --test-threads 1 "$@"
cleanup_profraw_files
;;
"install-deps")
$0 install-diesel
$0 install-tarpaulin
;;
"install-diesel")
echo ">>> Installing diesel_cli"
cargo install diesel_cli --no-default-features --features postgres --root ./scripts
;;
"install-tarpaulin")
echo ">>> Installing tarpaulin"
cargo install cargo-tarpaulin --root ./scripts
;;
*)
echo "Error: Unknown action '$action'"
help
exit
;;
esac
echo "DATABASE_URL=postgres://postgres:example@${POSTGRES_HOST-db}:${POSTGRES_PORT-5432}/${POSTGRES_DB-bearmark}" >.env
echo ">>> Setting up database"
diesel migration run
fi
echo ">>> Done"
;;
"teardown")
echo ">>> Tearing down the project development environment"
docker compose down
echo ">>> Done"
;;
"db-console")
docker compose exec db psql -Upostgres ${POSTGRES_DB-bearmark}
;;
"serve")
echo ">>> Running the api server"
cargo run --package bearmark-api --bin serve
;;
"test")
echo ">>> Running tests"
cargo test --workspace -- --show-output --test-threads 1 "$@"
cleanup_profraw_files
;;
"coverage")
echo ">>> Running tests with coverage"
if [[ -z "${CI-}" ]]; then
./scripts/bin/cargo-tarpaulin $tarpaulin_args "$@"
else
cargo tarpaulin $tarpaulin_args "$@"
fi
echo "open file ./tarpaulin-report.html to see coverage report"
cleanup_profraw_files
;;
"coverage-xml")
echo ">>> Running tests with coverage"
if [[ -z "${CI-}" ]]; then
./scripts/bin/cargo-tarpaulin $tarpaulin_xml_args "$@"
else
cargo tarpaulin $tarpaulin_xml_args "$@"
fi
cleanup_profraw_files
;;
"install-deps")
$0 install-diesel
$0 install-tarpaulin
;;
"install-diesel")
echo ">>> Installing diesel_cli"
cargo install diesel_cli --no-default-features --features postgres --root ./scripts
;;
"install-tarpaulin")
echo ">>> Installing tarpaulin"
cargo install cargo-tarpaulin --root ./scripts --git https://github.com/xd009642/tarpaulin.git
;;
*)
echo "Error: Unknown action '$action'"
help
exit
;;
esac
}

main "$@"
Loading