From de745106711f6bf983ca1c7fef4ba841bbc500ac Mon Sep 17 00:00:00 2001 From: GuilhermeWerner <26710260+GuilhermeWerner@users.noreply.github.com> Date: Wed, 12 May 2021 12:56:11 -0300 Subject: [PATCH] Change to Actix Web --- Cargo.toml | 20 +++++++---- Rocket.toml | 26 -------------- Source/Backend.rs | 25 ++++++++++++++ Source/Controllers/Hello.rs | 6 ++++ Source/Controllers/mod.rs | 1 + Source/Main.rs | 8 +++++ Source/Server.rs | 67 ------------------------------------- rust-toolchain | 1 - 8 files changed, 54 insertions(+), 100 deletions(-) delete mode 100644 Rocket.toml create mode 100644 Source/Backend.rs create mode 100644 Source/Controllers/Hello.rs create mode 100644 Source/Controllers/mod.rs create mode 100644 Source/Main.rs delete mode 100644 Source/Server.rs delete mode 100644 rust-toolchain diff --git a/Cargo.toml b/Cargo.toml index 32301eb..d41e086 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,14 +8,22 @@ license = "MIT" edition = "2018" publish = false +[lib] +name="Backend" +crate-type = ["rlib"] +path = "Source/Backend.rs" + [[bin]] -name="Server" -path = "Source/Server.rs" +name="Backend" +path = "Source/Main.rs" [dependencies] -rocket = "0.4.6" +Framework-Log = { path = "../Framework/Source/Log" } +actix-web = "3.3.2" +actix-service = "1.0.0" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" -rocket_contrib = "0.4.6" -mysql = "20.1.0" -dotenv = "0.15.0" +json = "0.12" + +[dev-dependencies] +actix-rt = "2.2.0" diff --git a/Rocket.toml b/Rocket.toml deleted file mode 100644 index c1b776f..0000000 --- a/Rocket.toml +++ /dev/null @@ -1,26 +0,0 @@ -[development] -address = "localhost" -port = 5000 -keep_alive = 5 -read_timeout = 5 -write_timeout = 5 -log = "normal" -limits = { forms = 32768 } - -[staging] -address = "localhost" -port = 5000 -keep_alive = 5 -read_timeout = 5 -write_timeout = 5 -log = "normal" -limits = { forms = 32768 } - -[production] -address = "localhost" -port = 5000 -keep_alive = 5 -read_timeout = 5 -write_timeout = 5 -log = "critical" -limits = { forms = 32768 } diff --git a/Source/Backend.rs b/Source/Backend.rs new file mode 100644 index 0000000..f05b962 --- /dev/null +++ b/Source/Backend.rs @@ -0,0 +1,25 @@ +#![allow(non_snake_case)] + +mod Controllers; + +use actix_web::{middleware, App, HttpServer}; +use std::env; +use std::io::Result; + +use Controllers::Hello; + +pub async fn Main() -> Result<()> { + env::set_var("FRAMEWORK_LOG_LEVEL", "debug"); + + Framework_Log::Init(); + + HttpServer::new(|| { + App::new() + .wrap(middleware::Logger::default()) + .wrap(middleware::Compress::default()) + .service(Hello::Index) + }) + .bind("localhost:5000")? + .run() + .await +} diff --git a/Source/Controllers/Hello.rs b/Source/Controllers/Hello.rs new file mode 100644 index 0000000..fdfc4c3 --- /dev/null +++ b/Source/Controllers/Hello.rs @@ -0,0 +1,6 @@ +use actix_web::get; + +#[get("/")] +pub async fn Index() -> &'static str { + return "Hello World\n"; +} diff --git a/Source/Controllers/mod.rs b/Source/Controllers/mod.rs new file mode 100644 index 0000000..8b2374f --- /dev/null +++ b/Source/Controllers/mod.rs @@ -0,0 +1 @@ +pub mod Hello; diff --git a/Source/Main.rs b/Source/Main.rs new file mode 100644 index 0000000..f5f7612 --- /dev/null +++ b/Source/Main.rs @@ -0,0 +1,8 @@ +#![allow(non_snake_case)] + +use std::io::Result; + +#[actix_web::main] +async fn main() -> Result<()> { + return Backend::Main().await; +} diff --git a/Source/Server.rs b/Source/Server.rs deleted file mode 100644 index 6965744..0000000 --- a/Source/Server.rs +++ /dev/null @@ -1,67 +0,0 @@ -#![feature(proc_macro_hygiene, decl_macro)] - -use std::env; -use dotenv::dotenv; - -use rocket::{get, routes, catch, catchers}; -use rocket_contrib::json; -use rocket_contrib::json::{Json, JsonValue}; - -use mysql::*; -use mysql::prelude::*; - -use serde::{Serialize, Deserialize}; - -#[derive(Serialize, Deserialize, Debug)] -struct Email { - address: String, - user_id: String, - is_confirmed: bool, - is_primary: bool, -} - -#[get("/")] -fn hello() -> JsonValue { - return json!({ "TribuFu": "Hello World" }); -} - -#[get("/emails")] -fn emails() -> JsonValue { - dotenv().ok(); - - let MYSQL_HOST: String = env::var("MYSQL_HOST").expect("Missing MYSQL_HOST in Environment"); - let MYSQL_PORT: String = env::var("MYSQL_PORT").expect("Missing MYSQL_PORT in Environmment"); - let MYSQL_USERNAME: String = env::var("MYSQL_USERNAME").expect("Missing MYSQL_USERNAME in Environmment"); - let MYSQL_PASSWORD: String = env::var("MYSQL_PASSWORD").expect("Missing MYSQL_PASSWORD in Environmment"); - let MYSQL_DATABASE: String = env::var("MYSQL_DATABASE").expect("Missing MYSQL_DATABASE in Environmment"); - - let url = format!("mysql://{}:{}@{}:{}/{}", MYSQL_USERNAME, MYSQL_PASSWORD, MYSQL_HOST, MYSQL_PORT, MYSQL_DATABASE); - - let pool = Pool::new(url).unwrap(); - - let mut connection = pool.get_conn().unwrap(); - - // Let's select emails from database. Type inference should do the trick here. - let emails = connection.query_map( - "SELECT Address, UserId, IsConfirmed, IsPrimary from Email", - |(address, user_id, is_confirmed, is_primary)| { - Email { address, user_id, is_confirmed, is_primary } - }, - ).unwrap(); - - return json!(emails); -} - -#[catch(404)] -fn not_found() -> JsonValue { - return json!({ - "Error": "RESOURCE_NOT_FOUND" - }); -} - -fn main() { - rocket::ignite() - .mount("/", routes![hello, emails]) - .register(catchers![not_found]) - .launch(); -} diff --git a/rust-toolchain b/rust-toolchain deleted file mode 100644 index bf867e0..0000000 --- a/rust-toolchain +++ /dev/null @@ -1 +0,0 @@ -nightly