diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 7314fd2..5e736bf 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -30,8 +30,10 @@ jobs: password: ${{ secrets.DOCKERHUB_TOKEN }} - name: Extract version tag id: extract_tag - if: github.event_name == 'release' - run: echo "RELEASE_TAG=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV + run: | + if [ "${{ github.event_name }}" == "release" ]; then + echo "RELEASE_TAG=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV + fi - name: Build and push image id: push uses: docker/build-push-action@v5.0.0 @@ -40,11 +42,11 @@ jobs: push: true platforms: linux/amd64,linux/arm64 tags: | - sjdonado/url-shortener:latest - ${{ github.event_name == 'release' && 'sjdonado/url-shortener:${{ env.RELEASE_TAG }}' || '' }} + sjdonado/bit:latest + ${{ github.event_name == 'release' && 'sjdonado/bit:${{ env.RELEASE_TAG }}' || '' }} - name: Attest uses: actions/attest-build-provenance@v1 id: attest with: - subject-name: sjdonado/url-shortener + subject-name: sjdonado/bit subject-digest: ${{ steps.push.outputs.digest }} diff --git a/Dockerfile b/Dockerfile index 3ae52f3..1995e85 100644 --- a/Dockerfile +++ b/Dockerfile @@ -16,4 +16,4 @@ COPY --from=build /usr/src/app/db db COPY --from=build /usr/src/app/bin /usr/local/bin EXPOSE 4000/tcp -CMD ["url-shortener"] +CMD ["bit"] diff --git a/README.md b/README.md index 90e721c..62ef748 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,15 @@ -# url-shortener +# bit + > Lightning fast, lightweight and minimal self-hosted url shortener ## Benchmark + ```shell ./benchmark.sh Semaphore initialized with 2666 slots. Setup... [+] Running 1/0 - ✔ Container url-shortener-app-1 Running 0.0s + ✔ Container bit-app-1 Running 0.0s 2024-05-20T16:39:53.818306Z INFO - micrate: No migrations to run. current version: 20240513130054 Captured API Key: 4y2mblZDneZLcI-YywHGFA Waiting for database to be ready... @@ -27,13 +29,14 @@ Accessing each link 10 times concurrently... Average Memory Usage: 11.00 MiB Average Response Time: 5.28 µs [+] Running 2/2 - ✔ Container url-shortener-app-1 Removed 10.2s - ✔ Network url-shortener_default Removed + ✔ Container bit-app-1 Removed 10.2s + ✔ Network bit_default Removed ``` ## Self-hosted - Run via docker-compose + ```bash docker-compose up @@ -42,56 +45,59 @@ docker-compose exec -it app cli --create-user=Admin ``` - Run via docker cli + ```bash docker run \ - --name url-shortener \ + --name bit \ -p 4000:4000 \ -e ENV="production" \ -e DATABASE_URL="sqlite3://./sqlite/data.db?journal_mode=wal&synchronous=normal&foreign_keys=true" \ -e APP_URL="http://localhost:4000" \ - sjdonado/url-shortener + sjdonado/bit -docker exec -it url-shortener migrate -docker exec -it url-shortener cli --create-user=Admin +docker exec -it bit migrate +docker exec -it bit cli --create-user=Admin ``` - Dokku + ```dockerfile -FROM sjdonado/url-shortener +FROM sjdonado/bit ``` ```bash -dokku apps:create url-shortener +dokku apps:create bit -dokku domains:set url-shortener bit.donado.co -dokku letsencrypt:enable url-shortener +dokku domains:set bit bit.donado.co +dokku letsencrypt:enable bit -dokku storage:ensure-directory url-shortener-sqlite -dokku storage:mount url-shortener /var/lib/dokku/data/storage/url-shortener-sqlite:/usr/src/app/sqlite/ +dokku storage:ensure-directory bit-sqlite +dokku storage:mount bit /var/lib/dokku/data/storage/bit-sqlite:/usr/src/app/sqlite/ -dokku config:set url-shortener DATABASE_URL="sqlite3://./sqlite/data.db?journal_mode=wal&synchronous=normal&foreign_keys=true" APP_URL=https://bit.donado.co +dokku config:set bit DATABASE_URL="sqlite3://./sqlite/data.db?journal_mode=wal&synchronous=normal&foreign_keys=true" APP_URL=https://bit.donado.co -dokku ports:add url-shortener http:80:4000 -dokku ports:add url-shortener https:443:4000 +dokku ports:add bit http:80:4000 +dokku ports:add bit https:443:4000 -dokku run url-shortener migrate -dokku run url-shortener cli --create-user=Admin +dokku run bit migrate +dokku run bit cli --create-user=Admin ``` ## Usage **REST API** -| Endpoint | HTTP Method | Description | Payload | -|----------|-------------|-------------|---------| -| `/api/ping` | GET | Ping the API to check if it's running | - | -| `/:slug` | GET | Retrieve a link by its slug | - | -| `/api/links` | GET | Retrieve all links | - | -| `/api/links` | POST | Create a new link | `{"url": "https://example.com"}` | -| `/api/links/:id` | PUT | Update an existing link by its ID | `{"url": "https://newexample.com"}` | -| `/api/links/:id` | DELETE | Delete a link by its ID | - | +| Endpoint | HTTP Method | Description | Payload | +| ---------------- | ----------- | ------------------------------------- | ----------------------------------- | +| `/api/ping` | GET | Ping the API to check if it's running | - | +| `/:slug` | GET | Retrieve a link by its slug | - | +| `/api/links` | GET | Retrieve all links | - | +| `/api/links` | POST | Create a new link | `{"url": "https://example.com"}` | +| `/api/links/:id` | PUT | Update an existing link by its ID | `{"url": "https://newexample.com"}` | +| `/api/links/:id` | DELETE | Delete a link by its ID | - | **CLI** + ``` Usage: ./cli [options] Options: @@ -111,7 +117,7 @@ brew install micrate ```bash shards run migrate -shards run url-shortener +shards run bit ``` **Generate the `X-Api-Key`** @@ -121,13 +127,14 @@ shards run cli -- --create-user=Admin ``` ## Run tests + ```bash ENV=test crystal spec ``` ## Contributing -1. Fork it () +1. Fork it () 2. Create your feature branch (`git checkout -b my-new-feature`) 3. Commit your changes (`git commit -am 'Add some feature'`) 4. Push to the branch (`git push origin my-new-feature`) diff --git a/benchmark.sh b/benchmark.sh index e4c4c3d..f010e94 100755 --- a/benchmark.sh +++ b/benchmark.sh @@ -21,7 +21,7 @@ echo "Semaphore initialized with $max_concurrent_processes slots." function get_resource_usage { while true; do - docker stats --no-stream --format "{{.MemUsage}} {{.CPUPerc}}" url-shortener >> resource_usage.txt + docker stats --no-stream --format "{{.MemUsage}} {{.CPUPerc}}" bit >> resource_usage.txt sleep $resource_usage_interval done } diff --git a/url-shortener.cr b/bit.cr similarity index 100% rename from url-shortener.cr rename to bit.cr diff --git a/shard.yml b/shard.yml index d21dd60..96ab0d4 100644 --- a/shard.yml +++ b/shard.yml @@ -1,12 +1,12 @@ -name: url-shortener -version: 0.1.0 +name: bit +version: 1.0.0 authors: - Juan Rodriguez targets: - url-shortener: - main: url-shortener.cr + bit: + main: bit.cr cli: main: scripts/cli.cr migrate: @@ -29,6 +29,6 @@ development_dependencies: spec-kemal: github: kemalcr/spec-kemal -crystal: '>= 1.12.1' +crystal: ">= 1.12.1" license: MIT diff --git a/spec/spec_helper.cr b/spec/spec_helper.cr index d06b1e1..cf08aa8 100644 --- a/spec/spec_helper.cr +++ b/spec/spec_helper.cr @@ -3,7 +3,7 @@ require "uuid" require "spec-kemal" require "micrate" -require "../url-shortener" +require "../bit" Spec.before_suite do Micrate::DB.connection_url = ENV["DATABASE_URL"]