Shurly, this is a URL shortener with API management
```sh
curl -v -H 'Content-Type: application/json' \ -H 'Authorization: Bearer tokentokentoken' \ -d '{ "slug": "the-one", "url": "https://www.example.com/" }' \ http://localhost:7000/api/destinations
curl -v http://localhost:7000/the-one
```
There are a couple of ways to run this, all depending on your preference:
Shurly can be directly installed with cargo install shurly
, this will place a
shurly
binary in your path (if the Cargo installation directly is in your
PATH
).
Building it yourself is also possible, of course, there are a couple more options. To start, you need to clone the repo.
sh
git clone git@github.com:workplacebuddy/shurley.git
Then it's only a cargo run
away. If [Docker] has your preference, then
building it is also possible.
sh
docker build --tag shurly .
Running it is the same as for all Docker container.
```sh docker run --rm --interactive --tty --publish 7000:7000 shurly .
docker run --rm -it -p 7000:7000 shurly . ```
Shurly has a very simple REST'ish interface to management the destinations and its properties.
Everything that is not matched by an API route will be handled by the root as a fallback, this root will look up a destination based on its path and will either redirect to that destination or show a 404.
Redirects are done based on the isPermanent
property of a destination;
Permanent redirects are done with the 308 (Permanent Redirect) status code and
the temporary redirect uses the 307 (Temporary Redirect) redirect. Both will
set the Location
header to the associated URL.
Only authorized users can manage destinations and need to get a token to access the other API endpoints.
```sh curl -v -H 'Content-Type: application/json' \ -d '{ "username": "admin", "password": "verysecret" }' \ http://localhost:7000/api/users/token
```
To create destinations the /api/destinations
URL can be posted to with a
payload to describe what needs to happen when.
```sh curl -v -H 'Content-Type: application/json' \ -H 'Authorization: Bearer tokentokentoken' \ -d '{ "slug": "some-easy-name", "url": "https://www.example.com/" }' \ http://localhost:7000/api/destinations
```
Optionally you can send the isPermanent
property, to indicate what kind of
redirect should be used. Permanent redirects can not be changed after they are
created.
Updating a destination happens in the same fashion.
```sh
curl -v XPATCH -H 'Content-Type: application/json' \
-H 'Authorization: Bearer tokentokentoken' \
-d '{ "url": "https://www.example.com/", "isPermanent": true }' \
http://localhost:7000/api/destinations/
```
The slug
can not be changed after creation. Changing the isPermanent
flag
to true
is possible, not the other way around. When isPermanent
is
true
, updating the url
will fail.
To remove the destination, a DELETE
endpoint is available.
sh
curl -v XDELETE \
-H 'Authorization: Bearer tokentokentoken' \
http://localhost:7000/api/destinations/<uuid>
This will soft-delete the destination; creating a new destination with the same slug is not possible: creativity is key.
There are a bunch more interactions available, but this should get you going.
By default Shurly runs its database in memory, loosing all data when it exits. This is not an ideal scenario when running Shurly in a production setting. This is why Shurly also comes with support for storing its database in a PostgreSQL database. To use this there is an optional feature that can be added to the installation.
```sh
cargo install shurly --features postgres
cargo run --features postgres
docker build --build-arg SHURLY_FEATURES=postgres --tag shurly . ```
An extra requirement is needed to actually run Shurly with a database, that is an actual database. This can be setup separately, or the [Docker Compose] setup can be used, which will run a PostgreSQL server container.
A simple docker compose up
will get you started. Use docker compose up
--build
to rebuild the Shurly image.
The Docker Compose setup also runs the Docker version of Shurly, this might not be ideal for fast development iterations. Docker Compose provides the option to only run a single container of the setup.
```sh
the-data
is the name of the PostgreSQL servicedocker compose up the-data ```
When running it without Docker, there needs to be a DATABASE_URL
environment
variable.
For those who like to use Docker, but don't want to go through the hassle of building the images; pre-built images are available:
memory
feature: [ghcr.io/workplacebuddy/shurly:master-memory
]postgres
feature: [ghcr.io/workplacebuddy/shurly:master-postgres
]When running with the defaults, missing configuration has a sane default oris automatically generated. For development this is fine, but running it in production has a different set of requirements. All configuration is done through environment variables.
[tracing
] is used for all logging (optional)
sh
RUST_LOG=shurly=debug,tower_http=debug
Secret for encoding JWT tokens, make sure this is long enough (optional, default: some random string)
sh
JWT_SECRET=
Connection string for PostgreSQL server (only required for postgres
feature).
When running the Docker Compose setup this will be provided.
sh
DATABASE_URL=
To communicate with the outside world, Shurly needs to bind to an address to accept connections.
```sh
ADDRESS=0.0.0.0:7000
PORT=7000 ```
On the first run there is a user created with some randomly generated
credentials; These credentials are displayed in the server log. The initial
credentials can be changed with the INITIAL_USERNAME
and INITIAL_PASSWORD
environment variables. When using these variables, they will not be output to
the log.
INITIAL_USERNAME
: Username of the first user for the first run (optional,
default: some UUIDv4)INITIAL_PASSWORD
: Password of the first user for the first run (optional,
default: something random)The environment variables can be set in a .env
file, see .env.default
for
an example.
Shurly uses SQLx
for all PostgreSQL database interactions, migrations are run
automatically on start up.
The migration files can be found in ./migrations
and are sorted on filename.
Working with migrations can be a bit of a hassle, the project does not build
properly without a valid database connection and the right schema. The Docker
image uses the SQLX_OFFLINE=true
environment variable to use the cached data
inside the sqlx-data.json
file. Running a compiled version of Shurly will
automatically run the migrations -- getting it compiled is the trick :)
Using the SQLx
CLI adds a couple of nicities to work with migrations.
cargo install sqlx-cli
, or with [other options].DATABASE_URL
environment variable is setcargo sqlx migrate run
cargo sqlx migrate revert
hits
, maybe?postgres
featureAnd, don't call me Shirly.