summaryrefslogtreecommitdiff
path: root/rust
diff options
context:
space:
mode:
authorniliara-edu <nil.jimeno@estudiant.fjaverianas.com>2024-09-27 14:04:23 +0200
committerniliara-edu <nil.jimeno@estudiant.fjaverianas.com>2024-09-27 14:04:23 +0200
commitdb58cb58b5d5612ec2aa347d0f8531b26ab2e7f3 (patch)
tree948e96bf2eabc7694194c07e9ac19acafa760b94 /rust
parent9d70bfed33df8ea4cd77f915454da265edccf401 (diff)
upload changes
Diffstat (limited to 'rust')
-rw-r--r--rust/.gitignore1
-rw-r--r--rust/Cargo.toml8
-rw-r--r--rust/N2
-rw-r--r--rust/env_example1
-rw-r--r--rust/scripts/create_db.sh10
-rw-r--r--rust/scripts/create_db.sql41
-rw-r--r--rust/scripts/create_user.sql1
-rw-r--r--rust/src/main.rs4
-rw-r--r--rust/src/routes.rs1
-rw-r--r--rust/src/routes/hello.rs11
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"
diff --git a/rust/N b/rust/N
new file mode 100644
index 0000000..a755b9a
--- /dev/null
+++ b/rust/N
@@ -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);
+}