allow all cross origin
This commit is contained in:
2
app.py
2
app.py
@ -1,6 +1,7 @@
|
|||||||
import logging
|
import logging
|
||||||
|
|
||||||
from flask import Flask
|
from flask import Flask
|
||||||
|
from flask_cors import cross_origin
|
||||||
|
|
||||||
import config
|
import config
|
||||||
from dto.requests import GameStartRequestDTO
|
from dto.requests import GameStartRequestDTO
|
||||||
@ -12,6 +13,7 @@ jb_client = JuliusBaerApiClient()
|
|||||||
|
|
||||||
|
|
||||||
@app.route('/new-game', methods=['POST'])
|
@app.route('/new-game', methods=['POST'])
|
||||||
|
@cross_origin() # allow all origins all methods
|
||||||
def new_game():
|
def new_game():
|
||||||
game_start_request = GameStartRequestDTO(player_name=config.API_TEAM)
|
game_start_request = GameStartRequestDTO(player_name=config.API_TEAM)
|
||||||
res = jb_client.start_game(game_start_request)
|
res = jb_client.start_game(game_start_request)
|
||||||
|
@ -6,7 +6,26 @@
|
|||||||
<title>PDF Viewer</title>
|
<title>PDF Viewer</title>
|
||||||
<script type="module" src="./js/main.js"></script>
|
<script type="module" src="./js/main.js"></script>
|
||||||
</head>
|
</head>
|
||||||
<body class="p-4">
|
<body x-data="gameManager" class="p-4">
|
||||||
|
|
||||||
|
<template x-if="isLoading">
|
||||||
|
<div class="alert alert-info" role="alert">
|
||||||
|
Loading game data...
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<template x-if="error">
|
||||||
|
<div class="alert alert-danger" role="alert" x-text="error"></div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<template x-if="gameData && !isLoading && !error">
|
||||||
|
<div>
|
||||||
|
<p>Game Ready!</p>
|
||||||
|
<p>Session ID: <code x-text="gameData.session_id"></code></p>
|
||||||
|
<p>First Client ID: <code x-text="gameData.client_id"></code></p>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
<div class="container py-4 px-3 mx-auto">
|
<div class="container py-4 px-3 mx-auto">
|
||||||
<h1>Hello, Bootstrap and Vite!</h1>
|
<h1>Hello, Bootstrap and Vite!</h1>
|
||||||
<button class="btn btn-primary">Primary button</button>
|
<button class="btn btn-primary">Primary button</button>
|
||||||
|
@ -7,6 +7,64 @@ import Alpine from 'alpinejs'
|
|||||||
|
|
||||||
window.Alpine = Alpine
|
window.Alpine = Alpine
|
||||||
|
|
||||||
|
// Define an Alpine component to manage the game state
|
||||||
|
Alpine.data('gameManager', () => ({
|
||||||
|
// --- Component State ---
|
||||||
|
isLoading: false,
|
||||||
|
error: null,
|
||||||
|
gameData: null,
|
||||||
|
|
||||||
|
init() {
|
||||||
|
console.log('Game manager initializing...');
|
||||||
|
this.startNewGame();
|
||||||
|
},
|
||||||
|
|
||||||
|
startNewGame() {
|
||||||
|
this.isLoading = true;
|
||||||
|
this.error = null;
|
||||||
|
this.gameData = null;
|
||||||
|
|
||||||
|
// Use the browser's fetch API
|
||||||
|
fetch('http://127.0.0.1:5000/new-game', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.then(response => {
|
||||||
|
// Check if the request was successful (status code 2xx)
|
||||||
|
if (!response.ok) {
|
||||||
|
// If not okay, throw an error to be caught by .catch()
|
||||||
|
// Try to get error details from response body if possible
|
||||||
|
return response.json().then(errData => {
|
||||||
|
throw new Error(errData.detail || `HTTP error! status: ${response.status}`);
|
||||||
|
}).catch(() => {
|
||||||
|
// Fallback if response is not JSON or other error
|
||||||
|
throw new Error(`HTTP error! status: ${response.status}`);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
// If okay, parse the JSON response body
|
||||||
|
return response.json();
|
||||||
|
})
|
||||||
|
.then(data => {
|
||||||
|
// Success! Store the received game data
|
||||||
|
console.log('New game data received:', data);
|
||||||
|
this.gameData = data; // e.g., { message, session_id, client_id, ... }
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
// Handle any errors during fetch or processing
|
||||||
|
console.error('Error starting new game:', error);
|
||||||
|
this.error = error.message || 'Failed to start game. Check console/backend.';
|
||||||
|
})
|
||||||
|
.finally(() => {
|
||||||
|
// This runs regardless of success or failure
|
||||||
|
this.isLoading = false; // Turn off loading indicator
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// submitDecision(decision) { ... }
|
||||||
|
}));
|
||||||
|
|
||||||
Alpine.data('pdfViewer', () => ({
|
Alpine.data('pdfViewer', () => ({
|
||||||
pdfUrl: null,
|
pdfUrl: null,
|
||||||
|
|
||||||
|
@ -39,3 +39,4 @@ Flask==3.1.0
|
|||||||
annotated-types==0.7.0
|
annotated-types==0.7.0
|
||||||
blinker==1.9.0
|
blinker==1.9.0
|
||||||
langchain-google-genai==2.1.2
|
langchain-google-genai==2.1.2
|
||||||
|
flask-cors==5.0.1
|
||||||
|
Reference in New Issue
Block a user