Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read cities with tokio::fs #50

Merged
merged 2 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/datasource.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use serde::{Deserialize, Serialize};
use std::fs;
use tokio::fs;

#[derive(Deserialize, Serialize, Clone)]
pub struct City {
Expand All @@ -15,7 +15,9 @@ pub struct AnonymizedCity<'a> {
pub name: &'a str,
}

pub fn get_cities() -> Vec<City> {
let cities_str = fs::read_to_string("./cities.json").expect("Unable to read cities.json file.");
pub async fn get_cities() -> Vec<City> {
let cities_str = fs::read_to_string("./cities.json")
.await
.expect("Unable to read cities.json file.");
serde_json::from_str(&cities_str).expect("cities.json does not have correct format.")
}
22 changes: 12 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use axum::response::IntoResponse;
use axum::routing::get;
use dotenvy::dotenv;
use socketioxide::SocketIoBuilder;
use std::fs;
use std::sync::{Arc, Mutex};
use tokio::fs;
use tower::ServiceBuilder;
use tower_http::cors::{Any, CorsLayer};
use tower_http::services::ServeDir;
Expand All @@ -25,23 +25,25 @@ struct AppState {
}

impl AppState {
fn get() -> AppState {
let mut landing = read_file("./frontend/landing.html");
async fn get() -> AppState {
let mut landing = read_file("./frontend/landing.html").await;
if std::env::var("HIDE_VIEW_SOURCE_BUTTON").unwrap_or("false".to_string()) != "false" {
landing = landing.replace("View Code", "");
}

AppState {
landing_page_content: landing,
game_page_content: read_file("./frontend/game.html"),
not_found_page_content: read_file("./frontend/404.html"),
robots_txt_content: read_file("./frontend/robots.txt"),
game_page_content: read_file("./frontend/game.html").await,
not_found_page_content: read_file("./frontend/404.html").await,
robots_txt_content: read_file("./frontend/robots.txt").await,
}
}
}

fn read_file(path: &str) -> String {
fs::read_to_string(path).expect("Should be able to read static page into memory")
async fn read_file(path: &str) -> String {
fs::read_to_string(path)
.await
.expect("Should be able to read static page into memory")
}

#[tokio::main]
Expand All @@ -52,7 +54,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let _ = dotenv();

info!("⏳ Loading cities!");
let cities = datasource::get_cities();
let cities = datasource::get_cities().await;
info!("✨ Loaded {} cities", cities.len());

let socketio_state = Arc::new(Mutex::new(game::GameState {
Expand All @@ -66,7 +68,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

io.ns("/", game::on_connect);

let state = AppState::get();
let state = AppState::get().await;
let app = axum::Router::new()
.route("/", get(landing_page))
.route("/game", get(game_page))
Expand Down
Loading