This is a Blog API written in Rust using the Axum framework. This application is (will serve as) backend for my portfolio blog site.
- Clone the repository:
git clone https://github.com/zzedddd/blogrs.git
- Navigate to the project directory:
cd blogrs
- Install cargo-shuttle:
cargo install --version 0.36.0 cargo-shuttle
- Install dependencies and run locally:
cargo shuttle run
To run the project, use the command cargo shuttle run
.
The application will start a server at http://localhost:8000
.
Note: docker
must be installed for local run as shuttle uses it for postgres.
This project provides the following API routes:
Fetches all posts.
Example usage:
curl -X GET http://localhost:8000/api/post
Fetches the details of a specific post, identified by its slug.
Example usage:
curl -X GET http://localhost:8000/post/api/my-first-post
Creates a new post. This route is protected and requires authentication.
Example usage:
curl -X POST -H "Content-Type: application/json" -d '{"title":"My Post", "slug":"my-post", "excerpt":"This is my post", "content":"This is the content of my post"}' http://localhost:8000/api/post/create
Updates a specific post, identified by its slug. This route is protected and requires authentication.
Example usage:
curl -X PATCH -H "Content-Type: application/json" -d '{"title":"Updated Post", "slug":"updated-post", "excerpt":"This is my updated post", "content":"This is the updated content of my post"}' http://localhost:8000/api/post/update/my-post
Deletes a specific post, identified by its slug. This route is protected and requires authentication.
Example usage:
curl -X DELETE http://localhost:8000/api/post/delete/my-post
Registers a new user.
Example usage:
curl -X POST -H "Content-Type: application/json" -d '{"email": "admin@gmail.com", "password":"password"}' http://localhost:8000/api/auth/register
Authenticates a user and returns a JWT token.
Example usage:
curl -X POST -H "Content-Type: application/json" -d '{"username":"admin", "email": "admin@gmail.com", "password":"password"}' http://localhost:8000/api/auth/login
Fetches the details of the currently authenticated user. This route is protected and requires authentication.
Example usage:
curl http://localhost:8000/api/auth/current_user
Logs out the currently authenticated user. This route is protected and requires authentication.
Example usage:
curl -X POST http://localhost:8000/api/auth/logout
This project uses the following environment variables:
DATABASE_URL
: The URL of the database to connect to.RUST_LOG
: (Optional) The logging level for the application.JWT_SECRET
: The secret key used to sign JWT tokens.
If you want to contribute to this project, please create a fork, make your changes, and submit a pull request.
This project is licensed under the MIT license. See the LICENSE file for more details.