I'm new to Rust and have created REST API in Rust using Warp. I created a dummy grocery list API by following the tutorial from LogRocket Blog which uses local storage. Now, I want to add SQL Server to the code instead of local storage, but not sure how.
Here is what I've done so far:
use warp::{http, Filter};
use parking_lot::RwLock;
use std::collections::HashMap;
use std::sync::Arc;
use serde::{Serialize, Deserialize};
use sqlx::mssql::{Mssql, MssqlPoolOptions};
use sqlx::Row; // Import Row trait for working with query results
// Other functions
#[tokio::main]
async fn main() {
// Initializing MS SQL Server database connection pool
let db_pool = MssqlPoolOptions::new()
.connect("my_connection_string")
.await
.unwrap();
let store = Store::new(db_pool);
let store_filter = warp::any().map(move || store.clone());
let add_items = warp::post()
.and(warp::path("v1"))
.and(warp::path("groceries"))
.and(warp::path::end())
.and(post_json())
.and(store_filter.clone())
.and_then(update_grocery_list);
let get_items = warp::get()
.and(warp::path("v1"))
.and(warp::path("groceries"))
.and(warp::path::end())
.and(store_filter.clone())
.and_then(get_grocery_list);
let delete_item = warp::delete()
.and(warp::path("v1"))
.and(warp::path("groceries"))
.and(warp::path::end())
.and(delete_json())
.and(store_filter.clone())
.and_then(delete_grocery_list_item);
let update_item = warp::put()
.and(warp::path("v1"))
.and(warp::path("groceries"))
.and(warp::path::end())
.and(post_json())
.and(store_filter.clone())
.and_then(update_grocery_list);
let routes = add_items.or(get_items).or(delete_item).or(update_item);
// Add the cors middleware
let cors = warp::cors()
.allow_origin("http://localhost:3000")
.allow_methods(vec!["GET", "POST", "PUT", "DELETE"]);
// Apply the cors middleware to the routes
let routes_with_cors = routes.with(cors);
warp::serve(routes_with_cors)
.run(([127, 0, 0, 1], 3030))
.await;
}
Here is my cargo.toml
[package]
name = "neat-api"
version = "0.1.0"
edition = "2021"
[dependencies]
warp = "0.3"
parking_lot = "0.11"
serde = { version = "1.0", features = ["derive"] }
sqlx = { version = "0.5", features = ["runtime-tokio-native-tls", "macros"] }
sqlx-core = { version = "0.5", features = ["mssql"] }
Multiple errors were thrown, and it says that the syn
crate cannot find specific modules, types, or macros being imported in the sqlx-macros
crate. I'm not sure what that means.
the complete error log can be found Here
Any help will be appreciated, such as suggesting online articles or YouTube videos about connecting Rust to SQL Server. Thanks.