From bf204c1c8500ee6b2d8fa86a428f08021624ef1c Mon Sep 17 00:00:00 2001 From: mirdaki Date: Mon, 26 Sep 2022 05:02:54 +0000 Subject: [PATCH] Add docker files, docs, and pipelines Docker can be used to build and package the project as well as deploy it The docs should explain how to use and contribute to the project Publish pipelines should publish the crate and docker images --- .github/workflows/publish-container.yml | 47 +++++++ .github/workflows/publish-crate.yml | 26 ++++ .github/workflows/release.yml | 30 ++++ CODE_OF_CONDUCT.md | 132 ++++++++++++++++++ CONTRIBUTING.md | 43 ++++++ Dockerfile | 21 +++ README.md | 174 +++++++++++++++++++++--- docker-compose.yaml | 10 ++ 8 files changed, 461 insertions(+), 22 deletions(-) create mode 100644 .github/workflows/publish-container.yml create mode 100644 .github/workflows/publish-crate.yml create mode 100644 .github/workflows/release.yml create mode 100644 CODE_OF_CONDUCT.md create mode 100644 CONTRIBUTING.md create mode 100644 Dockerfile create mode 100644 docker-compose.yaml diff --git a/.github/workflows/publish-container.yml b/.github/workflows/publish-container.yml new file mode 100644 index 0000000..cdb2923 --- /dev/null +++ b/.github/workflows/publish-container.yml @@ -0,0 +1,47 @@ +# Based on https://docs.github.com/en/actions/publishing-packages/publishing-docker-images#publishing-images-to-docker-hub-and-github-packages + +on: + release: + types: [created] + +name: Publish Container image + +jobs: + push_to_registries: + name: Push Docker image to multiple registries + runs-on: ubuntu-latest + permissions: + packages: write + contents: read + steps: + - name: Check out the repo + uses: actions/checkout@v3 + + - name: Log in to Docker Hub + uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + + - name: Log in to the Container registry + uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38 + with: + images: | + mirdaki/urshort + ghcr.io/${{ github.repository }} + + - name: Build and push Docker images + uses: docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc + with: + context: . + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} diff --git a/.github/workflows/publish-crate.yml b/.github/workflows/publish-crate.yml new file mode 100644 index 0000000..fafffe4 --- /dev/null +++ b/.github/workflows/publish-crate.yml @@ -0,0 +1,26 @@ +# Based on https://pratikpc.medium.com/publishing-crates-using-github-actions-165ee67780e1 + +on: + release: + types: [created] + +name: Publish to Crates.io + +jobs: + publish: + name: Publish Crate + runs-on: ubuntu-latest + steps: + - name: Checkout sources + uses: actions/checkout@v2 + + - name: Install stable toolchain + uses: actions-rs/toolchain@v1 + with: + profile: minimal + toolchain: stable + override: true + + - run: cargo publish --token ${CRATES_TOKEN} + env: + CRATES_TOKEN: ${{ secrets.CRATES_TOKEN }} diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..5410244 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,30 @@ +# Based on https://github.com/rust-build/rust-build.action + +on: + release: + types: [created] + +name: Release Artifacts + +jobs: + release: + name: Release ${{ matrix.target }} + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + include: + - target: x86_64-pc-windows-gnu + archive: zip + - target: x86_64-unknown-linux-musl + archive: tar.gz tar.xz + - target: x86_64-apple-darwin + archive: zip + steps: + - uses: actions/checkout@master + - name: Compile and release + uses: rust-build/rust-build.action@latest + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + RUSTTARGET: ${{ matrix.target }} + ARCHIVE_TYPES: ${{ matrix.archive }} diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md new file mode 100644 index 0000000..e25e63b --- /dev/null +++ b/CODE_OF_CONDUCT.md @@ -0,0 +1,132 @@ +# Contributor Covenant Code of Conduct + +## Our Pledge + +We as members, contributors, and leaders pledge to make participation in our +community a harassment-free experience for everyone, regardless of age, body +size, visible or invisible disability, ethnicity, sex characteristics, gender +identity and expression, level of experience, education, socio-economic status, +nationality, personal appearance, race, caste, color, religion, or sexual +identity and orientation. + +We pledge to act and interact in ways that contribute to an open, welcoming, +diverse, inclusive, and healthy community. + +## Our Standards + +Examples of behavior that contributes to a positive environment for our +community include: + +* Demonstrating empathy and kindness toward other people +* Being respectful of differing opinions, viewpoints, and experiences +* Giving and gracefully accepting constructive feedback +* Accepting responsibility and apologizing to those affected by our mistakes, + and learning from the experience +* Focusing on what is best not just for us as individuals, but for the overall + community + +Examples of unacceptable behavior include: + +* The use of sexualized language or imagery, and sexual attention or advances of + any kind +* Trolling, insulting or derogatory comments, and personal or political attacks +* Public or private harassment +* Publishing others' private information, such as a physical or email address, + without their explicit permission +* Other conduct which could reasonably be considered inappropriate in a + professional setting + +## Enforcement Responsibilities + +Community leaders are responsible for clarifying and enforcing our standards of +acceptable behavior and will take appropriate and fair corrective action in +response to any behavior that they deem inappropriate, threatening, offensive, +or harmful. + +Community leaders have the right and responsibility to remove, edit, or reject +comments, commits, code, wiki edits, issues, and other contributions that are +not aligned to this Code of Conduct, and will communicate reasons for moderation +decisions when appropriate. + +## Scope + +This Code of Conduct applies within all community spaces, and also applies when +an individual is officially representing the community in public spaces. +Examples of representing our community include using an official e-mail address, +posting via an official social media account, or acting as an appointed +representative at an online or offline event. + +## Enforcement + +Instances of abusive, harassing, or otherwise unacceptable behavior may be +reported to the community leaders responsible for enforcement at +https://codecaptured.com/contact/. +All complaints will be reviewed and investigated promptly and fairly. + +All community leaders are obligated to respect the privacy and security of the +reporter of any incident. + +## Enforcement Guidelines + +Community leaders will follow these Community Impact Guidelines in determining +the consequences for any action they deem in violation of this Code of Conduct: + +### 1. Correction + +**Community Impact**: Use of inappropriate language or other behavior deemed +unprofessional or unwelcome in the community. + +**Consequence**: A private, written warning from community leaders, providing +clarity around the nature of the violation and an explanation of why the +behavior was inappropriate. A public apology may be requested. + +### 2. Warning + +**Community Impact**: A violation through a single incident or series of +actions. + +**Consequence**: A warning with consequences for continued behavior. No +interaction with the people involved, including unsolicited interaction with +those enforcing the Code of Conduct, for a specified period of time. This +includes avoiding interactions in community spaces as well as external channels +like social media. Violating these terms may lead to a temporary or permanent +ban. + +### 3. Temporary Ban + +**Community Impact**: A serious violation of community standards, including +sustained inappropriate behavior. + +**Consequence**: A temporary ban from any sort of interaction or public +communication with the community for a specified period of time. No public or +private interaction with the people involved, including unsolicited interaction +with those enforcing the Code of Conduct, is allowed during this period. +Violating these terms may lead to a permanent ban. + +### 4. Permanent Ban + +**Community Impact**: Demonstrating a pattern of violation of community +standards, including sustained inappropriate behavior, harassment of an +individual, or aggression toward or disparagement of classes of individuals. + +**Consequence**: A permanent ban from any sort of public interaction within the +community. + +## Attribution + +This Code of Conduct is adapted from the [Contributor Covenant][homepage], +version 2.1, available at +[https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1]. + +Community Impact Guidelines were inspired by +[Mozilla's code of conduct enforcement ladder][Mozilla CoC]. + +For answers to common questions about this code of conduct, see the FAQ at +[https://www.contributor-covenant.org/faq][FAQ]. Translations are available at +[https://www.contributor-covenant.org/translations][translations]. + +[homepage]: https://www.contributor-covenant.org +[v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html +[Mozilla CoC]: https://github.com/mozilla/diversity +[FAQ]: https://www.contributor-covenant.org/faq +[translations]: https://www.contributor-covenant.org/translations diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..3ce5043 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,43 @@ +# Contributing to URShort + +Thank you for helping out with the URshort! + +Following these guidelines helps us keep good project workflow. We appreciate you working with us on it. + +Please read our [CODE_OF_CONDUCT.md](CODE_OF_CONDUCT.md) to understand our community expectations. + +Keep in mind the focus of this project is primarily to support an individual self-hoster. Feel free to open a discussion on what that means to you or fork this if you'd like to do something different! + +## How to help? + +Please check the [Issues][Issues] for current task. If you see something that you would like to help with, ask about helping in a comment and we'll assign it too you. + +Checkout the [introduction.md](docs/introduction.md) file for a high level explanation of the project. + +### Report Bug + +Check to see if someone already reported this [bug][Bug Issues] already exists. If so then leave a comment on that issue. If not, please provide a detailed description of the bug. Include what was happening before the error, all settings, and test results. This helps us more quickly identify and solve any problems. Create an [Issue][Issues] with the description and add a bug label. We will do our best to respond quickly to it. + +### Request a Feature + +Check to see if the feature is already listed in our [feature requests][Feature Issues]. If it's not, describe the feature and why it would be beneficial. Create an [Issue][Issues] with this description and add a feature enhancement label. We will do our best to respond quickly to it. + +### Work on URshort + +If you'd like to fix bugs or add features to URshort, please submit an issue or comment on a relevant issue to share your intentions and ask any questions. + +Once you are ready to make the Pull Request, consult the [Pull Request Checklist](#Pull-Request-Checklist) + +## Pull Request Checklist + +Please do these things before you make your Pull Request + +- [ ] Add any relevant unit tests +- [ ] Make sure all unit tests pass with `cargo test` +- [ ] Use our [.editorconfig](.editorconfig) to help automatically style all files +- [ ] Format the code with `cargo fmt` +- [ ] Link the relevant issue the Pull Request addresses + +[Issues]: https://github.com/mirdaki/urshort/issues?q=is%3Aissue+is%3Aopen+sort%3Aupdated-desc +[Bug Issues]: https://github.com/mirdaki/urshort/labels/bug +[Feature Issues]: https://github.com/mirdaki/urshort/labels/enhancement diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..4283f14 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,21 @@ +FROM rust:1.63.0 AS chef +RUN cargo install cargo-chef +WORKDIR app + +FROM chef AS prepper +COPY . . +RUN cargo chef prepare --recipe-path recipe.json + +FROM chef AS builder +COPY --from=prepper /app/recipe.json recipe.json +RUN cargo chef cook --release --recipe-path recipe.json +COPY . . +RUN cargo build --release --bin urshort + +FROM debian:bullseye-slim AS runtime +WORKDIR /usr/local/bin +COPY --from=builder /app/target/release/urshort /usr/local/bin +ccccckl + +EXPOSE 54027 +ENTRYPOINT ["/usr/local/bin/urshort"] diff --git a/README.md b/README.md index 43557d6..acae88a 100644 --- a/README.md +++ b/README.md @@ -1,24 +1,154 @@ # URShort -A blazingly fast and amazingly simple URL shortener designed for use by a single person. - -Steps: -1. [X] Get a simple redirect working -2. [X] Work with multiple simple redirects -3. [X] Load multiple simple redirects from file -4. [X] Get pattern redirects working -5. [X] Load multiple pattern from file -- [X] Make 404 fallback -6. [X] Get short URL request working - - Standard, pattern, or consistent hash - - ~~Limit to allowed domains (both the hashes and the requester)~~ -7. ~~[ ] Support http -> https~~ -8. ~~[ ] Support rate limiting~~ -9. ~~[ ] Add telemetry option~~ -10. ~~[ ] Store data in database~~ -11. [X] HTML Page for load and 404 -12. [X] Clean up, try to remove clones -13. [ ] README instructions -14. [ ] Docker image and compose -15. [ ] Publish on Dockerhub and Crates -16. [X] Build task +A blazingly fast and amazingly simple URL shortener designed for self-hosters. + +Support regular vanity URI mappings as well as (Perl style) regex based mapping! + +It uses environmental variables to configure all matches and loads them into an in-memory model for near instant access! Designed for hosting with containers and for individual use. + +## Build + +Use the standard [Rust tools](https://www.rust-lang.org/tools/install). There is also a [Dev Container](https://code.visualstudio.com/docs/remote/create-dev-container) if you would prefer to run it that way. + +```bash +cargo build + +# Also recommended for linting suggestions +cargo clippy + +# And for consistent formatting +cargo fmt +``` + +For creating the Docker container. Use the included [Dockerfile](Dockerfile) and this: + +```bash +docker build -t urshort:latest . +``` + +## Installation + +It is recommended to use [Docker](https://www.docker.com/) to use URShort: + +```bash +docker pull mirdaki/urshort +``` + +If you have prefer, you can also install it via [cargo](https://doc.rust-lang.org/cargo/): + +```bash +cargo install urshort +``` + +Or download directly from the [releases](https://github.com/mirdaki/urshort/releases). + +## Configuration + +As all configuration is stored in environmental variables, it is recommenced to store them in an `.env` file for ease of tracking and updating. For example: + +```.env +URSHORT_STANDARD_URI_test=https://example.com/ +URSHORT_STANDARD_URI_test2=https://example.com/2 + +URSHORT_PATTERN_REGEX_0='^i(?P\d+)$' +URSHORT_PATTERN_URI_0='https://example.com/$index' +URSHORT_PATTERN_REGEX_1='^(\d+)$' +URSHORT_PATTERN_URI_1='https://example.com/$1' +``` + +### Standard Mapping + +A standard vanity mapping would do something like: `hello -> example.com/hello` + +Using this config: +- Path: `hello` +- Redirect: `https://example.com/hello` + +This is stored in a single environmental variable: + +```bash +# <> is used to indicate the values to be changes +URSHORT_STANDARD_URI_= + +# Actual example +URSHORT_STANDARD_URI_test=https://example.com/ +``` + +Standard mappings will override any regex mapping. + +### Regex Mapping + +A single regex pattern mapping could do something like: +``` +i1 -> example.com/1 +i42 -> example.com/42 +i9001 -> example.com/9001 +... +``` + +Using this config: +- Regex pattern: `'^i(?P\d+)$'` +- Pattern redirect `'https://example.com/$index'` + +This is stored in two single environmental variables. The first contains +```bash +# <> is used to indicate the values to be changes +# The first environmental variable determines the place in which the pattern is evaluated and the regex for that pattern +URSHORT_PATTERN_REGEX_='' +# The second environmental variable must match the place of the first and contains the pattern redirect +URSHORT_PATTERN_URI_='' + +# Actual example +URSHORT_PATTERN_REGEX_0='^i(?P\d+)$' +URSHORT_PATTERN_URI_0='https://example.com/$index' +``` + +Be sure to quote the values. Be careful with the order you have the mappings. + +### Port + +You can specify a port the service will use. If not give, the default of `54027` will be used. + +This is useful when running the service directly. If you use Docker, it is recommended to use the built in port mapping features instead of this. + +Example environmental variable: `URSHORT_PORT=7777` + +## Usage + +Please use a web server, such as [Nginx](https://nginx.org/en/) or [Traefik](https://traefik.io/) in front of URShort. + +[Docker compose](https://github.com/docker/compose) is the recommended way to run URShort. An example file is [here](docker-compose.yaml). Load that file with your `.env` file and run: + +```bash +docker-compose up +``` + +You can also use the Docker run command directly: + +```bash +docker run -d \ + --name=urshort \ + -p 54027:54027 \ + -e URSHORT_STANDARD_URI_test=https://example.com/ \ + -e URSHORT_PATTERN_REGEX_0='^i(?P\d+)$' \ + -e URSHORT_PATTERN_URI_0='https://example.com/$index' \ + --restart unless-stopped \ + mirdaki/urshort:latest +``` + +Or if you have the bare executable, run `urshort` at the location of you `.env` file (or after your configuration is loaded directly into the environment). + +## Built With + +Thank you to all the projects that helped make this possible! + +- [Rust](https://www.rust-lang.org/) for being an awesome language to build with +- [Axum](https://github.com/tokio-rs/axum) for doing the whole URL redirect thing + +## Contributing + +Please read [CONTRIBUTING.md](CONTRIBUTING.md) for how to contribute to the project. + +## License + +This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details. diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..fbfce63 --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,10 @@ +version: "3.8" +services: + urshort: + image: mirdaki/urshort:latest + container_name: urshort + env_file: + - .env + ports: + - 54027:54027 + restart: unless-stopped