1
.gitignore
vendored
1
.gitignore
vendored
@@ -1 +1,2 @@
|
|||||||
/target
|
/target
|
||||||
|
.env
|
||||||
|
|||||||
1519
Cargo.lock
generated
1519
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -7,3 +7,9 @@ edition = "2021"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-web = "4"
|
actix-web = "4"
|
||||||
|
chrono = { version = "0.4.31", features = ["serde"] }
|
||||||
|
dotenvy = "0.15.7"
|
||||||
|
env_logger = "0.10.1"
|
||||||
|
serde = "1.0.193"
|
||||||
|
serde_json = "1.0.108"
|
||||||
|
sqlx = {version = "0.7.3", features=["postgres", "chrono", "uuid", "runtime-async-std"]}
|
||||||
|
|||||||
73
src/arrivals.rs
Normal file
73
src/arrivals.rs
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
use actix_web::{get, web, HttpResponse, Responder, post};
|
||||||
|
|
||||||
|
pub fn init_arrivals_scope() -> actix_web::Scope {
|
||||||
|
let scope = web::scope("/arrivals")
|
||||||
|
.service(show_arrivals)
|
||||||
|
.service(show_specific)
|
||||||
|
.service(insert_arrival);
|
||||||
|
scope
|
||||||
|
}
|
||||||
|
use chrono::NaiveTime;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
pub struct Arrival {
|
||||||
|
// a_id: i32,
|
||||||
|
time_of_day: NaiveTime,
|
||||||
|
week_day: i32,
|
||||||
|
tram_line: i32,
|
||||||
|
direction: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
use sqlx::{query, query_as, PgPool};
|
||||||
|
use web::Data;
|
||||||
|
#[get("all")]
|
||||||
|
async fn show_arrivals(db_pool: Data<PgPool>) -> impl Responder {
|
||||||
|
let arrivals = query_as!(
|
||||||
|
Arrival,
|
||||||
|
r#"SELECT time_of_day,week_day,tram_line,direction FROM arrivals"#
|
||||||
|
)
|
||||||
|
.fetch_all(db_pool.get_ref())
|
||||||
|
.await
|
||||||
|
.expect("Could not fetch arrivals");
|
||||||
|
dbg!(&arrivals);
|
||||||
|
HttpResponse::Ok()
|
||||||
|
.content_type("application/json")
|
||||||
|
.json(arrivals)
|
||||||
|
}
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct ArrivalFilter {
|
||||||
|
tram_line: i32,
|
||||||
|
week_day: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[get("specific")]
|
||||||
|
async fn show_specific(db_pool: Data<PgPool>, filter: web::Query<ArrivalFilter>) -> impl Responder {
|
||||||
|
//Le query
|
||||||
|
let arrivals = query_as!(
|
||||||
|
Arrival,
|
||||||
|
"SELECT time_of_day,week_day,tram_line,direction FROM arrivals WHERE tram_line = $1 AND week_day = $2",
|
||||||
|
filter.tram_line,
|
||||||
|
filter.week_day
|
||||||
|
)
|
||||||
|
.fetch_all(db_pool.get_ref())
|
||||||
|
.await
|
||||||
|
.expect("Could not fetch arrivals");
|
||||||
|
|
||||||
|
// Delet cuz we hardcoding
|
||||||
|
|
||||||
|
// dbg!(&arrivals);
|
||||||
|
HttpResponse::Ok()
|
||||||
|
.content_type("application/json")
|
||||||
|
.json(arrivals)
|
||||||
|
}
|
||||||
|
#[post("new")]
|
||||||
|
async fn insert_arrival(db_pool: Data<PgPool>, arrival: web::Query<Arrival>) -> impl Responder {
|
||||||
|
query!(
|
||||||
|
"INSERT INTO arrivals (time_of_day,week_day,tram_line,direction) VALUES ($1, $2, $3, $4)",
|
||||||
|
arrival.time_of_day,
|
||||||
|
arrival.week_day,
|
||||||
|
arrival.tram_line,
|
||||||
|
arrival.direction
|
||||||
|
).execute(db_pool.get_ref()).await.expect("I shat");
|
||||||
|
HttpResponse::Ok().body("inserted")
|
||||||
|
}
|
||||||
22
src/lib.rs
Normal file
22
src/lib.rs
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
use std::env;
|
||||||
|
|
||||||
|
pub fn init_address() -> (String, u16) {
|
||||||
|
let port: u16 = env::var("ACTIX_PORT")
|
||||||
|
.expect("SET ACTIX_PORT PLOX")
|
||||||
|
.parse()
|
||||||
|
.expect("Couldn't parse the ACTIX_PORT variable >:(");
|
||||||
|
let address = env::var("ACTIX_IP").expect("SET ACTIX_IP PLEASE");
|
||||||
|
(address, port)
|
||||||
|
}
|
||||||
|
use sqlx::Pool;
|
||||||
|
pub async fn init_dbpool() -> Pool<sqlx::Postgres> {
|
||||||
|
use sqlx::postgres::PgPoolOptions;
|
||||||
|
|
||||||
|
let database_url = env::var("DATABASE_URL").expect("Put a DB url in the .env file dumbass");
|
||||||
|
let pool = PgPoolOptions::new()
|
||||||
|
.max_connections(10)
|
||||||
|
.connect(database_url.as_str())
|
||||||
|
.await
|
||||||
|
.expect("No pool connection man :(");
|
||||||
|
pool
|
||||||
|
}
|
||||||
27
src/main.rs
27
src/main.rs
@@ -1,28 +1,25 @@
|
|||||||
use actix_web::{get, post, web, App, HttpResponse, HttpServer, Responder};
|
use actix_web::{post, web, App, HttpResponse, HttpServer, Responder};
|
||||||
|
use dotenvy::dotenv;
|
||||||
#[get("/")]
|
use witl_api::*;
|
||||||
async fn hello() -> impl Responder {
|
|
||||||
HttpResponse::Ok().body("Hello world!")
|
|
||||||
}
|
|
||||||
|
|
||||||
#[post("/echo")]
|
#[post("/echo")]
|
||||||
async fn echo(req_body: String) -> impl Responder {
|
async fn echo(req_body: String) -> impl Responder {
|
||||||
HttpResponse::Ok().body(req_body)
|
HttpResponse::Ok().body(req_body)
|
||||||
}
|
}
|
||||||
|
mod arrivals;
|
||||||
async fn manual_hello() -> impl Responder {
|
|
||||||
HttpResponse::Ok().body("Hey there!")
|
|
||||||
}
|
|
||||||
|
|
||||||
#[actix_web::main]
|
#[actix_web::main]
|
||||||
async fn main() -> std::io::Result<()> {
|
async fn main() -> std::io::Result<()> {
|
||||||
HttpServer::new(|| {
|
dotenv().ok();
|
||||||
|
std::env::set_var("RUST_LOG", "debug");
|
||||||
|
env_logger::init();
|
||||||
|
let db_pool = init_dbpool().await;
|
||||||
|
HttpServer::new(move || {
|
||||||
App::new()
|
App::new()
|
||||||
.service(hello)
|
|
||||||
.service(echo)
|
.service(echo)
|
||||||
.route("/hey", web::get().to(manual_hello))
|
.service(arrivals::init_arrivals_scope())
|
||||||
|
.app_data(web::Data::new(db_pool.clone()))
|
||||||
})
|
})
|
||||||
.bind(("localhost", 8080))?
|
.bind(init_address())?
|
||||||
.run()
|
.run()
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user