diff --git a/.env b/.env new file mode 100644 index 0000000..951db6b --- /dev/null +++ b/.env @@ -0,0 +1 @@ +VITE_API_ENDPOINT=/api \ No newline at end of file diff --git a/.gitignore b/.gitignore index 48a117a..05806f7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,7 @@ -.* +.vs +.idea +.code + vendor composer.lock diff --git a/ci/.drone.yml b/ci/.drone.yml index e4d5305..d6c474c 100644 --- a/ci/.drone.yml +++ b/ci/.drone.yml @@ -20,7 +20,8 @@ steps: - curl -L moshell.dev/setup.sh > /tmp/moshell_setup.sh - chmod +x /tmp/moshell_setup.sh - echo n | /tmp/moshell_setup.sh - + - echo "VITE_API_ENDPOINT=/IQBall/$DRONE_BRANCH/public/api" >> .env.PROD + - - /root/.local/bin/moshell ci/build_react.msh - image: composer:latest diff --git a/ci/build_react.msh b/ci/build_react.msh index d253914..203afa0 100755 --- a/ci/build_react.msh +++ b/ci/build_react.msh @@ -1,11 +1,10 @@ #!/usr/bin/env moshell -npm build react mkdir -p /outputs/public apt update && apt install jq -y npm install -npm run build -- --base=/IQBall/public +npm run build -- --base=/IQBall/public --mode PROD // Read generated mappings from build val result = $(jq -r 'to_entries|map(.key + " " +.value.file)|.[]' dist/manifest.json) diff --git a/front/Constants.ts b/front/Constants.ts new file mode 100644 index 0000000..aaaaa43 --- /dev/null +++ b/front/Constants.ts @@ -0,0 +1,4 @@ +/** + * This constant defines the API endpoint. + */ +export const API = import.meta.env.VITE_API_ENDPOINT; \ No newline at end of file diff --git a/front/views/Editor.tsx b/front/views/Editor.tsx index 6fa49e6..3cbbf9b 100644 --- a/front/views/Editor.tsx +++ b/front/views/Editor.tsx @@ -1,7 +1,7 @@ import React from "react"; import "../style/editor.css"; import TitleInput from "../components/TitleInput"; - +import {API} from "../Constants"; export default function Editor({id, name}: { id: number, name: string }) { return ( @@ -17,7 +17,7 @@ export default function Editor({id, name}: { id: number, name: string }) { function update_tactic_name(id: number, new_name: string) { //FIXME avoid absolute path as they would not work on staging server - fetch(`/api/tactic/${id}/edit/name`, { + fetch(`${API}/tactic/${id}/edit/name`, { method: "POST", body: JSON.stringify({ name: new_name diff --git a/profiles/prod-config-profile.php b/profiles/prod-config-profile.php index dfd4d02..e185dfc 100644 --- a/profiles/prod-config-profile.php +++ b/profiles/prod-config-profile.php @@ -2,7 +2,7 @@ // This file only exists on production servers, and defines the available assets mappings // in an `ASSETS` array constant. -require "../views-mappings.php"; +require __DIR__ . "/../views-mappings.php"; const _SUPPORTS_FAST_REFRESH = false; $database_file = __DIR__ . "/../database.sqlite"; diff --git a/public/api/index.php b/public/api/index.php index c0a8f8f..e9fc0c3 100644 --- a/public/api/index.php +++ b/public/api/index.php @@ -3,6 +3,7 @@ require "../../config.php"; require "../../vendor/autoload.php"; require "../../sql/database.php"; +require "../utils.php"; use App\Api\TacticEndpoint; use App\Connexion; @@ -11,7 +12,7 @@ use App\Gateway\TacticInfoGateway; $con = new Connexion(get_database()); $router = new AltoRouter(); -$router->setBasePath("/api"); +$router->setBasePath(get_public_path() . "/api"); $tacticEndpoint = new TacticEndpoint(new TacticInfoGateway($con)); $router->map("POST", "/tactic/[i:id]/edit/name", fn(int $id) => $tacticEndpoint->update_name($id)); diff --git a/public/index.php b/public/index.php index 91e15f6..48b0637 100644 --- a/public/index.php +++ b/public/index.php @@ -3,6 +3,7 @@ require "../vendor/autoload.php"; require "../config.php"; require "../sql/database.php"; +require "utils.php"; use \Twig\Loader\FilesystemLoader; use App\Connexion; @@ -12,25 +13,11 @@ use App\Controller\EditorController; use App\Gateway\FormResultGateway; use App\Gateway\TacticInfoGateway; -/** - * relative path of the index.php's directory from the server's document root. - */ -function get_base_path() { - // find the server path of the index.php file - $basePath = dirname(substr(__FILE__, strlen($_SERVER['DOCUMENT_ROOT']))); - - $c = $basePath[strlen($basePath) - 1]; - - if ($c == "/" || $c == "\\") { - $basePath = substr($basePath, 0, strlen($basePath) - 1); - } - return $basePath; -} $loader = new FilesystemLoader('../src/Views/'); $twig = new \Twig\Environment($loader); -$basePath = get_base_path(); +$basePath = get_public_path(); $con = new Connexion(get_database()); // routes initialization diff --git a/public/utils.php b/public/utils.php new file mode 100644 index 0000000..ca9aa14 --- /dev/null +++ b/public/utils.php @@ -0,0 +1,16 @@ +