mirror of
https://github.com/guilhermewerner/rust-api
synced 2025-06-15 14:35:15 +00:00
Change to Actix Web
This commit is contained in:
20
Cargo.toml
20
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"
|
||||
|
26
Rocket.toml
26
Rocket.toml
@ -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
25
Source/Backend.rs
Normal 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
|
||||
}
|
6
Source/Controllers/Hello.rs
Normal file
6
Source/Controllers/Hello.rs
Normal file
@ -0,0 +1,6 @@
|
||||
use actix_web::get;
|
||||
|
||||
#[get("/")]
|
||||
pub async fn Index() -> &'static str {
|
||||
return "Hello World\n";
|
||||
}
|
1
Source/Controllers/mod.rs
Normal file
1
Source/Controllers/mod.rs
Normal file
@ -0,0 +1 @@
|
||||
pub mod Hello;
|
8
Source/Main.rs
Normal file
8
Source/Main.rs
Normal file
@ -0,0 +1,8 @@
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
use std::io::Result;
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> Result<()> {
|
||||
return Backend::Main().await;
|
||||
}
|
@ -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();
|
||||
}
|
@ -1 +0,0 @@
|
||||
nightly
|
Reference in New Issue
Block a user