diff options
Diffstat (limited to 'rust')
-rw-r--r-- | rust/.gitignore | 1 | ||||
-rw-r--r-- | rust/Cargo.toml | 8 | ||||
-rw-r--r-- | rust/N | 2 | ||||
-rw-r--r-- | rust/env_example | 1 | ||||
-rw-r--r-- | rust/scripts/create_db.sh | 10 | ||||
-rw-r--r-- | rust/scripts/create_db.sql | 41 | ||||
-rw-r--r-- | rust/scripts/create_user.sql | 1 | ||||
-rw-r--r-- | rust/src/main.rs | 4 | ||||
-rw-r--r-- | rust/src/routes.rs | 1 | ||||
-rw-r--r-- | rust/src/routes/hello.rs | 11 |
10 files changed, 77 insertions, 3 deletions
diff --git a/rust/.gitignore b/rust/.gitignore index 523b2b0..9adab75 100644 --- a/rust/.gitignore +++ b/rust/.gitignore @@ -2,3 +2,4 @@ lock/ target/ Cargo.lock **/*.rs.bk +.env diff --git a/rust/Cargo.toml b/rust/Cargo.toml index 6fdfa00..bc8db03 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -5,6 +5,12 @@ edition = "2021" [dependencies] actix-web = { version = "4.9.0"} -sqlx = { version = "0.8.2", features = ["mysql", "macros" , "runtime-tokio"] } +sqlx = { version = "0.8.2", features = ["mysql", "macros", "runtime-tokio"] } serde = { version = "1.0.210", features = ["derive"] } tokio = "1.40.0" + +[package.metadata.scripts] +db_create = "bash ./scripts/create_db.sh" +# db_start = "sudo docker start sqlx" +# db_start = "sudo docker stop sqlx" +# db_remove = "sudo docker rm sqlx" @@ -0,0 +1,2 @@ +cargo install cargo-run-script +cargo-run-script db-create diff --git a/rust/env_example b/rust/env_example new file mode 100644 index 0000000..607b7cc --- /dev/null +++ b/rust/env_example @@ -0,0 +1 @@ +DATABASE_URL="mysql://balalaika_user:password@127.0.0.1:3306/balalaika" diff --git a/rust/scripts/create_db.sh b/rust/scripts/create_db.sh new file mode 100644 index 0000000..cddcdd9 --- /dev/null +++ b/rust/scripts/create_db.sh @@ -0,0 +1,10 @@ +#!/bin/bash +echo "choose a password for the database: 'input;" +read input +echo $(pwd) +echo "creating database..." +sudo mariadb -u root <<EOF +$(cat scripts/create_user.sql) +EOF + +sudo mariadb -u root < ./scripts/create_db.sql diff --git a/rust/scripts/create_db.sql b/rust/scripts/create_db.sql new file mode 100644 index 0000000..f803714 --- /dev/null +++ b/rust/scripts/create_db.sql @@ -0,0 +1,41 @@ +CREATE DATABASE IF NOT EXISTS balalaika; +USE balalaika; + +DROP TABLE IF EXISTS song; +DROP TABLE IF EXISTS album; +DROP TABLE IF EXISTS artist; + +CREATE TABLE artist ( + id int NOT NULL AUTO_INCREMENT, + name varchar(255), + + PRIMARY KEY (id) +); + +CREATE TABLE album ( + id int NOT NULL AUTO_INCREMENT, + name varchar(255), + cover varchar(510), + artist_id int, + + PRIMARY KEY (id), + FOREIGN KEY (artist_id) REFERENCES artist(id) +); + +CREATE TABLE song ( + id int NOT NULL AUTO_INCREMENT, + name varchar(255), + lyrics TEXT, + + album_id int, + + PRIMARY KEY (id), + FOREIGN KEY (album_id) REFERENCES album(id) +); + +ALTER TABLE song CONVERT TO CHARACTER SET utf8; +ALTER TABLE album CONVERT TO CHARACTER SET utf8; +ALTER TABLE artist CONVERT TO CHARACTER SET utf8; + +GRANT ALL PRIVILEGES ON balalaika.* TO 'balalaika_user'@'%' WITH GRANT OPTION; +FLUSH PRIVILEGES; diff --git a/rust/scripts/create_user.sql b/rust/scripts/create_user.sql new file mode 100644 index 0000000..b2eaaaa --- /dev/null +++ b/rust/scripts/create_user.sql @@ -0,0 +1 @@ +CREATE USER IF NOT EXISTS 'balalaika_user'@'%' IDENTIFIED BY '$input'; diff --git a/rust/src/main.rs b/rust/src/main.rs index 2ccdcee..af45779 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -1,4 +1,5 @@ mod structs; +mod routes; use actix_web::{web, App, HttpServer}; use sqlx::mysql::{MySqlPool, MySqlPoolOptions}; @@ -10,8 +11,6 @@ struct AppState { #[actix_web::main] async fn main() -> std::io::Result<()> { - const DB_URL: &str = "mysql://root:@127.0.0.1:3306/balalaika"; - let pool: MySqlPool = MySqlPoolOptions::new() .max_connections(10) .connect(DB_URL) @@ -24,6 +23,7 @@ async fn main() -> std::io::Result<()> { App::new() .app_data(web::Data::new(app_state.clone())) .route("/", web::get().to(root)) + .service(routes::hello::hello_actix) }) .bind(("127.0.0.1", 8000))? .run() diff --git a/rust/src/routes.rs b/rust/src/routes.rs new file mode 100644 index 0000000..8cb6ff0 --- /dev/null +++ b/rust/src/routes.rs @@ -0,0 +1 @@ +mod hello; diff --git a/rust/src/routes/hello.rs b/rust/src/routes/hello.rs new file mode 100644 index 0000000..c356081 --- /dev/null +++ b/rust/src/routes/hello.rs @@ -0,0 +1,11 @@ +use crate::AppState; +use actix_web::{get, web, HttpResponse}; +use serde::Deserialize; + +#[get("/hello")] +pub async fn hello_actix(app_state: web::Data<AppState>) -> HttpResponse { + struct Song { + } + + return HttpResponse::Ok().json(databases); +} |