Add working API calls, fix warnings
All checks were successful
Build Crate / build (push) Successful in 7m16s
All checks were successful
Build Crate / build (push) Successful in 7m16s
This commit is contained in:
@@ -13,6 +13,7 @@ env_logger = "0.10.0"
|
|||||||
log = "0.4.20"
|
log = "0.4.20"
|
||||||
once_cell = "1.18.0"
|
once_cell = "1.18.0"
|
||||||
web-sys = "0.3.64"
|
web-sys = "0.3.64"
|
||||||
|
cfg-if = { version = "1.0.0", features = [] }
|
||||||
|
|
||||||
|
|
||||||
[target.'cfg(engine)'.dev-dependencies]
|
[target.'cfg(engine)'.dev-dependencies]
|
||||||
@@ -26,4 +27,4 @@ tower-http = { version = "0.3", features = [ "fs" ] }
|
|||||||
|
|
||||||
[target.'cfg(client)'.dependencies]
|
[target.'cfg(client)'.dependencies]
|
||||||
wasm-bindgen = "0.2"
|
wasm-bindgen = "0.2"
|
||||||
reqwest = "0.11"
|
reqwest = { version = "0.11", features = ["json"] }
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ pub struct LayoutProps<'a, G: Html> {
|
|||||||
#[component]
|
#[component]
|
||||||
pub fn Layout<'a, G: Html>(
|
pub fn Layout<'a, G: Html>(
|
||||||
cx: Scope<'a>,
|
cx: Scope<'a>,
|
||||||
LayoutProps { title, children }: LayoutProps<'a, G>,
|
LayoutProps { title: _, children }: LayoutProps<'a, G>,
|
||||||
) -> View<G> {
|
) -> View<G> {
|
||||||
let children = children.call(cx);
|
let children = children.call(cx);
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
pub mod pool_match;
|
pub mod pool_match;
|
||||||
|
|
||||||
|
#[cfg(engine)]
|
||||||
pub mod store;
|
pub mod store;
|
||||||
pub mod global_state;
|
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
use std::collections::HashMap;
|
|
||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
use std::sync::Mutex;
|
use std::sync::Mutex;
|
||||||
use serde::{Serialize, Deserialize};
|
use serde::{Serialize, Deserialize};
|
||||||
use crate::data::pool_match::{PoolMatchList, PoolMatch};
|
use crate::data::pool_match::PoolMatchList;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
@@ -14,7 +14,7 @@ pub struct Store {
|
|||||||
|
|
||||||
impl Store {
|
impl Store {
|
||||||
fn new() -> Store {
|
fn new() -> Store {
|
||||||
fs::create_dir_all("data");
|
fs::create_dir_all("data").unwrap();
|
||||||
match Path::new("data/store.json").exists() {
|
match Path::new("data/store.json").exists() {
|
||||||
false => {
|
false => {
|
||||||
Store {
|
Store {
|
||||||
@@ -27,6 +27,8 @@ impl Store {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// TODO -> Store data
|
||||||
|
#[allow(dead_code)]
|
||||||
pub fn write(&self) {
|
pub fn write(&self) {
|
||||||
let contents = serde_json::to_string(&self).unwrap();
|
let contents = serde_json::to_string(&self).unwrap();
|
||||||
fs::write("data/store.json", contents).unwrap();
|
fs::write("data/store.json", contents).unwrap();
|
||||||
|
|||||||
2
src/endpoints.rs
Normal file
2
src/endpoints.rs
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
|
||||||
|
pub const MATCH: &str = "/api/post-match";
|
||||||
20
src/handler/mod.rs
Normal file
20
src/handler/mod.rs
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
use axum::{
|
||||||
|
extract::Json,
|
||||||
|
};
|
||||||
|
use crate::data::pool_match::{PoolMatch, PoolMatchList};
|
||||||
|
use std::thread;
|
||||||
|
use crate::data::store::DATA;
|
||||||
|
|
||||||
|
pub async fn post_match(Json(pool_match): Json<PoolMatch>) -> Json<PoolMatchList> {
|
||||||
|
// Update the store with the new match
|
||||||
|
let matches = thread::spawn(move || {
|
||||||
|
// Get the store
|
||||||
|
let mut store = DATA.lock().unwrap();
|
||||||
|
// Add the match
|
||||||
|
(*store).matches.pool_matches.push(pool_match);
|
||||||
|
// Return all pool matches
|
||||||
|
(*store).matches.clone()
|
||||||
|
}).join().unwrap();
|
||||||
|
|
||||||
|
Json(matches)
|
||||||
|
}
|
||||||
30
src/main.rs
30
src/main.rs
@@ -2,38 +2,28 @@ mod components;
|
|||||||
mod templates;
|
mod templates;
|
||||||
mod data;
|
mod data;
|
||||||
mod error_views;
|
mod error_views;
|
||||||
|
#[cfg(engine)]
|
||||||
|
mod handler;
|
||||||
|
mod endpoints;
|
||||||
|
|
||||||
use perseus::prelude::*;
|
use perseus::prelude::*;
|
||||||
use sycamore::prelude::view;
|
use sycamore::prelude::view;
|
||||||
|
|
||||||
#[cfg(engine)]
|
#[cfg(engine)]
|
||||||
use axum::{
|
use axum::routing::post;
|
||||||
body::Body,
|
|
||||||
extract::{Path, Query},
|
|
||||||
http::{Request, StatusCode},
|
|
||||||
response::{IntoResponse, Response},
|
|
||||||
routing::{get, get_service},
|
|
||||||
Router,
|
|
||||||
};
|
|
||||||
#[cfg(engine)]
|
|
||||||
use perseus::turbine::ApiResponse as PerseusApiResponse;
|
|
||||||
#[cfg(engine)]
|
#[cfg(engine)]
|
||||||
use perseus::{
|
use perseus::{
|
||||||
i18n::TranslationsManager,
|
i18n::TranslationsManager,
|
||||||
path::*,
|
|
||||||
server::ServerOptions,
|
server::ServerOptions,
|
||||||
stores::MutableStore,
|
stores::MutableStore,
|
||||||
turbine::{SubsequentLoadQueryParams, Turbine},
|
turbine::Turbine,
|
||||||
};
|
};
|
||||||
#[cfg(engine)]
|
#[cfg(engine)]
|
||||||
use tower_http::services::{ServeDir, ServeFile};
|
use crate::endpoints::MATCH;
|
||||||
|
#[cfg(engine)]
|
||||||
|
use crate::handler::post_match;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
async fn print_something() {
|
|
||||||
println!("haha");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(engine)]
|
#[cfg(engine)]
|
||||||
pub async fn dflt_server<M: MutableStore + 'static, T: TranslationsManager + 'static>(
|
pub async fn dflt_server<M: MutableStore + 'static, T: TranslationsManager + 'static>(
|
||||||
turbine: &'static Turbine<M, T>,
|
turbine: &'static Turbine<M, T>,
|
||||||
@@ -46,7 +36,7 @@ pub async fn dflt_server<M: MutableStore + 'static, T: TranslationsManager + 'st
|
|||||||
.parse()
|
.parse()
|
||||||
.expect("Invalid address provided to bind to.");
|
.expect("Invalid address provided to bind to.");
|
||||||
let mut app = perseus_axum::get_router(turbine, opts).await;
|
let mut app = perseus_axum::get_router(turbine, opts).await;
|
||||||
app = app.route("/api/test", get(print_something));
|
app = app.route(MATCH, post(post_match));
|
||||||
|
|
||||||
axum::Server::bind(&addr)
|
axum::Server::bind(&addr)
|
||||||
.serve(app.into_make_service())
|
.serve(app.into_make_service())
|
||||||
@@ -59,7 +49,7 @@ pub fn main<G: Html>() -> PerseusApp<G> {
|
|||||||
env_logger::init();
|
env_logger::init();
|
||||||
|
|
||||||
PerseusApp::new()
|
PerseusApp::new()
|
||||||
.global_state_creator(crate::data::global_state::get_global_state_creator())
|
.global_state_creator(crate::templates::global_state::get_global_state_creator())
|
||||||
.template(crate::templates::index::get_template())
|
.template(crate::templates::index::get_template())
|
||||||
.template(crate::templates::add_game_form::get_template())
|
.template(crate::templates::add_game_form::get_template())
|
||||||
.template(crate::templates::one_v_one_board::get_template())
|
.template(crate::templates::one_v_one_board::get_template())
|
||||||
|
|||||||
@@ -1,47 +1,70 @@
|
|||||||
use std::ops::Deref;
|
|
||||||
use crate::components::layout::Layout;
|
use crate::components::layout::Layout;
|
||||||
use perseus::prelude::*;
|
use perseus::prelude::*;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use sycamore::prelude::*;
|
use sycamore::prelude::*;
|
||||||
use crate::data::global_state::AppStateRx;
|
use web_sys::{Event};
|
||||||
use web_sys::{window, Event};
|
|
||||||
use crate::data::pool_match::PoolMatch;
|
cfg_if::cfg_if! {
|
||||||
#[cfg(client)]
|
if #[cfg(client)] {
|
||||||
use perseus::utils::get_path_prefix_client;
|
use crate::data::pool_match::{PoolMatch, PoolMatchList};
|
||||||
use crate::templates::get_api_path;
|
use crate::templates::global_state::AppStateRx;
|
||||||
|
use crate::endpoints::MATCH;
|
||||||
|
use crate::templates::get_api_path;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Reactive page
|
// Reactive page
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Clone, ReactiveState)]
|
#[derive(Serialize, Deserialize, Clone, ReactiveState)]
|
||||||
#[rx(alias = "PageStateRx")]
|
#[rx(alias = "PageStateRx")]
|
||||||
struct PageState {}
|
struct PageState {
|
||||||
|
name: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
fn add_game_form_page<'a, G: Html>(cx: BoundedScope<'_, 'a>, state: &'a PageStateRx) -> View<G> {
|
fn add_game_form_page<'a, G: Html>(cx: BoundedScope<'_, 'a>, state: &'a PageStateRx) -> View<G> {
|
||||||
let global_state = Reactor::<G>::from_cx(cx).get_global_state::<AppStateRx>(cx);
|
|
||||||
let api_path = get_api_path("/api/test");
|
|
||||||
|
|
||||||
let handle_add_match = move |event: Event| {
|
let handle_add_match = move |_event: Event| {
|
||||||
#[cfg(client)]
|
#[cfg(client)]
|
||||||
{
|
{
|
||||||
let path = get_api_path("/api/test");
|
|
||||||
println!("{}", path);
|
|
||||||
spawn_local_scoped(cx, async move {
|
spawn_local_scoped(cx, async move {
|
||||||
reqwest::get(get_api_path("/api/test").as_str()).await.unwrap();
|
let new_match = PoolMatch {
|
||||||
|
players: vec![],
|
||||||
|
winner: state.name.get().as_ref().clone(),
|
||||||
|
};
|
||||||
|
|
||||||
|
let client = reqwest::Client::new();
|
||||||
|
let new_matches = client.post(get_api_path(MATCH).as_str())
|
||||||
|
.json(&new_match)
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
.unwrap()
|
||||||
|
.json::<PoolMatchList>()
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
let global_state = Reactor::<G>::from_cx(cx).get_global_state::<AppStateRx>(cx);
|
||||||
|
global_state.matches.set(new_matches);
|
||||||
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
view! { cx,
|
view! { cx,
|
||||||
Layout(title = "Add Game Results") {
|
Layout(title = "Add Game Results") {
|
||||||
// Anything we put in here will be rendered inside the `<main>` block of the layout
|
div (class = "flex flex-wrap") {
|
||||||
button(on:click=handle_add_match) {
|
input (bind:value = state.name,
|
||||||
"Add result"
|
class = "appearance-none block w-full bg-gray-200 text-gray-700 border \
|
||||||
|
border-red-500 rounded py-3 px-4 mb-3 leading-tight focus:outline-none \
|
||||||
|
focus:bg-white",)
|
||||||
}
|
}
|
||||||
p {
|
div (class = "flex flex-wrap") {
|
||||||
(api_path)
|
button(on:click = handle_add_match,
|
||||||
|
class = "flex-shrink-0 bg-teal-500 hover:bg-teal-700 border-teal-500 \
|
||||||
|
hover:border-teal-700 text-sm border-4 text-white py-1 px-2 rounded",
|
||||||
|
) {
|
||||||
|
"Add result"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -50,9 +73,9 @@ fn add_game_form_page<'a, G: Html>(cx: BoundedScope<'_, 'a>, state: &'a PageStat
|
|||||||
#[engine_only_fn]
|
#[engine_only_fn]
|
||||||
async fn get_request_state(
|
async fn get_request_state(
|
||||||
_info: StateGeneratorInfo<()>,
|
_info: StateGeneratorInfo<()>,
|
||||||
req: Request,
|
_req: Request,
|
||||||
) -> Result<PageState, BlamedError<std::convert::Infallible>> {
|
) -> Result<PageState, BlamedError<std::convert::Infallible>> {
|
||||||
Ok(PageState {})
|
Ok(PageState { name: "Ferris".to_string() })
|
||||||
}
|
}
|
||||||
|
|
||||||
#[engine_only_fn]
|
#[engine_only_fn]
|
||||||
|
|||||||
47
src/templates/global_state.rs
Normal file
47
src/templates/global_state.rs
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
use perseus::{prelude::*, state::GlobalStateCreator};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
cfg_if::cfg_if! {
|
||||||
|
if #[cfg(engine)] {
|
||||||
|
use std::thread;
|
||||||
|
use std::ops::Deref;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(engine)]
|
||||||
|
use crate::data::store::DATA;
|
||||||
|
use crate::data::pool_match::PoolMatchList;
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, ReactiveState, Clone)]
|
||||||
|
#[rx(alias = "AppStateRx")]
|
||||||
|
pub struct AppState {
|
||||||
|
pub matches: PoolMatchList,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_global_state_creator() -> GlobalStateCreator {
|
||||||
|
GlobalStateCreator::new()
|
||||||
|
.build_state_fn(get_build_state)
|
||||||
|
.request_state_fn(get_request_state)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[engine_only_fn]
|
||||||
|
fn get_state() -> AppState {
|
||||||
|
let matches = thread::spawn(move || {
|
||||||
|
DATA.lock().unwrap().deref().matches.clone()
|
||||||
|
}).join().unwrap();
|
||||||
|
|
||||||
|
AppState {
|
||||||
|
matches
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[engine_only_fn]
|
||||||
|
pub async fn get_build_state() -> AppState {
|
||||||
|
get_state()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[engine_only_fn]
|
||||||
|
pub async fn get_request_state(_req: Request) -> AppState {
|
||||||
|
get_state()
|
||||||
|
}
|
||||||
@@ -2,10 +2,12 @@ pub mod index;
|
|||||||
pub mod add_game_form;
|
pub mod add_game_form;
|
||||||
pub mod one_v_one_board;
|
pub mod one_v_one_board;
|
||||||
pub mod overall_board;
|
pub mod overall_board;
|
||||||
|
pub mod global_state;
|
||||||
|
|
||||||
#[cfg(client)]
|
#[cfg(client)]
|
||||||
use perseus::utils::get_path_prefix_client;
|
use perseus::utils::get_path_prefix_client;
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
pub fn get_api_path(path: &str) -> String {
|
pub fn get_api_path(path: &str) -> String {
|
||||||
#[cfg(engine)]
|
#[cfg(engine)]
|
||||||
{
|
{
|
||||||
@@ -13,9 +15,8 @@ pub fn get_api_path(path: &str) -> String {
|
|||||||
}
|
}
|
||||||
#[cfg(client)]
|
#[cfg(client)]
|
||||||
{
|
{
|
||||||
let path = web_sys::window().unwrap().location().pathname().unwrap();
|
let origin = web_sys::window().unwrap().origin();
|
||||||
// let base_path = get_path_prefix_client();
|
let base_path = get_path_prefix_client();
|
||||||
// format!("{}{}", base_path, path)
|
format!("{}{}{}", origin, base_path, path)
|
||||||
path.to_string()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ struct PageState {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn one_v_one_board_page<'a, G: Html>(cx: BoundedScope<'_, 'a>, state: &'a PageStateRx) -> View<G> {
|
fn one_v_one_board_page<'a, G: Html>(cx: BoundedScope<'_, 'a>, _state: &'a PageStateRx) -> View<G> {
|
||||||
view! { cx,
|
view! { cx,
|
||||||
Layout(title = "1v1 Leaderboard") {
|
Layout(title = "1v1 Leaderboard") {
|
||||||
// Anything we put in here will be rendered inside the `<main>` block of the layout
|
// Anything we put in here will be rendered inside the `<main>` block of the layout
|
||||||
@@ -23,7 +23,7 @@ fn one_v_one_board_page<'a, G: Html>(cx: BoundedScope<'_, 'a>, state: &'a PageSt
|
|||||||
#[engine_only_fn]
|
#[engine_only_fn]
|
||||||
async fn get_request_state(
|
async fn get_request_state(
|
||||||
_info: StateGeneratorInfo<()>,
|
_info: StateGeneratorInfo<()>,
|
||||||
req: Request,
|
_req: Request,
|
||||||
) -> Result<PageState, BlamedError<std::convert::Infallible>> {
|
) -> Result<PageState, BlamedError<std::convert::Infallible>> {
|
||||||
Ok(PageState {})
|
Ok(PageState {})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,16 +1,8 @@
|
|||||||
use crate::components::layout::Layout;
|
use crate::components::layout::Layout;
|
||||||
use perseus::prelude::*;
|
use perseus::prelude::*;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
#[cfg(engine)]
|
|
||||||
use crate::data::store::DATA;
|
|
||||||
#[cfg(engine)]
|
|
||||||
use std::thread;
|
|
||||||
use sycamore::prelude::*;
|
use sycamore::prelude::*;
|
||||||
use crate::data::global_state::AppStateRx;
|
use crate::templates::global_state::AppStateRx;
|
||||||
|
|
||||||
use crate::data::pool_match::{
|
|
||||||
PoolMatchList, PoolMatch
|
|
||||||
};
|
|
||||||
|
|
||||||
// Reactive page
|
// Reactive page
|
||||||
|
|
||||||
@@ -20,7 +12,7 @@ struct PageState {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn overall_board_page<'a, G: Html>(cx: BoundedScope<'_, 'a>, state: &'a PageStateRx) -> View<G> {
|
fn overall_board_page<'a, G: Html>(cx: BoundedScope<'_, 'a>, _state: &'a PageStateRx) -> View<G> {
|
||||||
let global_state = Reactor::<G>::from_cx(cx).get_global_state::<AppStateRx>(cx);
|
let global_state = Reactor::<G>::from_cx(cx).get_global_state::<AppStateRx>(cx);
|
||||||
|
|
||||||
view! { cx,
|
view! { cx,
|
||||||
@@ -28,13 +20,12 @@ fn overall_board_page<'a, G: Html>(cx: BoundedScope<'_, 'a>, state: &'a PageStat
|
|||||||
// Anything we put in here will be rendered inside the `<main>` block of the layout
|
// Anything we put in here will be rendered inside the `<main>` block of the layout
|
||||||
ul {
|
ul {
|
||||||
(View::new_fragment(
|
(View::new_fragment(
|
||||||
global_state.store.get()
|
global_state.matches.get()
|
||||||
.matches
|
|
||||||
.pool_matches
|
.pool_matches
|
||||||
.iter()
|
.iter()
|
||||||
.rev()
|
.rev()
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.map(|(index, item)| {
|
.map(|(_index, item)| {
|
||||||
let game = item.clone();
|
let game = item.clone();
|
||||||
view! { cx,
|
view! { cx,
|
||||||
li {
|
li {
|
||||||
@@ -52,7 +43,7 @@ fn overall_board_page<'a, G: Html>(cx: BoundedScope<'_, 'a>, state: &'a PageStat
|
|||||||
#[engine_only_fn]
|
#[engine_only_fn]
|
||||||
async fn get_request_state(
|
async fn get_request_state(
|
||||||
_info: StateGeneratorInfo<()>,
|
_info: StateGeneratorInfo<()>,
|
||||||
req: Request,
|
_req: Request,
|
||||||
) -> Result<PageState, BlamedError<std::convert::Infallible>> {
|
) -> Result<PageState, BlamedError<std::convert::Infallible>> {
|
||||||
Ok(PageState {})
|
Ok(PageState {})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user