├── .env ├── .gitignore ├── Cargo.toml ├── LICENSE ├── README.md ├── create.sql ├── rustfmt.toml └── src └── main.rs /.env: -------------------------------------------------------------------------------- 1 | DATABASE_URL = 'postgres://axum_postgres:axum_postgres@127.0.0.1:5432/axum_postgres' 2 | SERVER_ADDRESS = '127.0.0.1:7878' -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /Cargo.lock 3 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "axum-postgres" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | #axum 10 | axum = "0.7.4" 11 | tokio = { version = "1.35.1", features = ["full"] } 12 | 13 | #postgres 14 | sqlx = {version = "0.7.3", features = ["runtime-tokio", "tls-native-tls", "postgres", "macros"]} 15 | 16 | #serde 17 | serde = { version = "1.0.195", features = ["derive"] } 18 | serde_json = {version = "1.0.111"} 19 | 20 | #env 21 | dotenvy = "0.15.7" -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Creative Commons Legal Code 2 | 3 | CC0 1.0 Universal 4 | 5 | CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE 6 | LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN 7 | ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS 8 | INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES 9 | REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS 10 | PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM 11 | THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED 12 | HEREUNDER. 13 | 14 | Statement of Purpose 15 | 16 | The laws of most jurisdictions throughout the world automatically confer 17 | exclusive Copyright and Related Rights (defined below) upon the creator 18 | and subsequent owner(s) (each and all, an "owner") of an original work of 19 | authorship and/or a database (each, a "Work"). 20 | 21 | Certain owners wish to permanently relinquish those rights to a Work for 22 | the purpose of contributing to a commons of creative, cultural and 23 | scientific works ("Commons") that the public can reliably and without fear 24 | of later claims of infringement build upon, modify, incorporate in other 25 | works, reuse and redistribute as freely as possible in any form whatsoever 26 | and for any purposes, including without limitation commercial purposes. 27 | These owners may contribute to the Commons to promote the ideal of a free 28 | culture and the further production of creative, cultural and scientific 29 | works, or to gain reputation or greater distribution for their Work in 30 | part through the use and efforts of others. 31 | 32 | For these and/or other purposes and motivations, and without any 33 | expectation of additional consideration or compensation, the person 34 | associating CC0 with a Work (the "Affirmer"), to the extent that he or she 35 | is an owner of Copyright and Related Rights in the Work, voluntarily 36 | elects to apply CC0 to the Work and publicly distribute the Work under its 37 | terms, with knowledge of his or her Copyright and Related Rights in the 38 | Work and the meaning and intended legal effect of CC0 on those rights. 39 | 40 | 1. Copyright and Related Rights. A Work made available under CC0 may be 41 | protected by copyright and related or neighboring rights ("Copyright and 42 | Related Rights"). Copyright and Related Rights include, but are not 43 | limited to, the following: 44 | 45 | i. the right to reproduce, adapt, distribute, perform, display, 46 | communicate, and translate a Work; 47 | ii. moral rights retained by the original author(s) and/or performer(s); 48 | iii. publicity and privacy rights pertaining to a person's image or 49 | likeness depicted in a Work; 50 | iv. rights protecting against unfair competition in regards to a Work, 51 | subject to the limitations in paragraph 4(a), below; 52 | v. rights protecting the extraction, dissemination, use and reuse of data 53 | in a Work; 54 | vi. database rights (such as those arising under Directive 96/9/EC of the 55 | European Parliament and of the Council of 11 March 1996 on the legal 56 | protection of databases, and under any national implementation 57 | thereof, including any amended or successor version of such 58 | directive); and 59 | vii. other similar, equivalent or corresponding rights throughout the 60 | world based on applicable law or treaty, and any national 61 | implementations thereof. 62 | 63 | 2. Waiver. To the greatest extent permitted by, but not in contravention 64 | of, applicable law, Affirmer hereby overtly, fully, permanently, 65 | irrevocably and unconditionally waives, abandons, and surrenders all of 66 | Affirmer's Copyright and Related Rights and associated claims and causes 67 | of action, whether now known or unknown (including existing as well as 68 | future claims and causes of action), in the Work (i) in all territories 69 | worldwide, (ii) for the maximum duration provided by applicable law or 70 | treaty (including future time extensions), (iii) in any current or future 71 | medium and for any number of copies, and (iv) for any purpose whatsoever, 72 | including without limitation commercial, advertising or promotional 73 | purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each 74 | member of the public at large and to the detriment of Affirmer's heirs and 75 | successors, fully intending that such Waiver shall not be subject to 76 | revocation, rescission, cancellation, termination, or any other legal or 77 | equitable action to disrupt the quiet enjoyment of the Work by the public 78 | as contemplated by Affirmer's express Statement of Purpose. 79 | 80 | 3. Public License Fallback. Should any part of the Waiver for any reason 81 | be judged legally invalid or ineffective under applicable law, then the 82 | Waiver shall be preserved to the maximum extent permitted taking into 83 | account Affirmer's express Statement of Purpose. In addition, to the 84 | extent the Waiver is so judged Affirmer hereby grants to each affected 85 | person a royalty-free, non transferable, non sublicensable, non exclusive, 86 | irrevocable and unconditional license to exercise Affirmer's Copyright and 87 | Related Rights in the Work (i) in all territories worldwide, (ii) for the 88 | maximum duration provided by applicable law or treaty (including future 89 | time extensions), (iii) in any current or future medium and for any number 90 | of copies, and (iv) for any purpose whatsoever, including without 91 | limitation commercial, advertising or promotional purposes (the 92 | "License"). The License shall be deemed effective as of the date CC0 was 93 | applied by Affirmer to the Work. Should any part of the License for any 94 | reason be judged legally invalid or ineffective under applicable law, such 95 | partial invalidity or ineffectiveness shall not invalidate the remainder 96 | of the License, and in such case Affirmer hereby affirms that he or she 97 | will not (i) exercise any of his or her remaining Copyright and Related 98 | Rights in the Work or (ii) assert any associated claims and causes of 99 | action with respect to the Work, in either case contrary to Affirmer's 100 | express Statement of Purpose. 101 | 102 | 4. Limitations and Disclaimers. 103 | 104 | a. No trademark or patent rights held by Affirmer are waived, abandoned, 105 | surrendered, licensed or otherwise affected by this document. 106 | b. Affirmer offers the Work as-is and makes no representations or 107 | warranties of any kind concerning the Work, express, implied, 108 | statutory or otherwise, including without limitation warranties of 109 | title, merchantability, fitness for a particular purpose, non 110 | infringement, or the absence of latent or other defects, accuracy, or 111 | the present or absence of errors, whether or not discoverable, all to 112 | the greatest extent permissible under applicable law. 113 | c. Affirmer disclaims responsibility for clearing rights of other persons 114 | that may apply to the Work or any use thereof, including without 115 | limitation any person's Copyright and Related Rights in the Work. 116 | Further, Affirmer disclaims responsibility for obtaining any necessary 117 | consents, permissions or other rights required for any use of the 118 | Work. 119 | d. Affirmer understands and acknowledges that Creative Commons is not a 120 | party to this document and has no duty or obligation with respect to 121 | this CC0 or use of the Work. 122 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Axum-Postgres CRUD App 2 | 3 | Hi, welcome to Axum-Postgres, a basic CRUD (Create, Read, Update, Delete) application built with Rust's Axum web framework and PostgreSQL. This project provides a simple API for managing tasks, demonstrating the integration of Axum with a PostgreSQL database. 4 | 5 | Here is the YouTube video associated with this repo: https://youtu.be/NJsTgmayHZY 6 | 7 | ## Table of Contents 8 | 9 | - [Prerequisites](#prerequisites) 10 | - [Setup](#setup) 11 | - [Database Configuration](#database-configuration) 12 | - [Run the Application](#run-the-application) 13 | - [API Endpoints](#api-endpoints) 14 | - [Project Structure](#project-structure) 15 | - [Contributing](#contributing) 16 | - [License](#license) 17 | 18 | ## Prerequisites 19 | 20 | Make sure you have the following installed on your system: 21 | 22 | - Rust (https://www.rust-lang.org/tools/install) 23 | - PostgreSQL (https://www.postgresql.org/download/) 24 | 25 | ## Setup 26 | 27 | 1. Clone the repository: 28 | 29 | ```bash 30 | git clone https://github.com/CudiLala/Rust-axum-postgres-CRUD-app.git 31 | cd Rust-axum-postgres-CRUD-app 32 | 33 | ``` 34 | 35 | 2. Build binaries 36 | 37 | ```bash 38 | cargo build 39 | ``` 40 | 41 | 3. Database Configuration 42 | 43 | Create a PostgreSQL database and user by executing the following SQL commands in your PostgreSQL shell or client: 44 | 45 | ```sql 46 | -- create user 47 | CREATE ROLE axum_postgres WITH LOGIN PASSWORD 'axum_postgres'; 48 | 49 | -- create database 50 | CREATE DATABASE axum_postgres WITH OWNER = 'axum_postgres'; 51 | 52 | -- in your axum_postgres database 53 | -- create task table 54 | CREATE TABLE tasks ( 55 | task_id SERIAL PRIMARY KEY, 56 | name VARCHAR NOT NULL, 57 | priority INT 58 | ); 59 | 60 | ``` 61 | 62 | Copy the contents of the `create.sql` file and execute it in your PostgreSQL database. 63 | 64 | Create a `.env` file in the project root and configure the `DATABASE_URL` and `SERVER_ADDRESS`: 65 | 66 | ```env 67 | DATABASE_URL=postgres://axum_postgres:axum_postgres@127.0.0.1:5432/axum_postgres 68 | SERVER_ADDRESS=127.0.0.1:7878 69 | 70 | ``` 71 | 72 | 4. Run the application 73 | Run the application with 74 | ```bash 75 | cargo run 76 | ``` 77 | 78 | ## API Endpoints 79 | 80 | ### Get all tasks 81 | ```http 82 | GET /tasks 83 | 84 | ``` 85 | Retrieves a list of all tasks. 86 | 87 | ### Create task 88 | ```http 89 | POST /tasks 90 | Content-Type: application/json 91 | 92 | { 93 | "name": "Task Name", 94 | "priority": 1 95 | } 96 | 97 | ``` 98 | Creates a new task. 99 | 100 | ### Update task 101 | ```http 102 | PATCH /tasks/{task_id} 103 | Content-Type: application/json 104 | 105 | { 106 | "name": "New Task Name", 107 | "priority": 2 108 | } 109 | 110 | ``` 111 | Updates an existing task. 112 | 113 | ### Delete task 114 | ```http 115 | DELETE /tasks/{task_id} 116 | 117 | ``` 118 | 119 | ## Contributing 120 | Feel free to contribute by opening issues or creating pull requests. Your feedback and contributions are highly appreciated. 121 | 122 | ## License 123 | This project is licensed under the CC0 License. 124 | -------------------------------------------------------------------------------- /create.sql: -------------------------------------------------------------------------------- 1 | -- create user 2 | CREATE ROLE axum_postgres WITH LOGIN PASSWORD 'axum_postgres'; 3 | 4 | -- create database 5 | CREATE DATABASE axum_postgres WITH OWNER = 'axum_postgres'; 6 | 7 | -- in your axum_postgres database 8 | -- create task table 9 | CREATE TABLE tasks ( 10 | task_id SERIAL PRIMARY KEY, 11 | name VARCHAR NOT NULL, 12 | priority INT 13 | ); -------------------------------------------------------------------------------- /rustfmt.toml: -------------------------------------------------------------------------------- 1 | tab_spaces = 2 -------------------------------------------------------------------------------- /src/main.rs: -------------------------------------------------------------------------------- 1 | use std::time::Duration; 2 | 3 | use axum::{ 4 | extract::{Path, State}, 5 | http::StatusCode, 6 | routing::{get, patch}, 7 | Json, Router, 8 | }; 9 | use serde::{Deserialize, Serialize}; 10 | use serde_json::json; 11 | use sqlx::{postgres::PgPoolOptions, PgPool}; 12 | use tokio::net::TcpListener; 13 | 14 | #[tokio::main] 15 | async fn main() { 16 | //expose environment variables from .env file 17 | dotenvy::dotenv().expect("Unable to access .env file"); 18 | 19 | //set variables from enviroment variables 20 | let server_address = std::env::var("SERVER_ADDRESS").unwrap_or("127.0.0.1:3000".to_owned()); 21 | let database_url = std::env::var("DATABASE_URL").expect("DATABASE_URL not found in env file"); 22 | 23 | //create our database pool 24 | let db_pool = PgPoolOptions::new() 25 | .max_connections(64) 26 | .acquire_timeout(Duration::from_secs(5)) 27 | .connect(&database_url) 28 | .await 29 | .expect("can't connect to database"); 30 | 31 | //create our tcp listener 32 | let listener = TcpListener::bind(server_address) 33 | .await 34 | .expect("Could not create tcp listener"); 35 | 36 | println!("listening on {}", listener.local_addr().unwrap()); 37 | 38 | // compose the routes 39 | let app = Router::new() 40 | .route("/", get(|| async { "Hello world" })) 41 | .route("/tasks", get(get_tasks).post(create_task)) 42 | .route("/tasks/:task_id", patch(update_task).delete(delete_task)) 43 | .with_state(db_pool); 44 | 45 | //serve the application 46 | axum::serve(listener, app) 47 | .await 48 | .expect("Error serving application"); 49 | } 50 | 51 | async fn get_tasks( 52 | State(db_pool): State, 53 | ) -> Result<(StatusCode, String), (StatusCode, String)> { 54 | let rows = sqlx::query_as!(TaskRow, "SELECT * FROM tasks ORDER BY task_id") 55 | .fetch_all(&db_pool) 56 | .await 57 | .map_err(|e| { 58 | ( 59 | StatusCode::INTERNAL_SERVER_ERROR, 60 | json!({"success": false, "message": e.to_string()}).to_string(), 61 | ) 62 | })?; 63 | 64 | Ok(( 65 | StatusCode::OK, 66 | json!({"success": true, "data": rows}).to_string(), 67 | )) 68 | } 69 | 70 | async fn create_task( 71 | State(db_pool): State, 72 | Json(task): Json, 73 | ) -> Result<(StatusCode, String), (StatusCode, String)> { 74 | let row = sqlx::query_as!( 75 | CreateTaskRow, 76 | "INSERT INTO tasks (name, priority) VALUES ($1, $2) RETURNING task_id", 77 | task.name, 78 | task.priority 79 | ) 80 | .fetch_one(&db_pool) 81 | .await 82 | .map_err(|e| { 83 | ( 84 | StatusCode::INTERNAL_SERVER_ERROR, 85 | json!({"success": false, "message": e.to_string()}).to_string(), 86 | ) 87 | })?; 88 | 89 | Ok(( 90 | StatusCode::CREATED, 91 | json!({"success": true, "data": row}).to_string(), 92 | )) 93 | } 94 | 95 | async fn update_task( 96 | State(db_pool): State, 97 | Path(task_id): Path, 98 | Json(task): Json, 99 | ) -> Result<(StatusCode, String), (StatusCode, String)> { 100 | let mut query = "UPDATE tasks SET task_id = $1".to_owned(); 101 | 102 | let mut i = 2; 103 | 104 | if task.name.is_some() { 105 | query.push_str(&format!(", name = ${i}")); 106 | i = i + 1; 107 | }; 108 | 109 | if task.priority.is_some() { 110 | query.push_str(&format!(", priority = ${i}")); 111 | }; 112 | 113 | query.push_str(&format!(" WHERE task_id = $1")); 114 | 115 | let mut s = sqlx::query(&query).bind(task_id); 116 | 117 | if task.name.is_some() { 118 | s = s.bind(task.name); 119 | } 120 | 121 | if task.priority.is_some() { 122 | s = s.bind(task.priority); 123 | } 124 | 125 | s.execute(&db_pool).await.map_err(|e| { 126 | ( 127 | StatusCode::INTERNAL_SERVER_ERROR, 128 | json!({"success": false, "message": e.to_string()}).to_string(), 129 | ) 130 | })?; 131 | 132 | Ok((StatusCode::OK, json!({"success":true}).to_string())) 133 | } 134 | 135 | async fn delete_task( 136 | State(db_pool): State, 137 | Path(task_id): Path, 138 | ) -> Result<(StatusCode, String), (StatusCode, String)> { 139 | sqlx::query!("DELETE FROM tasks WHERE task_id = $1", task_id,) 140 | .execute(&db_pool) 141 | .await 142 | .map_err(|e| { 143 | ( 144 | StatusCode::INTERNAL_SERVER_ERROR, 145 | json!({"success": false, "message": e.to_string()}).to_string(), 146 | ) 147 | })?; 148 | 149 | Ok((StatusCode::OK, json!({"success":true}).to_string())) 150 | } 151 | 152 | #[derive(Serialize)] 153 | struct TaskRow { 154 | task_id: i32, 155 | name: String, 156 | priority: Option, 157 | } 158 | 159 | #[derive(Deserialize)] 160 | struct CreateTaskReq { 161 | name: String, 162 | priority: Option, 163 | } 164 | 165 | #[derive(Serialize)] 166 | struct CreateTaskRow { 167 | task_id: i32, 168 | } 169 | 170 | #[derive(Deserialize)] 171 | struct UpdateTaskReq { 172 | name: Option, 173 | priority: Option, 174 | } 175 | --------------------------------------------------------------------------------