Skip to content

Commit 367987a

Browse files
committed
Merge remote-tracking branch 'origin/main' into web-app
2 parents 3f71f44 + aafb9f2 commit 367987a

File tree

4 files changed

+16
-14
lines changed

4 files changed

+16
-14
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ axum = { version = "0.7.5", features = [
5050
"http2",
5151
], default-features = false }
5252
axum-server = { version = "0.6.0", features = ["tls-rustls"] }
53-
tower-http = { version = "0.5.2", features = ["compression-full"] }
53+
tower-http = { version = "0.5.2", features = ["compression-full", "cors"] }
5454
juniper_graphql_ws = "0.4.0"
5555
pretty_env_logger = "0.5.0"
5656
log = { version = "0.4.21" }

src/cache/menu_cache.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,22 @@ use firestore::FirestoreDb;
99
use futures::{stream::FuturesUnordered, StreamExt};
1010
use log::info;
1111
use tokio::io::AsyncReadExt;
12-
1312
const CACHES_COLLECTION: &str = "caches";
14-
1513
#[derive(Debug)]
1614
pub struct MenuCache<'a> {
1715
cached_at: DateTime<Utc>,
1816
locations: Locations<'a>,
1917
}
20-
2118
#[derive(serde::Serialize, serde::Deserialize, Default)]
22-
struct InDbMenuCache {
19+
struct GCloudMenuCache {
2320
cached_at: DateTime<Utc>,
2421
data: Vec<u8>,
2522
}
2623

2724
pub static REFRESH_INTERVAL: chrono::Duration = chrono::Duration::minutes(15);
2825

2926
impl<'a> MenuCache<'a> {
30-
async fn from_async(cache: InDbMenuCache) -> Self {
27+
async fn from_async(cache: GCloudMenuCache) -> Self {
3128
if cache.data.is_empty() {
3229
return MenuCache {
3330
cached_at: cache.cached_at,
@@ -85,7 +82,7 @@ impl<'a> MenuCache<'a> {
8582

8683
async fn fetch_from_db() -> Result<Self, crate::error::Error> {
8784
let db = FirestoreDb::new("ucsc-menu").await?;
88-
let cache: InDbMenuCache = db
85+
let cache: GCloudMenuCache = db
8986
.fluent()
9087
.select()
9188
.by_id_in(CACHES_COLLECTION)
@@ -96,7 +93,7 @@ impl<'a> MenuCache<'a> {
9693
Ok(MenuCache::from_async(cache).await)
9794
}
9895

99-
async fn to_db_representation(&self) -> InDbMenuCache {
96+
async fn to_db_representation(&self) -> GCloudMenuCache {
10097
let json = serde_json::to_string(self.locations()).unwrap();
10198
let mut compressed = Vec::with_capacity(json.len() / 4);
10299
let mut compress =
@@ -105,14 +102,14 @@ impl<'a> MenuCache<'a> {
105102
.read_buf(&mut compressed)
106103
.await
107104
.expect("This should succeed");
108-
InDbMenuCache {
105+
GCloudMenuCache {
109106
cached_at: self.cached_at,
110107
data: compressed,
111108
}
112109
}
113110

114111
async fn save_to_db(&self) -> Result<(), firestore::errors::FirestoreError> {
115-
let cache: InDbMenuCache = self.to_db_representation().await;
112+
let cache: GCloudMenuCache = self.to_db_representation().await;
116113
let db = FirestoreDb::new("ucsc-menu").await?;
117114
db.fluent()
118115
.update()

src/main.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,20 @@ use std::{
2121

2222
use axum::{
2323
body::Body,
24+
http::Method,
2425
response::Response,
2526
routing::{get, on, MethodFilter},
2627
Extension, Router,
2728
};
2829

30+
use crate::{cache::Multithreaded, fetch::make_client};
2931
use juniper::{graphql_object, EmptyMutation, EmptySubscription, RootNode};
3032
use juniper_axum::{graphiql, graphql, playground, ws};
3133
use juniper_graphql_ws::ConnectionConfig;
3234
use parse::Locations;
3335
use tokio::{net::TcpListener, sync::OnceCell, time::sleep};
34-
use tower_http::compression::CompressionLayer;
35-
36-
use crate::{cache::Multithreaded, fetch::make_client};
36+
use tower_http::cors::CorsLayer;
37+
use tower_http::{compression::CompressionLayer, cors::Any};
3738

3839
#[derive(Clone, Copy, Debug)]
3940
pub struct Query;
@@ -95,6 +96,9 @@ async fn main() {
9596
.deflate(true)
9697
.gzip(true)
9798
.zstd(true);
99+
let cors_layer = CorsLayer::new()
100+
.allow_methods([Method::GET, Method::POST]) // intentionally excludes request-refresh/PUT
101+
.allow_origin(Any);
98102
pretty_env_logger::init();
99103

100104
let app = Router::new()
@@ -112,6 +116,7 @@ async fn main() {
112116
.route("/request-refresh", on(MethodFilter::PUT, refresh))
113117
.route("/graphiql", get(graphiql("/graphql", "/subscriptions")))
114118
.route("/playground", get(playground("/graphql", "/subscriptions")))
119+
.layer(cors_layer)
115120
.layer(Extension(Arc::new(schema)))
116121
.layer(comression_layer);
117122
tokio::spawn(async move {

src/parse/location_page/locations.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ mod tests {
180180
.parse()
181181
.expect("url should be valid");
182182
let mut location = Location::new(LocationMeta::from_url(url).unwrap());
183-
let v = vec![html];
183+
let v = [html];
184184
location.add_meals(v.iter()).unwrap();
185185
assert!(location.hydrated());
186186
let root = RootNode::new(

0 commit comments

Comments
 (0)