Giter VIP home page Giter VIP logo

axum-rust-rest-api-template's Introduction

Axum-Rust-Rest-Api-Template

This project is an open source Rest Api Template built with Rust's Axum framework.

Why this project?

I have been learning rust for some time now and I noticed a lack of open source rust Rest ApI template template that define directory structure the way I like and I'm used to ( SOA ). Most other projects have all the code placed in a single main.rs file.

Features

This project uses Axum framework and SQLx for DB access layer, as well as other wonderful packages. It includes three basic routes: register, login, and profile page.

Getting Started

  1. Clone the project
  2. Update .env file with your DB credentials
  3. Install sqlx-cli or run cargo sqlx database create to create your DB
  4. Run the migration file using cargo sqlx migrate run. This will run the migration file that exists in the migration folder in the root of the project.
  5. Build the project and dependencies using cargo build
  6. Run the project using cargo run -- up

For this project, I used MySQL has my DB driver. You can use any other DB you like, but you will need to update the .env file with the appropriate DB connection url, Also you will need to update src/config/database.rs so sqlx can use your new driver. For example, if you want to use postgres, you will need to update the src/config/database.rs file to look like this:

use crate::parameter;
use async_trait::async_trait;
use sqlx::{Error, Pool, Postgres};

pub struct Database {
    pool: Pool<Postgres>,
}

#[async_trait]
pub trait DatabaseTrait {
    async fn init() -> Result<Self, Error>
        where
            Self: Sized;
    fn get_pool(&self) -> &Pool<Postgres>;
}

#[async_trait]
impl DatabaseTrait for Database {
    async fn init() -> Result<Self, Error> {
        let database_url = parameter::get("DATABASE_URL");
        let pool = Postgres::connect(&database_url).await?;
        Ok(Self { pool })
    }

    fn get_pool(&self) -> &Pool<Postgres> {
        &self.pool
    }
}

You will also need to update the migration files in `migrations/*` to match.

Contributing

Contributions are always welcome!

axum-rust-rest-api-template's People

Contributors

thanipro avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.