emarifer / url-shortener-axum-askama-htmx

A Full Stack Url Shortener App using Rust's Axum framework + </>HTMX & Askama

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Shortify - A Rust/Axum URL Shortener App


A Full Stack Url Shortener App using Rust's Axum framework + </>HTMX & Askama.

GitHub License Static Badge Static Badge


πŸ€” What Stack have we used?

In the implementation of this application we have used the following technologies:


Note

The use of </>htmx allows behavior similar to that of a SPA, without page reloads when switching from one route to another or when making requests (via AJAX) to the backend. Likewise, the _hyperscript library allows you to add some dynamic features to the frontend in a very easy way.


πŸ–ΌοΈ Screenshots:

Home page showing button to copy shortened URL and loading indicator & error handling pages:

    


πŸ‘¨β€πŸš€ Getting Started

How to build for production

Besides the obvious prerequisite of having Rust on your machine, you must have cargo watch installed for hot reloading when editing code.

Since we use the PostgreSQL database from a Docker container, it is necessary to have the latter also installed and execute this command in the project folder:

$ docker compose up -d

These other commands will also be useful to manage the database from its container:

$ docker start shortify-db # start container
$ docker stop shortify-db # stop container
$ docker exec -it shorturl-rs psql -U enrique -W shorturl # (pass: emarifer) access the database

You will also need to have the sqlx CLI installed (sqlx-cli, install it with the command cargo install sqlx-cli) to be able to do the migrations to the database. Run the following command in the migrations folder:

$ sqlx migrate run # sqlx migrate revert (to reverse the migration)

Before compiling the binary, you will have to regenerate the CSS. To do this, go to the tailwind folder and run the following (you must have Node.js installed):

$ cd tailwind && npm run build-css-prod

Build the binary file and run it with the command:

$ cargo build --release && ./target/release/shorturl-rs # Ctrl + C to stop the application

Build for development

If what you want is to edit the code, it will be more like activating hot reload:

$ cargo watch -x run -w src -w assets -w templates

⚠️ Since the docker compose up -d command creates a volume (as a hidden folder) in the project folder to store the data of the Postgres database and that folder requires system administrator permissions, the cargo command watch -x run would return an error. Therefore, we only "watch" the src, assets and templates folders.

If you also want to modify the CSS of the templates, it will be useful to activate the watch mode of Tailwindcss, executing the command inside the tailwind folder (you need to have Node.js installed):

$ npm run watch-css # minify for production: npm run build-css-prod

Happy coding πŸ˜€!!

About

A Full Stack Url Shortener App using Rust's Axum framework + </>HTMX & Askama

License:GNU General Public License v3.0


Languages

Language:Rust 69.8%Language:HTML 27.6%Language:JavaScript 1.7%Language:CSS 0.9%