Skip to content

Commit

Permalink
shuttle
Browse files Browse the repository at this point in the history
  • Loading branch information
wyatt-avilla committed Jun 22, 2024
1 parent bb2d240 commit 9ddd80c
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 50 deletions.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
workspace = { members = ["backend", "config", "frontend", "types"] }
[workspace]
members = ["backend", "config", "frontend", "types"]
default-members = ["backend"]

[package]
name = "feframe"
Expand Down
4 changes: 4 additions & 0 deletions Shuttle.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
assets = [
".env",
"frontend/dist/*"
]
3 changes: 3 additions & 0 deletions backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
63 changes: 32 additions & 31 deletions backend/src/main.rs
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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<impl FnOnce(&mut ServiceConfig) + Send + Clone + 'static> {
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())
}
1 change: 0 additions & 1 deletion config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 0 additions & 6 deletions config/build.rs

This file was deleted.

2 changes: 1 addition & 1 deletion config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/github/scroller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Commit> = response.json().await.unwrap();
Expand Down
7 changes: 3 additions & 4 deletions frontend/src/components/goodreads/scroller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Book> = response.json().await.unwrap();
books.set(fetched_books);
});
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/lastfm/scroller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Song> = response.json().await.unwrap();
Expand Down
7 changes: 3 additions & 4 deletions frontend/src/components/letterboxd/scroller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Movie> = response.json().await.unwrap();
movies.set(fetched_movies);
});
Expand Down

0 comments on commit 9ddd80c

Please sign in to comment.