refactor: rename url-shortener to bit

This commit is contained in:
Juan Rodriguez
2024-07-10 22:25:17 +02:00
parent b0160f8127
commit 050fd6f1e3
7 changed files with 51 additions and 42 deletions
+7 -5
View File
@@ -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 }}
+1 -1
View File
@@ -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"]
+36 -29
View File
@@ -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 (<https://github.com/sjdonado/url-shortener/fork>)
1. Fork it (<https://github.com/sjdonado/bit/fork>)
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`)
+1 -1
View File
@@ -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
}
View File
+5 -5
View File
@@ -1,12 +1,12 @@
name: url-shortener
version: 0.1.0
name: bit
version: 1.0.0
authors:
- Juan Rodriguez <sjdonado@icloud.com>
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
+1 -1
View File
@@ -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"]