Change to Actix Web

This commit is contained in:
GuilhermeWerner
2021-05-12 12:56:11 -03:00
parent 5a38931498
commit de74510671
8 changed files with 54 additions and 100 deletions

View File

@ -8,14 +8,22 @@ license = "MIT"
edition = "2018" edition = "2018"
publish = false publish = false
[lib]
name="Backend"
crate-type = ["rlib"]
path = "Source/Backend.rs"
[[bin]] [[bin]]
name="Server" name="Backend"
path = "Source/Server.rs" path = "Source/Main.rs"
[dependencies] [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 = { version = "1.0", features = ["derive"] }
serde_json = "1.0" serde_json = "1.0"
rocket_contrib = "0.4.6" json = "0.12"
mysql = "20.1.0"
dotenv = "0.15.0" [dev-dependencies]
actix-rt = "2.2.0"

View File

@ -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 }

25
Source/Backend.rs Normal file
View File

@ -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
}

View File

@ -0,0 +1,6 @@
use actix_web::get;
#[get("/")]
pub async fn Index() -> &'static str {
return "Hello World\n";
}

View File

@ -0,0 +1 @@
pub mod Hello;

8
Source/Main.rs Normal file
View File

@ -0,0 +1,8 @@
#![allow(non_snake_case)]
use std::io::Result;
#[actix_web::main]
async fn main() -> Result<()> {
return Backend::Main().await;
}

View File

@ -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();
}

View File

@ -1 +0,0 @@
nightly