Skip to content

Commit 9ddd80c

Browse files
committed
shuttle
1 parent bb2d240 commit 9ddd80c

File tree

11 files changed

+51
-50
lines changed

11 files changed

+51
-50
lines changed

Cargo.toml

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
workspace = { members = ["backend", "config", "frontend", "types"] }
1+
[workspace]
2+
members = ["backend", "config", "frontend", "types"]
3+
default-members = ["backend"]
24

35
[package]
46
name = "feframe"

Shuttle.toml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
assets = [
2+
".env",
3+
"frontend/dist/*"
4+
]

backend/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,6 @@ yew = "0.21.0"
2222
log = "0.4.21"
2323
env_logger = "0.11.3"
2424
chrono = "0.4.38"
25+
shuttle-actix-web = "0.46.0"
26+
shuttle-runtime = "0.46.0"
27+
actix-cors = "0.7.0"

backend/src/main.rs

+32-31
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-
use actix_web::{web, App, HttpServer, Responder};
2-
use chrono::Local;
1+
use actix_cors::Cors;
2+
use actix_files::Files;
3+
use actix_web::{
4+
get, http,
5+
web::{self, ServiceConfig},
6+
Responder,
7+
};
38
use config::{ENDPOINT, ENV};
4-
use env_logger::Builder;
5-
use log::LevelFilter;
6-
use std::io::Write;
9+
use shuttle_actix_web::ShuttleActixWeb;
710

811
mod fetching;
912

13+
#[get("/github")]
1014
async fn github() -> impl Responder {
1115
web::Json(
1216
fetching::github::fetch_newest(ENV.username.github, 10)
@@ -15,6 +19,7 @@ async fn github() -> impl Responder {
1519
)
1620
}
1721

22+
#[get("/lastfm")]
1823
async fn lastfm() -> impl Responder {
1924
web::Json(
2025
fetching::lastfm::fetch_newest(ENV.username.lastfm, ENV.key.lastfm, 10)
@@ -23,6 +28,7 @@ async fn lastfm() -> impl Responder {
2328
)
2429
}
2530

31+
#[get("/goodreads")]
2632
async fn goodreads() -> impl Responder {
2733
web::Json(
2834
fetching::goodreads::fetch_newest(ENV.link.goodreads, 10)
@@ -31,6 +37,7 @@ async fn goodreads() -> impl Responder {
3137
)
3238
}
3339

40+
#[get("/letterboxd")]
3441
async fn letterboxd() -> impl Responder {
3542
web::Json(
3643
fetching::letterboxd::fetch_newest(ENV.username.letterboxd, 4)
@@ -39,32 +46,26 @@ async fn letterboxd() -> impl Responder {
3946
)
4047
}
4148

42-
#[actix_web::main]
43-
async fn main() -> std::io::Result<()> {
44-
Builder::new()
45-
.format(|buf, record| {
46-
writeln!(
47-
buf,
48-
"{} [{}] - {}",
49-
Local::now().format("%Y-%m-%dT%H:%M:%S"),
50-
record.level(),
51-
record.args()
52-
)
53-
})
54-
.filter(None, LevelFilter::Info)
55-
.init();
49+
#[allow(clippy::unused_async)]
50+
#[shuttle_runtime::main]
51+
async fn main() -> ShuttleActixWeb<impl FnOnce(&mut ServiceConfig) + Send + Clone + 'static> {
52+
let config = move |cfg: &mut ServiceConfig| {
53+
let cors = Cors::default()
54+
.allowed_origin(ENDPOINT.base)
55+
.allowed_methods(vec!["GET", "POST"])
56+
.allowed_headers(vec![http::header::CONTENT_TYPE])
57+
.max_age(3600);
5658

57-
log::info!("Server opened on {}", ENDPOINT.base);
59+
cfg.service(
60+
web::scope("/api")
61+
.wrap(cors)
62+
.service(github)
63+
.service(lastfm)
64+
.service(goodreads)
65+
.service(letterboxd),
66+
);
67+
cfg.service(Files::new("/", "frontend/dist").index_file("index.html"));
68+
};
5869

59-
HttpServer::new(|| {
60-
App::new()
61-
.route(ENDPOINT.github, web::get().to(github))
62-
.route(ENDPOINT.lastfm, web::get().to(lastfm))
63-
.route(ENDPOINT.goodreads, web::get().to(goodreads))
64-
.route(ENDPOINT.letterboxd, web::get().to(letterboxd))
65-
.service(actix_files::Files::new("/", "../frontend/dist").index_file("index.html"))
66-
})
67-
.bind(ENDPOINT.base)?
68-
.run()
69-
.await
70+
Ok(config.into())
7071
}

config/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
name = "config"
33
version = "0.1.0"
44
edition = "2021"
5-
build = "build.rs"
65

76
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
87

config/build.rs

-6
This file was deleted.

config/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub const ENV: Env = Env {
1515
};
1616

1717
pub const ENDPOINT: Endpoint = Endpoint {
18-
base: dotenv!("URL_BASE"),
18+
base: "https://feframe.shuttleapp.rs",
1919
github: "/api/github",
2020
lastfm: "/api/lastfm",
2121
letterboxd: "/api/letterboxd",

frontend/src/components/github/scroller.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub fn Scroller() -> Html {
1414
use_effect_with((), move |()| {
1515
let commits = commits.clone();
1616
wasm_bindgen_futures::spawn_local(async move {
17-
let response = reqwest::get(format!("http://{}{}", ENDPOINT.base, ENDPOINT.github))
17+
let response = reqwest::get(format!("{}{}", ENDPOINT.base, ENDPOINT.github))
1818
.await
1919
.unwrap();
2020
let fetched_commits: Vec<Commit> = response.json().await.unwrap();

frontend/src/components/goodreads/scroller.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@ pub fn Scroller() -> Html {
1313
use_effect_with((), move |()| {
1414
let books = books.clone();
1515
wasm_bindgen_futures::spawn_local(async move {
16-
let response =
17-
reqwest::get(format!("http://{}{}", ENDPOINT.base, ENDPOINT.goodreads))
18-
.await
19-
.unwrap();
16+
let response = reqwest::get(format!("{}{}", ENDPOINT.base, ENDPOINT.goodreads))
17+
.await
18+
.unwrap();
2019
let fetched_books: Vec<Book> = response.json().await.unwrap();
2120
books.set(fetched_books);
2221
});

frontend/src/components/lastfm/scroller.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub fn Scroller() -> Html {
1313
use_effect_with((), move |()| {
1414
let songs = songs.clone();
1515
wasm_bindgen_futures::spawn_local(async move {
16-
let response = reqwest::get(format!("http://{}{}", ENDPOINT.base, ENDPOINT.lastfm))
16+
let response = reqwest::get(format!("{}{}", ENDPOINT.base, ENDPOINT.lastfm))
1717
.await
1818
.unwrap();
1919
let fetched_songs: Vec<Song> = response.json().await.unwrap();

frontend/src/components/letterboxd/scroller.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@ pub fn Scroller() -> Html {
1313
use_effect_with((), move |()| {
1414
let movies = movies.clone();
1515
wasm_bindgen_futures::spawn_local(async move {
16-
let response =
17-
reqwest::get(format!("http://{}{}", ENDPOINT.base, ENDPOINT.letterboxd))
18-
.await
19-
.unwrap();
16+
let response = reqwest::get(format!("{}{}", ENDPOINT.base, ENDPOINT.letterboxd))
17+
.await
18+
.unwrap();
2019
let fetched_movies: Vec<Movie> = response.json().await.unwrap();
2120
movies.set(fetched_movies);
2221
});

0 commit comments

Comments
 (0)