Skip to content

Commit

Permalink
feat: just build
Browse files Browse the repository at this point in the history
  • Loading branch information
voznik committed May 28, 2024
1 parent 8be2e48 commit 1f456bf
Show file tree
Hide file tree
Showing 14 changed files with 114 additions and 134 deletions.
2 changes: 1 addition & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
.github
.vscode
docs
target
# target
tmp

# Files
Expand Down
3 changes: 0 additions & 3 deletions .envrc

This file was deleted.

5 changes: 0 additions & 5 deletions .gitattributes

This file was deleted.

1 change: 1 addition & 0 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ jobs:
name: Evaluate code coverage
runs-on: ubuntu-latest
steps:
- uses: extractions/setup-just@v1
- uses: actions/checkout@v3
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
Expand Down
80 changes: 79 additions & 1 deletion .justfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,80 @@
set export

export MUSL_TARGET_NAME := "ralertsinua-x86_64-unknown-linux-musl"
export MUSL_DIST := "target/distrib/'${MUSL_TARGET_NAME}'"
export RUST_LOG := "verbose"
export RUST_BACKTRACE := "1"

# foo := if env_var("RELEASE") == "true" { `get-something-from-release-database` } else { "dummy-value" }

clean:
rm -rf tests/data/.task tests/data/.config
#!/bin/sh
find target -mindepth 1 -maxdepth 1 ! -name "debug" ! -name "tmp" -exec rm -rf {} +

docs:
#!/bin/sh
curl --proto '=https' --tlsv1.2 -LsSf https://github.com/axodotdev/oranda/releases/latest/download/oranda-installer.sh | sh
oranda build --config-path docs/oranda.render.json

[confirm]
publish:
#!/bin/sh
# If crate A depends on crate B, B must come before A in this list
crates=(
ralertsinua-models
ralertsinua-http
ralertsinua-geo
)
for crate in "${crates[@]}"; do
echo "Publishing ${crate}"
(
cd "$crate"
cargo publish --no-verify
)
sleep 20
done

[group('build')]
build target_env="gnu":
#!/bin/sh
echo "Building for target_env: ${target_env}"
if [ "{{target_env}}" = "musl" ]; then
# INFO: https://github.com/clux/muslrust?tab=readme-ov-file#filesystem-permissions-on-local-builds
# Filesystem permissions on local builds
# When building locally, the permissions of the musl parts of the ./target artifacts dir will be owned by root and requires sudo rm -rf target/ to clear. This is an intended complexity tradeoff with user builds.
docker run \
-v cargo-cache:/root/.cargo/registry \
-v "$PWD:/volume" \
-e OPENSSL_LIB_DIR=/usr/lib \
-e OPENSSL_INCLUDE_DIR=/usr/include \
--rm -it clux/muslrust \
cargo build --release --no-default-features --features cache,reqwest-rustls-tls

# Create a directory for the files to be archived
mkdir -p $MUSL_DIST
cp target/x86_64-unknown-linux-musl/release/ralertsinua $MUSL_DIST
cp CHANGELOG.md LICENSE README.md $MUSL_DIST
tar -C target/distrib -cJf target/distrib/$MUSL_TARGET_NAME.tar.xz $MUSL_TARGET_NAME
else
cargo dist build
fi

find ./target -type f -name 'ralertsinua' -exec du -sh {} \;


[group('run')]
run mode="dev":
#!/bin/sh
echo "Running with mode: ${mode}"
if [ "{{mode}}" = "prod" ]; then
if test -f ./target/release/ralertsinua; then
./target/release/ralertsinua
else
echo "Binary not found. Run 'cargo build --release' first."
fi
elif [ "{{mode}}" = "ttyd" ]; then
docker build -f Dockerfile -t ralertsinua-ttyd .
docker run --env-file .env -p 7681:7681 --rm -it ralertsinua-ttyd:latest
else
cargo run
fi
11 changes: 6 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ build = "build.rs"
# and we don't rely on it for debugging that much.
debug = false

# [profile.release]
# panic = 'unwind'
[profile.release]
panic = 'unwind'
# debug = true

[workspace]
Expand All @@ -52,17 +52,18 @@ installers = ["shell"]
targets = [
"aarch64-apple-darwin",
"x86_64-apple-darwin",
"x86_64-unknown-linux-gnu",
"x86_64-pc-windows-msvc",
# "x86_64-unknown-linux-musl", # TODO: doens't work with cargo-dist, use `sh tools/musl.sh` instead to manually build for musl
"x86_64-unknown-linux-gnu",
"x86_64-unknown-linux-musl", # INFO: musl is built using clux/muslrust
]
# Publish jobs to run in CI
pr-run-mode = "plan"
# Whether to install an updater program
install-updater = false

# TODO: https://github.com/moonrepo/proto/blob/master/Cargo.toml#L71
# [workspace.metadata.dist.github-custom-runners]
[workspace.metadata.dist.github-custom-runners]
x86_64-unknown-linux-musl = "clux/muslrust"
# aarch64-unknown-linux-gnu = "buildjet-4vcpu-ubuntu-2204-arm"
# aarch64-unknown-linux-musl = "buildjet-4vcpu-ubuntu-2204-arm"

Expand Down
21 changes: 11 additions & 10 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
FROM raonigabriel/web-terminal

# Set the OpenSSL lib directory
ENV OPENSSL_LIB_DIR=/usr/lib
ENV OPENSSL_INCLUDE_DIR=/usr/include

ENV RELEASE_URL=https://github.com/voiceapiai/ralertsinua/releases/latest/download/ralertsinua-x86_64-unknown-linux-musl.tar.xz
ENV RUST_BACKTRACE=1

WORKDIR /home/ralertsinua

COPY ./tmp/ralertsinua .

RUN mv ralertsinua /usr/local/bin
# COPY /target/x86_64-unknown-linux-musl/release # This is for local testing
# RUN mv ralertsinua /usr/local/bin # This is for local testing

# # Install the latest musl version of ralertsinua
# ADD $RELEASE_URL ralertsinua.tar.xz
# RUN tar -xvJf ralertsinua.tar.xz && \
# find . -type f -name 'ralertsinua' -exec mv {} /usr/local/bin/ \; && \
# rm ralertsinua.tar.xz
# Install the latest musl version of ralertsinua
ADD $RELEASE_URL ralertsinua.tar.xz
RUN tar -xvJf ralertsinua.tar.xz && \
find . -type f -name 'ralertsinua' -exec mv {} /usr/local/bin/ \; && \
rm ralertsinua.tar.xz

HEALTHCHECK CMD ["ralertsinua", "--help"]

# Comment this line if you don't need ttyd, and then just run ralertsina
# CMD [ "ttyd", "-s", "3", "-t", "titleFixed=Rust alerts.in.ua TUI - ralertsinua", "-t", "rendererType=webgl", "-t", "disableLeaveAlert=true", "ralertsinua" ]
CMD [ "ralertsinua" ]

# TODO: https://github.com/colinmurphy1/docker-ttyd/blob/main/entrypoint.sh
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ curl --proto '=https' --tlsv1.2 -LsSf https://github.com/voiceapiai/ralertsinua/
```
### Download prebuilt binaries from the [releases page](https://github.com/voiceapiai/ralertsinua/releases/latest)

#### NOTE
Linux binaries compiled with musl and are light-weight, call straight into the kernel without other dynamic system library dependencies, can be shipped to most linux distributions without compatibility issues, and can be inserted as-is into lightweight docker images such as static distroless, scratch, or alpine.

## Usage

⚠️ Before you can use this library, you need to obtain an API token by submitting an [API request form](https://alerts.in.ua/api-request).
Expand Down
21 changes: 13 additions & 8 deletions ralertsinua-http/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ type Result<T> = miette::Result<T, ApiError>;
pub const API_BASE_URL: &str = "https://api.alerts.in.ua";
pub const API_VERSION: &str = "/v1";
pub const API_CACHE_SIZE: usize = 1000;
#[rustfmt::skip]
const CACHE_ENABLED_STR: &str = if cfg!(feature = "cache") { "enabled" } else { "disabled" };

const PKG: &str = env!("CARGO_PKG_NAME");
const USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"),);

pub struct AlertsInUaClient {
base_url: String,
Expand All @@ -38,21 +43,21 @@ impl std::fmt::Debug for AlertsInUaClient {
}

impl AlertsInUaClient {
const APP_USER_AGENT: &'static str =
concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"),);

pub fn new(base_url: &str, token: &str) -> Self {
let base_url = base_url.into();
let token = token.into();
let client = ClientBuilder::new()
.timeout(std::time::Duration::from_secs(10))
.user_agent(Self::APP_USER_AGENT)
.user_agent(USER_AGENT)
.build()
// building with these options cannot fail
.unwrap();

#[cfg(feature = "cache")]
let cache_manager = Arc::new(CacheManagerQuick::new(API_CACHE_SIZE));

log::debug!(target: PKG, "Caching is {}", CACHE_ENABLED_STR);

Self {
base_url,
token,
Expand Down Expand Up @@ -101,11 +106,11 @@ impl AlertsInUaClient {
// Configuring the request for the specific type (get/post/put/delete)
req = add_data(req);
// Finally performing the request and handling the response
log::trace!(target: env!("CARGO_PKG_NAME"), "Request {:?}", req);
log::trace!(target: PKG, "Request {:?}", req);
let res: Response = req.send().await.inspect_err(|e| {
log::error!(target: env!("CARGO_PKG_NAME"), "Error making request: {:?}", e);
log::error!(target: PKG, "Error making request: {:?}", e);
})?;
log::trace!(target: env!("CARGO_PKG_NAME"), "Response {:?}", res);
log::trace!(target: PKG, "Response {:?}", res);
// Making sure that the status code is OK
if let Err(err) = res.error_for_status_ref() {
let err = match err.status() {
Expand All @@ -130,7 +135,7 @@ impl AlertsInUaClient {
let data: Bytes = match res.status() {
#[cfg(feature = "cache")]
StatusCode::NOT_MODIFIED => {
log::trace!(target: env!("CARGO_PKG_NAME"), "Response status '304 Not Modified', return cached data");
log::trace!(target: PKG, "Response status '304 Not Modified', return cached data");
cached_data
}
_ => {
Expand Down
4 changes: 0 additions & 4 deletions tools/docs.sh

This file was deleted.

45 changes: 0 additions & 45 deletions tools/install-from-release.sh

This file was deleted.

26 changes: 0 additions & 26 deletions tools/musl.sh

This file was deleted.

19 changes: 0 additions & 19 deletions tools/publish.sh

This file was deleted.

7 changes: 0 additions & 7 deletions ttyd.sh

This file was deleted.

0 comments on commit 1f456bf

Please sign in to comment.