swissgeol URL Shortener | Read-only mirror of https://github.com/swisstopo/swissgeol-viewer-app-abbreviator — Bundesamt für Landestopografie swisstopo. Issues & pull requests at the source.
  • Rust 88.9%
  • Dockerfile 10.1%
  • Shell 1%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
vladyslav-tk 2ebd1f0914
Merge pull request #10 from swisstopo/GSNGM-1178_move_to_GH_packages
Use github repository to store Docker images
2024-08-08 14:51:14 +03:00
.github/workflows Use github repository to store Docker images 2024-08-05 16:01:48 +03:00
migrations Reuse id if url is already shortened 2022-02-07 13:44:15 +01:00
src Use clap for config 2022-05-20 18:55:10 +02:00
tests Use clap for config 2022-05-20 18:55:10 +02:00
.env Reuse id if url is already shortened 2022-02-07 13:44:15 +01:00
.gitignore Initial commit 2021-12-24 16:13:40 +01:00
Cargo.lock Use clap for config 2022-05-20 18:55:10 +02:00
Cargo.toml License as MIT/Apache-2.0 2022-05-20 19:08:23 +02:00
Dockerfile Facilitate storage persistence with unprivileged user 2022-01-17 15:19:37 +01:00
entrypoint.sh Facilitate storage persistence with unprivileged user 2022-01-17 15:19:37 +01:00
LICENSE-APACHE License as MIT/Apache-2.0 2022-05-20 19:08:23 +02:00
LICENSE-MIT License as MIT/Apache-2.0 2022-05-20 19:08:23 +02:00
README.md update repo in readme 2024-08-08 14:48:20 +03:00

abbreviator

URL Shortener, originally developed for the swissgeol subsurface viewer.

Quickstart

Spawn the docker image built from the main branch.

# run
docker run -d -p 8080:8080 \
    -e DATABASE_URL='sqlite::memory:' \
    -e HOST_WHITELIST='viewer.swissgeol.ch' \
    ghcr.io/swisstopo/swissgeol-viewer-app-abbreviator:main

This exposes the service on localhost:8080 with an in-memory sqlite database.

Sample request to shorten a url:

curl -v localhost:8080/ -d '{ "url": "https://viewer.swissgeol.ch/?layers=ch.swisstopo.geologie-geocover" }'

The Location header contains the shortened url to retrieve the original url:

# replace id with the last path segment of the shortened url
curl -v localhost:8080/{id}

This returns a response with status 301 and the original url in the Location header.

Endpoints

POST /

Expects a json body of the form:

{
    "url": "http://some.host/..."
}

Returns an empty response with status 201 Created and the shortened url in the Location header.

GET /{id}

Returns an empty response with status 301 Moved Permanently and the original url in the Location header.

GET /health_check

Checks the connection to the database and returns the CARGO_PKG_VERSION environment variable on success, 503 Service Unavailable otherwise.

Develop

Install Rust then clone the project and run it locally.

# clone
git clone git@github.com:swisstopo/swissgeol-viewer-app-abbreviator.git
cd abbreviator

# build and test
cargo test

# run locally
cargo run

Deploy

The following environment variables can be set to customize the service:

Variable Description
DATABASE_URL SQLite database url.
HOST_WHITELIST Whitespace separated list of allowed hosts of the URL to be shortened.
ID_LENGTH Length of the generated key, defaults to 5.
HOST Host the application listens to, defaults to 0.0.0.0.
PORT Port the application listens to, defaults to 8080.

To persist the database mount a volume or directory to /storage and let the DATABASE_URL point to there.

# build & run
docker build -t abbreviator:prod -f Dockerfile .
docker run -d -p 8080:8080 \
    -e DATABASE_URL='sqlite:///storage/prod.db' \
    -e HOST_WHITELIST='dev.swissgeol.ch int.swissgeol.ch viewer.swissgeol.ch swissgeol.ch' \
    -v `pwd`:/storage \
    abbreviator:prod

The app runs with an unprivileged user appuser that gets write access on startup, see entrypoint.sh

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.