diff --git a/Cargo.toml b/Cargo.toml index 12bf4a2..436b2dd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,4 +1,6 @@ -workspace = { members = ["backend", "config", "frontend", "types"] } +[workspace] +members = ["backend", "config", "frontend", "types"] +default-members = ["backend"] [package] name = "feframe" diff --git a/Shuttle.toml b/Shuttle.toml new file mode 100644 index 0000000..19c2370 --- /dev/null +++ b/Shuttle.toml @@ -0,0 +1,4 @@ +assets = [ + ".env", + "frontend/dist/*" +] diff --git a/backend/Cargo.toml b/backend/Cargo.toml index 1b58d40..4ca65bf 100644 --- a/backend/Cargo.toml +++ b/backend/Cargo.toml @@ -22,3 +22,6 @@ yew = "0.21.0" log = "0.4.21" env_logger = "0.11.3" chrono = "0.4.38" +shuttle-actix-web = "0.46.0" +shuttle-runtime = "0.46.0" +actix-cors = "0.7.0" diff --git a/backend/src/main.rs b/backend/src/main.rs index ab5a4c4..8735d56 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -1,12 +1,16 @@ -use actix_web::{web, App, HttpServer, Responder}; -use chrono::Local; +use actix_cors::Cors; +use actix_files::Files; +use actix_web::{ + get, http, + web::{self, ServiceConfig}, + Responder, +}; use config::{ENDPOINT, ENV}; -use env_logger::Builder; -use log::LevelFilter; -use std::io::Write; +use shuttle_actix_web::ShuttleActixWeb; mod fetching; +#[get("/github")] async fn github() -> impl Responder { web::Json( fetching::github::fetch_newest(ENV.username.github, 10) @@ -15,6 +19,7 @@ async fn github() -> impl Responder { ) } +#[get("/lastfm")] async fn lastfm() -> impl Responder { web::Json( fetching::lastfm::fetch_newest(ENV.username.lastfm, ENV.key.lastfm, 10) @@ -23,6 +28,7 @@ async fn lastfm() -> impl Responder { ) } +#[get("/goodreads")] async fn goodreads() -> impl Responder { web::Json( fetching::goodreads::fetch_newest(ENV.link.goodreads, 10) @@ -31,6 +37,7 @@ async fn goodreads() -> impl Responder { ) } +#[get("/letterboxd")] async fn letterboxd() -> impl Responder { web::Json( fetching::letterboxd::fetch_newest(ENV.username.letterboxd, 4) @@ -39,32 +46,26 @@ async fn letterboxd() -> impl Responder { ) } -#[actix_web::main] -async fn main() -> std::io::Result<()> { - Builder::new() - .format(|buf, record| { - writeln!( - buf, - "{} [{}] - {}", - Local::now().format("%Y-%m-%dT%H:%M:%S"), - record.level(), - record.args() - ) - }) - .filter(None, LevelFilter::Info) - .init(); +#[allow(clippy::unused_async)] +#[shuttle_runtime::main] +async fn main() -> ShuttleActixWeb { + let config = move |cfg: &mut ServiceConfig| { + let cors = Cors::default() + .allowed_origin(ENDPOINT.base) + .allowed_methods(vec!["GET", "POST"]) + .allowed_headers(vec![http::header::CONTENT_TYPE]) + .max_age(3600); - log::info!("Server opened on {}", ENDPOINT.base); + cfg.service( + web::scope("/api") + .wrap(cors) + .service(github) + .service(lastfm) + .service(goodreads) + .service(letterboxd), + ); + cfg.service(Files::new("/", "frontend/dist").index_file("index.html")); + }; - HttpServer::new(|| { - App::new() - .route(ENDPOINT.github, web::get().to(github)) - .route(ENDPOINT.lastfm, web::get().to(lastfm)) - .route(ENDPOINT.goodreads, web::get().to(goodreads)) - .route(ENDPOINT.letterboxd, web::get().to(letterboxd)) - .service(actix_files::Files::new("/", "../frontend/dist").index_file("index.html")) - }) - .bind(ENDPOINT.base)? - .run() - .await + Ok(config.into()) } diff --git a/config/Cargo.toml b/config/Cargo.toml index cd4f221..c655951 100644 --- a/config/Cargo.toml +++ b/config/Cargo.toml @@ -2,7 +2,6 @@ name = "config" version = "0.1.0" edition = "2021" -build = "build.rs" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/config/build.rs b/config/build.rs deleted file mode 100644 index 475cb4a..0000000 --- a/config/build.rs +++ /dev/null @@ -1,6 +0,0 @@ -use dotenv_codegen::dotenv; -use url::Url; - -fn main() { - let _ = Url::parse(dotenv!("GOODREADS_SHELF")).expect("Goodreads shelf link is invalid"); -} diff --git a/config/src/lib.rs b/config/src/lib.rs index ddd64f1..ad04d14 100644 --- a/config/src/lib.rs +++ b/config/src/lib.rs @@ -15,7 +15,7 @@ pub const ENV: Env = Env { }; pub const ENDPOINT: Endpoint = Endpoint { - base: dotenv!("URL_BASE"), + base: "https://feframe.shuttleapp.rs", github: "/api/github", lastfm: "/api/lastfm", letterboxd: "/api/letterboxd", diff --git a/frontend/src/components/github/scroller.rs b/frontend/src/components/github/scroller.rs index 7115d38..775d83b 100644 --- a/frontend/src/components/github/scroller.rs +++ b/frontend/src/components/github/scroller.rs @@ -14,7 +14,7 @@ pub fn Scroller() -> Html { use_effect_with((), move |()| { let commits = commits.clone(); wasm_bindgen_futures::spawn_local(async move { - let response = reqwest::get(format!("http://{}{}", ENDPOINT.base, ENDPOINT.github)) + let response = reqwest::get(format!("{}{}", ENDPOINT.base, ENDPOINT.github)) .await .unwrap(); let fetched_commits: Vec = response.json().await.unwrap(); diff --git a/frontend/src/components/goodreads/scroller.rs b/frontend/src/components/goodreads/scroller.rs index 49d8bbe..a5f0574 100644 --- a/frontend/src/components/goodreads/scroller.rs +++ b/frontend/src/components/goodreads/scroller.rs @@ -13,10 +13,9 @@ pub fn Scroller() -> Html { use_effect_with((), move |()| { let books = books.clone(); wasm_bindgen_futures::spawn_local(async move { - let response = - reqwest::get(format!("http://{}{}", ENDPOINT.base, ENDPOINT.goodreads)) - .await - .unwrap(); + let response = reqwest::get(format!("{}{}", ENDPOINT.base, ENDPOINT.goodreads)) + .await + .unwrap(); let fetched_books: Vec = response.json().await.unwrap(); books.set(fetched_books); }); diff --git a/frontend/src/components/lastfm/scroller.rs b/frontend/src/components/lastfm/scroller.rs index 76ad8fc..cb37c21 100644 --- a/frontend/src/components/lastfm/scroller.rs +++ b/frontend/src/components/lastfm/scroller.rs @@ -13,7 +13,7 @@ pub fn Scroller() -> Html { use_effect_with((), move |()| { let songs = songs.clone(); wasm_bindgen_futures::spawn_local(async move { - let response = reqwest::get(format!("http://{}{}", ENDPOINT.base, ENDPOINT.lastfm)) + let response = reqwest::get(format!("{}{}", ENDPOINT.base, ENDPOINT.lastfm)) .await .unwrap(); let fetched_songs: Vec = response.json().await.unwrap(); diff --git a/frontend/src/components/letterboxd/scroller.rs b/frontend/src/components/letterboxd/scroller.rs index 5c20c4d..a759c57 100644 --- a/frontend/src/components/letterboxd/scroller.rs +++ b/frontend/src/components/letterboxd/scroller.rs @@ -13,10 +13,9 @@ pub fn Scroller() -> Html { use_effect_with((), move |()| { let movies = movies.clone(); wasm_bindgen_futures::spawn_local(async move { - let response = - reqwest::get(format!("http://{}{}", ENDPOINT.base, ENDPOINT.letterboxd)) - .await - .unwrap(); + let response = reqwest::get(format!("{}{}", ENDPOINT.base, ENDPOINT.letterboxd)) + .await + .unwrap(); let fetched_movies: Vec = response.json().await.unwrap(); movies.set(fetched_movies); });