From c5cfba85f99d3dfc7ea9bd3cfde86744da0f18ef Mon Sep 17 00:00:00 2001 From: SinTan1729 Date: Sun, 24 Mar 2024 19:12:38 -0500 Subject: [PATCH 01/20] chg: Allow specifying target in the Dockerfile --- Dockerfile | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index 1c1fdff..d79faac 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,3 +1,6 @@ +ARG target=x86_64-unknown-linux-musl +ENV target=$target + FROM lukemathwalker/cargo-chef:latest-rust-slim AS chef WORKDIR /chhoto-url @@ -12,15 +15,15 @@ RUN rustup target add x86_64-unknown-linux-musl COPY --from=planner /chhoto-url/recipe.json recipe.json # Build dependencies - this is the caching Docker layer -RUN cargo chef cook --release --target=x86_64-unknown-linux-musl --recipe-path recipe.json +RUN cargo chef cook --release --target=$target --recipe-path recipe.json COPY ./actix/Cargo.toml ./actix/Cargo.lock . COPY ./actix/src ./src # Build application -RUN cargo build --release --target=x86_64-unknown-linux-musl --locked --bin chhoto-url +RUN cargo build --release --target=$target --locked --bin chhoto-url FROM scratch -COPY --from=builder /chhoto-url/target/x86_64-unknown-linux-musl/release/chhoto-url /chhoto-url +COPY --from=builder /chhoto-url/target/$target/release/chhoto-url /chhoto-url COPY ./resources /resources ENTRYPOINT ["/chhoto-url"] From 9221c3e3711dcaa26cf477a4d83ae14ac38eb411 Mon Sep 17 00:00:00 2001 From: SinTan1729 Date: Sun, 24 Mar 2024 19:38:33 -0500 Subject: [PATCH 02/20] chg: Add support for specifying the architecture --- Dockerfile | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index d79faac..b2f6b89 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,3 @@ -ARG target=x86_64-unknown-linux-musl -ENV target=$target - FROM lukemathwalker/cargo-chef:latest-rust-slim AS chef WORKDIR /chhoto-url @@ -10,8 +7,9 @@ COPY ./actix/src ./src RUN cargo chef prepare --recipe-path recipe.json FROM chef as builder +ARG target=x86_64-unknown-linux-musl RUN apt-get update && apt-get install -y musl-tools -RUN rustup target add x86_64-unknown-linux-musl +RUN rustup target add $target COPY --from=planner /chhoto-url/recipe.json recipe.json # Build dependencies - this is the caching Docker layer @@ -23,6 +21,7 @@ COPY ./actix/src ./src RUN cargo build --release --target=$target --locked --bin chhoto-url FROM scratch +ARG target=x86_64-unknown-linux-musl COPY --from=builder /chhoto-url/target/$target/release/chhoto-url /chhoto-url COPY ./resources /resources ENTRYPOINT ["/chhoto-url"] From 5bd174d287500e39e31654c28750e4dd3936402e Mon Sep 17 00:00:00 2001 From: SinTan1729 Date: Sun, 24 Mar 2024 23:03:18 -0500 Subject: [PATCH 03/20] build: Switched to a cross-rt based multi-arch build process --- Dockerfile | 28 ++++------------------------ Makefile | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 24 deletions(-) create mode 100644 Makefile diff --git a/Dockerfile b/Dockerfile index b2f6b89..567cb8e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,28 +1,8 @@ -FROM lukemathwalker/cargo-chef:latest-rust-slim AS chef -WORKDIR /chhoto-url - -FROM chef as planner -COPY ./actix/Cargo.toml ./actix/Cargo.lock . -COPY ./actix/src ./src -RUN cargo chef prepare --recipe-path recipe.json - -FROM chef as builder -ARG target=x86_64-unknown-linux-musl -RUN apt-get update && apt-get install -y musl-tools -RUN rustup target add $target - -COPY --from=planner /chhoto-url/recipe.json recipe.json -# Build dependencies - this is the caching Docker layer -RUN cargo chef cook --release --target=$target --recipe-path recipe.json - -COPY ./actix/Cargo.toml ./actix/Cargo.lock . -COPY ./actix/src ./src -# Build application -RUN cargo build --release --target=$target --locked --bin chhoto-url - FROM scratch -ARG target=x86_64-unknown-linux-musl -COPY --from=builder /chhoto-url/target/$target/release/chhoto-url /chhoto-url +ARG ARCH=linux/amd64 + +COPY $ARCH/chhoto-url /chhoto-url COPY ./resources /resources + ENTRYPOINT ["/chhoto-url"] diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..62da911 --- /dev/null +++ b/Makefile @@ -0,0 +1,18 @@ +setup: + cargo install cross + +build: + cross build --release --locked --manifest-path=actix/Cargo.toml --target aarch64-unknown-linux-musl + cross build --release --locked --manifest-path=actix/Cargo.toml --target x86_64-unknown-linux-musl + +docker: build + mkdir -p linux/amd64 linux/arm64 + cp actix/target/aarch64-unknown-linux-musl/release/chhoto-url linux/aarch64/ + cp actix/target/x86_64-unknown-linux-musl/release/chhoto-url linux/amd64/ + docker buildx -t chhoto-url --platform linux/amd64,linux/arm64,linux/arm/v7 . + rm -rf linux + +clean: + cargo clean --manifest-path=actix/Cargo.toml + +.PHONY: build clean docker From ffb48462390cfc6c8dc4b656dd575fb5c4e13bc0 Mon Sep 17 00:00:00 2001 From: SinTan1729 Date: Sun, 24 Mar 2024 23:27:31 -0500 Subject: [PATCH 04/20] fix: Multi-arch upload --- .gitignore | 3 ++- Dockerfile | 4 ++-- Makefile | 12 +++++++----- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index b8d6911..4fd7ce8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ -# Ignore cargo build outputs +# Ignore build outputs actix/target +.docker # Ignore SQLite file urls.sqlite diff --git a/Dockerfile b/Dockerfile index 567cb8e..8560074 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,7 @@ FROM scratch -ARG ARCH=linux/amd64 +ARG TARGETARCH -COPY $ARCH/chhoto-url /chhoto-url +COPY .docker/$TARGETARCH/chhoto-url /chhoto-url COPY ./resources /resources ENTRYPOINT ["/chhoto-url"] diff --git a/Makefile b/Makefile index 62da911..5a2e232 100644 --- a/Makefile +++ b/Makefile @@ -1,18 +1,20 @@ setup: cargo install cross + docker buildx create --use --platform=linux/arm64,linux/amd64 --name multi-platform-builder + docker buildx inspect --bootstrap build: cross build --release --locked --manifest-path=actix/Cargo.toml --target aarch64-unknown-linux-musl cross build --release --locked --manifest-path=actix/Cargo.toml --target x86_64-unknown-linux-musl docker: build - mkdir -p linux/amd64 linux/arm64 - cp actix/target/aarch64-unknown-linux-musl/release/chhoto-url linux/aarch64/ - cp actix/target/x86_64-unknown-linux-musl/release/chhoto-url linux/amd64/ - docker buildx -t chhoto-url --platform linux/amd64,linux/arm64,linux/arm/v7 . - rm -rf linux + mkdir -p .docker/amd64 .docker/arm64 + cp actix/target/aarch64-unknown-linux-musl/release/chhoto-url .docker/arm64/ + cp actix/target/x86_64-unknown-linux-musl/release/chhoto-url .docker/amd64/ + docker buildx build --push --tag sintan1729/chhoto-url:dev --platform linux/amd64,linux/arm64 . clean: cargo clean --manifest-path=actix/Cargo.toml + rm -rf .docker .PHONY: build clean docker From 2baa481040f9bdba21f21af5d8ea27519c345ec1 Mon Sep 17 00:00:00 2001 From: SinTan1729 Date: Sun, 24 Mar 2024 23:33:41 -0500 Subject: [PATCH 05/20] chg: Change the push script to work with the new setup --- Makefile | 2 +- docker_push_script.sh | 28 +++------------------------- 2 files changed, 4 insertions(+), 26 deletions(-) diff --git a/Makefile b/Makefile index 5a2e232..8aa8dac 100644 --- a/Makefile +++ b/Makefile @@ -17,4 +17,4 @@ clean: cargo clean --manifest-path=actix/Cargo.toml rm -rf .docker -.PHONY: build clean docker +.PHONY: build diff --git a/docker_push_script.sh b/docker_push_script.sh index c3d5156..e5d7eba 100755 --- a/docker_push_script.sh +++ b/docker_push_script.sh @@ -1,10 +1,7 @@ #!/bin/env bash if [ "$1" == "dev" ]; then - name="chhoto-url" - docker build -t $name . - docker tag $name sintan1729/$name:dev - docker push sintan1729/$name:dev + docker buildx build --push --tag sintan1729/$name:dev --platform linux/amd64 elif [ "$1" == "release" ]; then v_patch=$(cat actix/Cargo.toml | sed -rn 's/^version = "(.+)"$/\1/p') @@ -12,26 +9,7 @@ elif [ "$1" == "release" ]; then v_major=$(echo $v_minor | sed -rn 's/^(.+)\..+$/\1/p') name="chhoto-url" - - docker build -t $name . - - for tag in $v_major $v_minor $v_patch latest - do - docker tag $name sintan1729/$name:$tag - done - - echo "Do you want to push these to Docker Hub?" - select yn in "Yes" "No"; - do - if [ "$yn"="Yes" ]; then - for tag in $v_major $v_minor $v_patch latest - do - docker push sintan1729/$name:$tag - done - else - echo "Okay! Not pushing." - fi - break - done + docker buildx build --push --tag sintan1729/$name:$v_major --tag sintan1729/$v_minor: \ + --tag sintan1729/$name:$v_patch --tag sintan1729/$name:latest --platform linux/amd64,linux/arm64 fi From 82559d38fd194ff1ca8afd80ab17c6678056756e Mon Sep 17 00:00:00 2001 From: SinTan1729 Date: Mon, 25 Mar 2024 00:56:39 -0500 Subject: [PATCH 06/20] chg: Nicer multi-arch builds and also add armv7 --- Dockerfile | 8 -------- Dockerfile-for-push-script | 15 +++++++++++++++ Makefile | 6 ++---- 3 files changed, 17 insertions(+), 12 deletions(-) delete mode 100644 Dockerfile create mode 100644 Dockerfile-for-push-script diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index 8560074..0000000 --- a/Dockerfile +++ /dev/null @@ -1,8 +0,0 @@ -FROM scratch -ARG TARGETARCH - -COPY .docker/$TARGETARCH/chhoto-url /chhoto-url -COPY ./resources /resources - -ENTRYPOINT ["/chhoto-url"] - diff --git a/Dockerfile-for-push-script b/Dockerfile-for-push-script new file mode 100644 index 0000000..abb460f --- /dev/null +++ b/Dockerfile-for-push-script @@ -0,0 +1,15 @@ +FROM scratch as builder-amd64 +COPY ./actix/target/x86_64-unknown-linux-musl/release/chhoto-url /chhoto-url + +FROM scratch as builder-arm64 +COPY ./actix/target/aarch64-unknown-linux-musl/release/chhoto-url /chhoto-url + +FROM scratch as builder-arm +COPY ./actix/target/armv7-unknown-linux-musleabihf/release/chhoto-url /chhoto-url + +ARG TARGETARCH +FROM builder-$TARGETARCH +COPY ./resources /resources + +ENTRYPOINT ["/chhoto-url"] + diff --git a/Makefile b/Makefile index 8aa8dac..55480f1 100644 --- a/Makefile +++ b/Makefile @@ -5,13 +5,11 @@ setup: build: cross build --release --locked --manifest-path=actix/Cargo.toml --target aarch64-unknown-linux-musl + cross build --release --locked --manifest-path=actix/Cargo.toml --target armv7-unknown-linux-musleabihf cross build --release --locked --manifest-path=actix/Cargo.toml --target x86_64-unknown-linux-musl docker: build - mkdir -p .docker/amd64 .docker/arm64 - cp actix/target/aarch64-unknown-linux-musl/release/chhoto-url .docker/arm64/ - cp actix/target/x86_64-unknown-linux-musl/release/chhoto-url .docker/amd64/ - docker buildx build --push --tag sintan1729/chhoto-url:dev --platform linux/amd64,linux/arm64 . + docker buildx build --push --tag sintan1729/chhoto-url:dev --platform linux/amd64,linux/arm64,linux/arm/v7 . clean: cargo clean --manifest-path=actix/Cargo.toml From e54aa3b33b2e14e061af6caa67cee49e5592620c Mon Sep 17 00:00:00 2001 From: SinTan1729 Date: Mon, 25 Mar 2024 01:00:25 -0500 Subject: [PATCH 07/20] build: Use different dockerfiles for single and multi-arch builds --- Dockerfile | 26 +++++++++++++++++++ ...le-for-push-script => Dockerfile.multiarch | 0 Makefile | 2 +- 3 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 Dockerfile rename Dockerfile-for-push-script => Dockerfile.multiarch (100%) diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..1c1fdff --- /dev/null +++ b/Dockerfile @@ -0,0 +1,26 @@ +FROM lukemathwalker/cargo-chef:latest-rust-slim AS chef +WORKDIR /chhoto-url + +FROM chef as planner +COPY ./actix/Cargo.toml ./actix/Cargo.lock . +COPY ./actix/src ./src +RUN cargo chef prepare --recipe-path recipe.json + +FROM chef as builder +RUN apt-get update && apt-get install -y musl-tools +RUN rustup target add x86_64-unknown-linux-musl + +COPY --from=planner /chhoto-url/recipe.json recipe.json +# Build dependencies - this is the caching Docker layer +RUN cargo chef cook --release --target=x86_64-unknown-linux-musl --recipe-path recipe.json + +COPY ./actix/Cargo.toml ./actix/Cargo.lock . +COPY ./actix/src ./src +# Build application +RUN cargo build --release --target=x86_64-unknown-linux-musl --locked --bin chhoto-url + +FROM scratch +COPY --from=builder /chhoto-url/target/x86_64-unknown-linux-musl/release/chhoto-url /chhoto-url +COPY ./resources /resources +ENTRYPOINT ["/chhoto-url"] + diff --git a/Dockerfile-for-push-script b/Dockerfile.multiarch similarity index 100% rename from Dockerfile-for-push-script rename to Dockerfile.multiarch diff --git a/Makefile b/Makefile index 55480f1..457dd34 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,7 @@ build: cross build --release --locked --manifest-path=actix/Cargo.toml --target x86_64-unknown-linux-musl docker: build - docker buildx build --push --tag sintan1729/chhoto-url:dev --platform linux/amd64,linux/arm64,linux/arm/v7 . + docker buildx build --push --tag sintan1729/chhoto-url:dev --platform linux/amd64,linux/arm64,linux/arm/v7 -f Dockerfile.multiarch . clean: cargo clean --manifest-path=actix/Cargo.toml From db5d1f72bd56eabfc86ba0dba844605f503df0aa Mon Sep 17 00:00:00 2001 From: SinTan1729 Date: Mon, 25 Mar 2024 01:05:06 -0500 Subject: [PATCH 08/20] chg: Use username as a variable in Makefile --- Dockerfile | 1 - Makefile | 5 +++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index 1c1fdff..070b64d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -23,4 +23,3 @@ FROM scratch COPY --from=builder /chhoto-url/target/x86_64-unknown-linux-musl/release/chhoto-url /chhoto-url COPY ./resources /resources ENTRYPOINT ["/chhoto-url"] - diff --git a/Makefile b/Makefile index 457dd34..7b56ab2 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,5 @@ +USERNAME := ${USERNAME} + setup: cargo install cross docker buildx create --use --platform=linux/arm64,linux/amd64 --name multi-platform-builder @@ -9,10 +11,9 @@ build: cross build --release --locked --manifest-path=actix/Cargo.toml --target x86_64-unknown-linux-musl docker: build - docker buildx build --push --tag sintan1729/chhoto-url:dev --platform linux/amd64,linux/arm64,linux/arm/v7 -f Dockerfile.multiarch . + docker buildx build --push --tag ${USERNAME}/chhoto-url:dev --platform linux/amd64,linux/arm64,linux/arm/v7 -f Dockerfile.multiarch . clean: cargo clean --manifest-path=actix/Cargo.toml - rm -rf .docker .PHONY: build From ce76f04f35e725dd8a2cbd8698f7703ed931b849 Mon Sep 17 00:00:00 2001 From: SinTan1729 Date: Mon, 25 Mar 2024 01:10:09 -0500 Subject: [PATCH 09/20] build: Support specifying targets with build-arg --- Dockerfile | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index 070b64d..abd13ca 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,19 +7,20 @@ COPY ./actix/src ./src RUN cargo chef prepare --recipe-path recipe.json FROM chef as builder +ARG target=x86_64-unknown-linux-musl RUN apt-get update && apt-get install -y musl-tools -RUN rustup target add x86_64-unknown-linux-musl +RUN rustup target add $target COPY --from=planner /chhoto-url/recipe.json recipe.json # Build dependencies - this is the caching Docker layer -RUN cargo chef cook --release --target=x86_64-unknown-linux-musl --recipe-path recipe.json +RUN cargo chef cook --release --target=$target --recipe-path recipe.json COPY ./actix/Cargo.toml ./actix/Cargo.lock . COPY ./actix/src ./src # Build application -RUN cargo build --release --target=x86_64-unknown-linux-musl --locked --bin chhoto-url +RUN cargo build --release --target=$target --locked --bin chhoto-url FROM scratch -COPY --from=builder /chhoto-url/target/x86_64-unknown-linux-musl/release/chhoto-url /chhoto-url +COPY --from=builder /chhoto-url/target/$target/release/chhoto-url /chhoto-url COPY ./resources /resources ENTRYPOINT ["/chhoto-url"] From 629e66a57c9657fe234b974016bd90fba7a0ba1b Mon Sep 17 00:00:00 2001 From: SinTan1729 Date: Mon, 25 Mar 2024 01:22:54 -0500 Subject: [PATCH 10/20] docs: Updated build instructions --- Makefile | 2 +- README.md | 10 ++++++++-- docker_push_script.sh | 2 +- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 7b56ab2..c9a0555 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -USERNAME := ${USERNAME} +USERNAME := ${DOCKER_USERNAME} setup: cargo install cross diff --git a/README.md b/README.md index 993442f..b70e5a6 100644 --- a/README.md +++ b/README.md @@ -75,10 +75,16 @@ place, resulting in possibly unwanted behavior. ## Building and running with docker ### `docker run` method -0. (Only if you really want to) Build the image +0. (Only if you really want to) Build the image for the default `x86_64-unknown-linux-musl` target: ``` -docker build . -t chhoto-url:latest +docker build . -t chhoto-url ``` +For building on `arm64` or `arm/v7`, use the following: +``` +docker build . -t chhoto-url --build-arg target= +``` +For cross-compilation, take a look at the `Makefile`. It builds and pushes for `linux/amd64`, `linux/aarch64` +and `linux/arm/v7` architectures. For any other architectures, open a discussion, and I'll try to help you out. 1. Run the image ``` docker run -p 4567:4567 diff --git a/docker_push_script.sh b/docker_push_script.sh index e5d7eba..3e95cd2 100755 --- a/docker_push_script.sh +++ b/docker_push_script.sh @@ -1,7 +1,7 @@ #!/bin/env bash if [ "$1" == "dev" ]; then - docker buildx build --push --tag sintan1729/$name:dev --platform linux/amd64 + docker buildx build --push --tag sintan1729/$name:dev . elif [ "$1" == "release" ]; then v_patch=$(cat actix/Cargo.toml | sed -rn 's/^version = "(.+)"$/\1/p') From 6d3d220cff74027ebd1cd7928737a4984e9d088d Mon Sep 17 00:00:00 2001 From: SinTan1729 Date: Mon, 25 Mar 2024 01:26:13 -0500 Subject: [PATCH 11/20] build: Fix the docker_push_script --- docker_push_script.sh | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/docker_push_script.sh b/docker_push_script.sh index 3e95cd2..7a1787b 100755 --- a/docker_push_script.sh +++ b/docker_push_script.sh @@ -7,9 +7,10 @@ elif [ "$1" == "release" ]; then v_patch=$(cat actix/Cargo.toml | sed -rn 's/^version = "(.+)"$/\1/p') v_minor=$(echo $v_patch | sed -rn 's/^(.+\..+)\..+$/\1/p') v_major=$(echo $v_minor | sed -rn 's/^(.+)\..+$/\1/p') - + + make build name="chhoto-url" docker buildx build --push --tag sintan1729/$name:$v_major --tag sintan1729/$v_minor: \ - --tag sintan1729/$name:$v_patch --tag sintan1729/$name:latest --platform linux/amd64,linux/arm64 + --tag sintan1729/$name:$v_patch --tag sintan1729/$name:latest --platform linux/amd64,linux/arm64,linux/arm/v7 -f Dockerfile.multiarch . fi From dfefff2703074fc8e76c90f710be0d28b56f0295 Mon Sep 17 00:00:00 2001 From: SinTan1729 Date: Mon, 25 Mar 2024 01:40:10 -0500 Subject: [PATCH 12/20] docs: Added a note about building --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index b70e5a6..0e019be 100644 --- a/README.md +++ b/README.md @@ -83,6 +83,7 @@ For building on `arm64` or `arm/v7`, use the following: ``` docker build . -t chhoto-url --build-arg target= ``` +Make sure that the desired target is a `musl` one, since the docker image is built from `scratch`. For cross-compilation, take a look at the `Makefile`. It builds and pushes for `linux/amd64`, `linux/aarch64` and `linux/arm/v7` architectures. For any other architectures, open a discussion, and I'll try to help you out. 1. Run the image From 1f18766f797ceb83cb0841fd134c634ef0491011 Mon Sep 17 00:00:00 2001 From: SinTan1729 Date: Mon, 25 Mar 2024 16:02:52 -0500 Subject: [PATCH 13/20] build: Switch workflow to only use Makefile --- .gitignore | 3 ++- Makefile | 31 ++++++++++++++++++++++++++----- docker_push_script.sh | 16 ---------------- 3 files changed, 28 insertions(+), 22 deletions(-) delete mode 100755 docker_push_script.sh diff --git a/.gitignore b/.gitignore index 4fd7ce8..da49308 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,5 @@ # Ignore build outputs actix/target -.docker # Ignore SQLite file urls.sqlite @@ -8,3 +7,5 @@ urls.sqlite # Ignore irrelevant dotfiles .vscode/ **/.directory +.env + diff --git a/Makefile b/Makefile index c9a0555..9e87a43 100644 --- a/Makefile +++ b/Makefile @@ -1,19 +1,40 @@ -USERNAME := ${DOCKER_USERNAME} +# .env file has the variables $DOCKER_USERNAME and $PASSWORD defined +include .env setup: cargo install cross + rustup target add x86_64-unknown-linux-musl docker buildx create --use --platform=linux/arm64,linux/amd64 --name multi-platform-builder docker buildx inspect --bootstrap -build: +build-dev: + cargo build --release --locked --manifest-path=actix/Cargo.toml --target x86_64-unknown-linux-musl + +docker-local: build-dev + docker build --tag chhoto-url --build-arg TARGETARCH=amd64 -f Dockerfile.multiarch . + +docker-test: docker-local + docker ps -q --filter "name=chhoto-url" | xargs -r docker stop + docker ps -aq --filter "name=chhoto-url" | xargs -r docker rm + docker run -p 4567:4567 --name chhoto-url -e password="${PASSWORD}" -d chhoto-url + docker logs chhoto-url -f + +docker-dev: build-dev + docker build --push --tag ${DOCKER_USERNAME}:chhoto-url:dev --build-arg TARGETARCH=amd64 -f Dockerfile.multiarch . + +build-release: cross build --release --locked --manifest-path=actix/Cargo.toml --target aarch64-unknown-linux-musl cross build --release --locked --manifest-path=actix/Cargo.toml --target armv7-unknown-linux-musleabihf cross build --release --locked --manifest-path=actix/Cargo.toml --target x86_64-unknown-linux-musl -docker: build - docker buildx build --push --tag ${USERNAME}/chhoto-url:dev --platform linux/amd64,linux/arm64,linux/arm/v7 -f Dockerfile.multiarch . +V_PATCH := $(shell cat actix/Cargo.toml | sed -rn 's/^version = "(.+)"$$/\1/p') +V_MINOR := $(shell cat actix/Cargo.toml | sed -rn 's/^version = "(.+)\..+"$$/\1/p') +V_MAJOR := $(shell cat actix/Cargo.toml | sed -rn 's/^version = "(.+)\..+\..+"$$/\1/p') +docker-release: build-release + docker buildx build --push --tag ${DOCKER_USERNAME}/chhoto-url:${V_MAJOR} --tag ${DOCKER_USERNAME}/chhoto-url:${V_MINOR} --tag ${DOCKER_USERNAME}/chhoto-url:${V_PATCH} \ + ---tag ${DOCKER_USERNAME}/chhoto-url:latest -platform linux/amd64,linux/arm64,linux/arm/v7 -f Dockerfile.multiarch . clean: cargo clean --manifest-path=actix/Cargo.toml -.PHONY: build +.PHONY: build-dev docker-local build-release diff --git a/docker_push_script.sh b/docker_push_script.sh deleted file mode 100755 index 7a1787b..0000000 --- a/docker_push_script.sh +++ /dev/null @@ -1,16 +0,0 @@ -#!/bin/env bash - -if [ "$1" == "dev" ]; then - docker buildx build --push --tag sintan1729/$name:dev . - -elif [ "$1" == "release" ]; then - v_patch=$(cat actix/Cargo.toml | sed -rn 's/^version = "(.+)"$/\1/p') - v_minor=$(echo $v_patch | sed -rn 's/^(.+\..+)\..+$/\1/p') - v_major=$(echo $v_minor | sed -rn 's/^(.+)\..+$/\1/p') - - make build - name="chhoto-url" - docker buildx build --push --tag sintan1729/$name:$v_major --tag sintan1729/$v_minor: \ - --tag sintan1729/$name:$v_patch --tag sintan1729/$name:latest --platform linux/amd64,linux/arm64,linux/arm/v7 -f Dockerfile.multiarch . -fi - From 0cfa6740297db91df5df25504e21927509e280a2 Mon Sep 17 00:00:00 2001 From: SinTan1729 Date: Mon, 25 Mar 2024 16:08:04 -0500 Subject: [PATCH 14/20] fix: Typo in Makefile --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 9e87a43..30dac00 100644 --- a/Makefile +++ b/Makefile @@ -20,7 +20,7 @@ docker-test: docker-local docker logs chhoto-url -f docker-dev: build-dev - docker build --push --tag ${DOCKER_USERNAME}:chhoto-url:dev --build-arg TARGETARCH=amd64 -f Dockerfile.multiarch . + docker build --push --tag ${DOCKER_USERNAME}/chhoto-url:dev --build-arg TARGETARCH=amd64 -f Dockerfile.multiarch . build-release: cross build --release --locked --manifest-path=actix/Cargo.toml --target aarch64-unknown-linux-musl From 731cb416466d084d915d1afcebd0738f71763977 Mon Sep 17 00:00:00 2001 From: SinTan1729 Date: Mon, 25 Mar 2024 16:44:47 -0500 Subject: [PATCH 15/20] chg: Added stopping the docker image to make clean --- Makefile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Makefile b/Makefile index 30dac00..faeb1bd 100644 --- a/Makefile +++ b/Makefile @@ -35,6 +35,8 @@ docker-release: build-release ---tag ${DOCKER_USERNAME}/chhoto-url:latest -platform linux/amd64,linux/arm64,linux/arm/v7 -f Dockerfile.multiarch . clean: + docker ps -q --filter "name=chhoto-url" | xargs -r docker stop + docker ps -aq --filter "name=chhoto-url" | xargs -r docker rm cargo clean --manifest-path=actix/Cargo.toml .PHONY: build-dev docker-local build-release From c76b39dc161f047682900b66fffc07fe9bc82394 Mon Sep 17 00:00:00 2001 From: SinTan1729 Date: Mon, 25 Mar 2024 17:28:24 -0500 Subject: [PATCH 16/20] fix: Typo in Makefile, and break lines --- Makefile | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index faeb1bd..95502c9 100644 --- a/Makefile +++ b/Makefile @@ -31,8 +31,9 @@ V_PATCH := $(shell cat actix/Cargo.toml | sed -rn 's/^version = "(.+)"$$/\1/p') V_MINOR := $(shell cat actix/Cargo.toml | sed -rn 's/^version = "(.+)\..+"$$/\1/p') V_MAJOR := $(shell cat actix/Cargo.toml | sed -rn 's/^version = "(.+)\..+\..+"$$/\1/p') docker-release: build-release - docker buildx build --push --tag ${DOCKER_USERNAME}/chhoto-url:${V_MAJOR} --tag ${DOCKER_USERNAME}/chhoto-url:${V_MINOR} --tag ${DOCKER_USERNAME}/chhoto-url:${V_PATCH} \ - ---tag ${DOCKER_USERNAME}/chhoto-url:latest -platform linux/amd64,linux/arm64,linux/arm/v7 -f Dockerfile.multiarch . + docker buildx build --push --tag ${DOCKER_USERNAME}/chhoto-url:${V_MAJOR} --tag ${DOCKER_USERNAME}/chhoto-url:${V_MINOR} \ + --tag ${DOCKER_USERNAME}/chhoto-url:${V_PATCH} --tag ${DOCKER_USERNAME}/chhoto-url:latest \ + --platform linux/amd64,linux/arm64,linux/arm/v7 -f Dockerfile.multiarch . clean: docker ps -q --filter "name=chhoto-url" | xargs -r docker stop From 5e4db14ea23da12c3227d4b2078f05edad74519b Mon Sep 17 00:00:00 2001 From: SinTan1729 Date: Tue, 26 Mar 2024 17:46:26 -0500 Subject: [PATCH 17/20] docs: Some changes to the README --- README.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 0e019be..aa74f67 100644 --- a/README.md +++ b/README.md @@ -18,8 +18,12 @@ If you feel like a feature is missing, please let me know by creating an issue using the "feature request" template. ## But why another URL shortener? -I've looked at a couple popular URL shorteners, however they either have -unnecessary features, or they didn't have all the features I wanted. +Most URL shorteners are either bloated with unnecessary features, or are a pain to set up. +Even fewer are written with simplicity and lightness in mind. When I saw the simply-shorten +project (linked below), I really liked the idea but thought that it missed some details. Also, +I didn't like the fact that a simple app like this had a ~200 MB docker image (mostly due to the +included java runtime). So, I decided to rewrite it in Rust and add some features to it that I +thought were essential (e.g. hit counting). ## What does the name mean? Chhoto (ছোট, [IPA](https://en.wikipedia.org/wiki/Help:IPA/Bengali): /tʃʰoʈo/) is the Bangla word From 5d8dd6fb630c5bfbb20f383e48e46e332d920e3d Mon Sep 17 00:00:00 2001 From: SinTan1729 Date: Tue, 26 Mar 2024 23:52:24 -0500 Subject: [PATCH 18/20] chg: Use Option instead of returning empty String --- actix/src/database.rs | 15 ++++----------- actix/src/main.rs | 7 +++---- actix/src/utils.rs | 6 +++--- 3 files changed, 10 insertions(+), 18 deletions(-) diff --git a/actix/src/database.rs b/actix/src/database.rs index c939c14..53e32e6 100644 --- a/actix/src/database.rs +++ b/actix/src/database.rs @@ -1,20 +1,13 @@ use rusqlite::Connection; -pub fn find_url(shortlink: &str, db: &Connection) -> String { +pub fn find_url(shortlink: &str, db: &Connection) -> Option { let mut statement = db .prepare_cached("SELECT long_url FROM urls WHERE short_url = ?1") .unwrap(); - let links = statement - .query_map([shortlink], |row| row.get("long_url")) - .unwrap(); - - let mut longlink = String::new(); - for link in links { - longlink = link.unwrap(); - } - - longlink + statement + .query_row([shortlink], |row| row.get("long_url")) + .ok() } pub fn getall(db: &Connection) -> Vec { diff --git a/actix/src/main.rs b/actix/src/main.rs index 1c2419c..40cc920 100644 --- a/actix/src/main.rs +++ b/actix/src/main.rs @@ -78,10 +78,7 @@ async fn error404() -> impl Responder { #[get("/{shortlink}")] async fn link_handler(shortlink: web::Path, data: web::Data) -> impl Responder { let shortlink_str = shortlink.to_string(); - let longlink = utils::get_longurl(shortlink_str, &data.db); - if longlink.is_empty() { - Redirect::to("/err/404") - } else { + if let Some(longlink) = utils::get_longurl(shortlink_str, &data.db) { let redirect_method = env::var("redirect_method").unwrap_or(String::from("PERMANENT")); database::add_hit(shortlink.as_str(), &data.db); if redirect_method == "TEMPORARY" { @@ -90,6 +87,8 @@ async fn link_handler(shortlink: web::Path, data: web::Data) - // Defaults to permanent redirection Redirect::to(longlink).permanent() } + } else { + Redirect::to("/err/404") } } diff --git a/actix/src/utils.rs b/actix/src/utils.rs index b474fe5..ba64e66 100644 --- a/actix/src/utils.rs +++ b/actix/src/utils.rs @@ -3,11 +3,11 @@ use rand::seq::SliceRandom; use regex::Regex; use rusqlite::Connection; -pub fn get_longurl(shortlink: String, db: &Connection) -> String { +pub fn get_longurl(shortlink: String, db: &Connection) -> Option { if validate_link(&shortlink) { database::find_url(shortlink.as_str(), db) } else { - String::new() + None } } @@ -35,7 +35,7 @@ pub fn add_link(req: String, db: &Connection) -> (bool, String) { shortlink = random_name(); } - if validate_link(shortlink.as_str()) && get_longurl(shortlink.clone(), db).is_empty() { + if validate_link(shortlink.as_str()) && get_longurl(shortlink.clone(), db).is_none() { ( database::add_link(shortlink.clone(), longlink, db), shortlink, From f526e7ec5b356cc34d0dd2d0ec677d946734151b Mon Sep 17 00:00:00 2001 From: SinTan1729 Date: Thu, 28 Mar 2024 13:27:33 -0500 Subject: [PATCH 19/20] chg: 404 response doesn't change the url --- actix/src/main.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/actix/src/main.rs b/actix/src/main.rs index 40cc920..8d5901f 100644 --- a/actix/src/main.rs +++ b/actix/src/main.rs @@ -6,7 +6,7 @@ use actix_web::{ http::StatusCode, middleware, post, web::{self, Redirect}, - App, HttpResponse, HttpServer, Responder, + App, Either, HttpResponse, HttpServer, Responder, }; use rusqlite::Connection; use std::env; @@ -82,13 +82,18 @@ async fn link_handler(shortlink: web::Path, data: web::Data) - let redirect_method = env::var("redirect_method").unwrap_or(String::from("PERMANENT")); database::add_hit(shortlink.as_str(), &data.db); if redirect_method == "TEMPORARY" { - Redirect::to(longlink) + Either::Left(Redirect::to(longlink)) } else { // Defaults to permanent redirection - Redirect::to(longlink).permanent() + Either::Left(Redirect::to(longlink).permanent()) } } else { - Redirect::to("/err/404") + Either::Right( + NamedFile::open_async("./resources/static/404.html") + .await + .customize() + .with_status(StatusCode::NOT_FOUND), + ) } } From fbcb0882607fde1a7efbf5f7e754c9ffb6cbf805 Mon Sep 17 00:00:00 2001 From: SinTan1729 Date: Thu, 28 Mar 2024 13:28:28 -0500 Subject: [PATCH 20/20] chg: Updated deps --- actix/Cargo.lock | 84 ++++++++++++++++++++++++------------------------ 1 file changed, 42 insertions(+), 42 deletions(-) diff --git a/actix/Cargo.lock b/actix/Cargo.lock index 959f5dd..a03ed8c 100644 --- a/actix/Cargo.lock +++ b/actix/Cargo.lock @@ -8,7 +8,7 @@ version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5f7b0a21988c1bf877cf4759ef5ddaac04c1c9fe808c9142ecb78ba97d97a28a" dependencies = [ - "bitflags 2.4.2", + "bitflags 2.5.0", "bytes", "futures-core", "futures-sink", @@ -29,7 +29,7 @@ dependencies = [ "actix-service", "actix-utils", "actix-web", - "bitflags 2.4.2", + "bitflags 2.5.0", "bytes", "derive_more", "futures-core", @@ -54,7 +54,7 @@ dependencies = [ "actix-utils", "ahash", "base64 0.21.7", - "bitflags 2.4.2", + "bitflags 2.5.0", "brotli", "bytes", "bytestring", @@ -88,7 +88,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e01ed3140b2f8d422c68afa1ed2e85d996ea619c988ac834d255db32138655cb" dependencies = [ "quote", - "syn 2.0.53", + "syn 2.0.55", ] [[package]] @@ -217,7 +217,7 @@ dependencies = [ "actix-router", "proc-macro2", "quote", - "syn 2.0.53", + "syn 2.0.55", ] [[package]] @@ -285,9 +285,9 @@ dependencies = [ [[package]] name = "aho-corasick" -version = "1.1.2" +version = "1.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" dependencies = [ "memchr", ] @@ -369,15 +369,15 @@ checksum = "0952808a6c2afd1aa8947271f3a60f1a6763c7b912d210184c5149b5cf147247" [[package]] name = "autocfg" -version = "1.1.0" +version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" +checksum = "f1fdabc7756949593fe60f30ec81974b613357de856987752631dea1e3394c80" [[package]] name = "backtrace" -version = "0.3.69" +version = "0.3.71" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2089b7e3f35b9dd2d0ed921ead4f6d318c27680d4a5bd167b3ee120edb105837" +checksum = "26b05800d2e817c8b3b4b54abd461726265fa9789ae34330622f2db9ee696f9d" dependencies = [ "addr2line", "cc", @@ -408,9 +408,9 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] name = "bitflags" -version = "2.4.2" +version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ed570934406eb16438a4e976b1b4500774099c13b8cb96eec99f620f05090ddf" +checksum = "cf4b9d6a944f767f8e5e0db018570623c85f3d925ac718db4e06d0187adb21c1" [[package]] name = "block-buffer" @@ -444,9 +444,9 @@ dependencies = [ [[package]] name = "bytes" -version = "1.5.0" +version = "1.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a2bd12c1caf447e69cd4528f47f94d203fd2582878ecb9e9465484c4148a8223" +checksum = "514de17de45fdb8dc022b1a7975556c53c86f9f0aa5f534b98977b171857c2c9" [[package]] name = "bytestring" @@ -842,9 +842,9 @@ dependencies = [ [[package]] name = "indexmap" -version = "2.2.5" +version = "2.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7b0b929d511467233429c45a44ac1dcaa21ba0f5ba11e4879e6ed28ddb4f9df4" +checksum = "168fb715dda47215e360912c096649d23d58bf392ac62f73919e831745e40f26" dependencies = [ "equivalent", "hashbrown", @@ -861,9 +861,9 @@ dependencies = [ [[package]] name = "itoa" -version = "1.0.10" +version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c" +checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" [[package]] name = "jobserver" @@ -932,9 +932,9 @@ checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c" [[package]] name = "memchr" -version = "2.7.1" +version = "2.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149" +checksum = "6c8640c5d730cb13ebd907d8d04b52f55ac9a2eec55b440c8892f40d56c76c1d" [[package]] name = "mime" @@ -1136,9 +1136,9 @@ dependencies = [ [[package]] name = "regex" -version = "1.10.3" +version = "1.10.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b62dbe01f0b06f9d8dc7d49e05a0785f153b00b2c227856282f671e0318c9b15" +checksum = "c117dbdfde9c8308975b6a18d71f3f385c89461f7b3fb054288ecf2a2058ba4c" dependencies = [ "aho-corasick", "memchr", @@ -1159,9 +1159,9 @@ dependencies = [ [[package]] name = "regex-syntax" -version = "0.8.2" +version = "0.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" +checksum = "adad44e29e4c806119491a7f06f03de4d1af22c3a680dd47f1e6e179439d1f56" [[package]] name = "rusqlite" @@ -1169,7 +1169,7 @@ version = "0.31.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b838eba278d213a8beaf485bd313fd580ca4505a00d5871caeb1457c55322cae" dependencies = [ - "bitflags 2.4.2", + "bitflags 2.5.0", "fallible-iterator", "fallible-streaming-iterator", "hashlink", @@ -1227,14 +1227,14 @@ checksum = "7eb0b34b42edc17f6b7cac84a52a1c5f0e1bb2227e997ca9011ea3dd34e8610b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.53", + "syn 2.0.55", ] [[package]] name = "serde_json" -version = "1.0.114" +version = "1.0.115" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c5f09b1bd632ef549eaa9f60a1f8de742bdbc698e6cee2095fc84dde5f549ae0" +checksum = "12dc5c46daa8e9fdf4f5e71b6cf9a53f2487da0e86e55808e2d35539666497dd" dependencies = [ "itoa", "ryu", @@ -1295,9 +1295,9 @@ dependencies = [ [[package]] name = "smallvec" -version = "1.13.1" +version = "1.13.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e6ecd384b10a64542d77071bd64bd7b231f4ed5940fba55e98c3de13824cf3d7" +checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" [[package]] name = "socket2" @@ -1328,9 +1328,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.53" +version = "2.0.55" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7383cd0e49fff4b6b90ca5670bfd3e9d6a733b3f90c686605aa7eec8c4996032" +checksum = "002a1b3dbf967edfafc32655d0f377ab0bb7b994aa1d32c8cc7e9b8bf3ebb8f0" dependencies = [ "proc-macro2", "quote", @@ -1385,9 +1385,9 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" [[package]] name = "tokio" -version = "1.36.0" +version = "1.37.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "61285f6515fa018fb2d1e46eb21223fff441ee8db5d0f1435e8ab4f5cdb80931" +checksum = "1adbebffeca75fcfd058afa480fb6c0b81e165a0323f9c9d39c9697e37c46787" dependencies = [ "backtrace", "bytes", @@ -1670,32 +1670,32 @@ checksum = "9ce1b18ccd8e73a9321186f97e46f9f04b778851177567b1975109d26a08d2a6" dependencies = [ "proc-macro2", "quote", - "syn 2.0.53", + "syn 2.0.55", ] [[package]] name = "zstd" -version = "0.13.0" +version = "0.13.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bffb3309596d527cfcba7dfc6ed6052f1d39dfbd7c867aa2e865e4a449c10110" +checksum = "2d789b1514203a1120ad2429eae43a7bd32b90976a7bb8a05f7ec02fa88cc23a" dependencies = [ "zstd-safe", ] [[package]] name = "zstd-safe" -version = "7.0.0" +version = "7.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "43747c7422e2924c11144d5229878b98180ef8b06cca4ab5af37afc8a8d8ea3e" +checksum = "1cd99b45c6bc03a018c8b8a86025678c87e55526064e38f9df301989dce7ec0a" dependencies = [ "zstd-sys", ] [[package]] name = "zstd-sys" -version = "2.0.9+zstd.1.5.5" +version = "2.0.10+zstd.1.5.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9e16efa8a874a0481a574084d34cc26fdb3b99627480f785888deb6386506656" +checksum = "c253a4914af5bafc8fa8c86ee400827e83cf6ec01195ec1f1ed8441bf00d65aa" dependencies = [ "cc", "pkg-config",