From 87e73a3c5fa5281da2791247bce1a45bbb389838 Mon Sep 17 00:00:00 2001 From: "maxime.batista" Date: Mon, 8 Jan 2024 11:06:52 +0100 Subject: [PATCH] add manual deployment scripts --- .gitignore | 4 +++- ci/.drone.yml | 6 ++++-- ci/build_and_deploy_to.sh | 17 +++++++++++++++++ ci/build_react.msh | 15 ++++++--------- config.php | 4 ++++ front/views/Editor.tsx | 10 +--------- profiles/dev-config-profile.php | 4 ++++ profiles/prod-config-profile.php | 7 +++++++ public/.htaccess | 4 ++++ public/api/.htaccess | 4 ++++ public/api/index.php | 6 +++--- public/index.php | 3 +-- src/Api/API.php | 2 +- src/Api/Controller/APITacticController.php | 1 + src/App/App.php | 2 +- src/index-utils.php | 21 --------------------- 16 files changed, 61 insertions(+), 49 deletions(-) create mode 100755 ci/build_and_deploy_to.sh create mode 100644 public/.htaccess create mode 100644 public/api/.htaccess delete mode 100644 src/index-utils.php diff --git a/.gitignore b/.gitignore index 3934c5c..e5a863d 100644 --- a/.gitignore +++ b/.gitignore @@ -8,13 +8,15 @@ vendor .nfs* composer.lock *.phar -/dist +dist .guard +outputs # sqlite database files *.sqlite views-mappings.php +.env.PROD # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. diff --git a/ci/.drone.yml b/ci/.drone.yml index a282766..42b1c42 100644 --- a/ci/.drone.yml +++ b/ci/.drone.yml @@ -37,8 +37,9 @@ steps: - echo n | /tmp/moshell_setup.sh - echo "VITE_API_ENDPOINT=/IQBall/$DRONE_BRANCH/public/api" >> .env.PROD - echo "VITE_BASE=/IQBall/$DRONE_BRANCH/public" >> .env.PROD + - apt update && apt install jq -y - - - /root/.local/bin/moshell ci/build_react.msh + - BASE="/IQBall/$DRONE_BRANCH/public" OUTPUT=/outputs /root/.local/bin/moshell ci/build_react.msh - image: ubuntu:latest name: "prepare php" @@ -48,7 +49,8 @@ steps: commands: - mkdir -p /outputs/public # this sed command will replace the included `profile/dev-config-profile.php` to `profile/prod-config-file.php` in the config.php file. - - sed -iE 's/\\/\\*PROFILE_FILE\\*\\/\\s*".*"/"profiles\\/prod-config-profile.php"/' config.php + - sed -E -i 's/\\/\\*PROFILE_FILE\\*\\/\\s*".*"/"profiles\\/prod-config-profile.php"/' config.php + - sed -E -i "s/const BASE_PATH = .*;/const BASE_PATH = \"\\/IQBall\\/$DRONE_BRANCH\\/public\";/" profiles/prod-config-profile.php - rm profiles/dev-config-profile.php - mv src config.php sql profiles vendor /outputs/ diff --git a/ci/build_and_deploy_to.sh b/ci/build_and_deploy_to.sh new file mode 100755 index 0000000..b0d2924 --- /dev/null +++ b/ci/build_and_deploy_to.sh @@ -0,0 +1,17 @@ + +export OUTPUT=$1 +export BASE=$2 + +rm -rf $OUTPUT/* + +echo "VITE_API_ENDPOINT=$BASE/api" >> .env.PROD +echo "VITE_BASE=$BASE" >> .env.PROD + +ci/build_react.msh + +mkdir -p $OUTPUT/profiles/ + +sed -E 's/\/\*PROFILE_FILE\*\/\s*".*"/"profiles\/prod-config-profile.php"/' config.php > $OUTPUT/config.php +sed -E "s/const BASE_PATH = .*;/const BASE_PATH = \"$(sed s/\\//\\\\\\//g <<< "$BASE")\";/" profiles/prod-config-profile.php > $OUTPUT/profiles/prod-config-profile.php + +cp -r vendor sql src public $OUTPUT diff --git a/ci/build_react.msh b/ci/build_react.msh index 3d3a8f0..a687ac6 100755 --- a/ci/build_react.msh +++ b/ci/build_react.msh @@ -1,20 +1,17 @@ #!/usr/bin/env moshell -mkdir -p /outputs/public +val base = std::env("BASE").unwrap() +val outputs = std::env("OUTPUT").unwrap() -apt update && apt install jq -y +mkdir -p $outputs/public -val drone_branch = std::env("DRONE_BRANCH").unwrap() - -val base = "/IQBall/$drone_branch/public" npm run build -- --base=$base --mode PROD // Read generated mappings from build val result = $(jq -r 'to_entries|map(.key + " " +.value.file)|.[]' dist/manifest.json) val mappings = $result.split('\n') - -echo ' views-mappings.php +echo ' views-mappings.php while $mappings.len() > 0 { val mapping = $mappings.pop().unwrap(); @@ -28,5 +25,5 @@ echo "];" >> views-mappings.php chmod +r views-mappings.php -mv dist/* front/assets/ front/style/ public/* /outputs/public/ -mv views-mappings.php /outputs/ +cp -r dist/* front/assets/ front/style/ public/* $outputs/public/ +cp -r views-mappings.php $outputs/ diff --git a/config.php b/config.php index 0dd030a..a3871c6 100644 --- a/config.php +++ b/config.php @@ -18,6 +18,10 @@ function asset(string $assetURI): string { return _asset($assetURI); } +function get_base_path(): string { + return _get_base_path(); +} + global $_data_source_name; $data_source_name = $_data_source_name; const DATABASE_USER = _DATABASE_USER; diff --git a/front/views/Editor.tsx b/front/views/Editor.tsx index cbb2da5..7dfea4f 100644 --- a/front/views/Editor.tsx +++ b/front/views/Editor.tsx @@ -121,15 +121,7 @@ function EditorView({ const [content, setContent, saveState] = useContentState( initialContent, isInGuestMode ? SaveStates.Guest : SaveStates.Ok, - useMemo( - () => - debounceAsync( - (content) => - onContentChange(content).then((success) => - success ? SaveStates.Ok : SaveStates.Err, - ), - 250, - ), + useMemo(() => debounceAsync(onContentChange), [onContentChange], ), ) diff --git a/profiles/dev-config-profile.php b/profiles/dev-config-profile.php index e39f2f0..0c06513 100644 --- a/profiles/dev-config-profile.php +++ b/profiles/dev-config-profile.php @@ -30,3 +30,7 @@ function _init_database(PDO $pdo): void { $accounts->setIsAdmin($id, true); } } + +function _get_base_path(): string { + return ""; +} diff --git a/profiles/prod-config-profile.php b/profiles/prod-config-profile.php index 185541a..224f8de 100644 --- a/profiles/prod-config-profile.php +++ b/profiles/prod-config-profile.php @@ -4,6 +4,9 @@ // in an `ASSETS` array constant. require __DIR__ . "/../views-mappings.php"; +// THIS VALUE IS TO SET IN THE CI +const BASE_PATH = null; + const _SUPPORTS_FAST_REFRESH = false; $database_file = __DIR__ . "/../database.sqlite"; $_data_source_name = "sqlite:/$database_file"; @@ -23,3 +26,7 @@ function _asset(string $assetURI): string { function _init_database(PDO $pdo): void {} + +function _get_base_path(): string { + return BASE_PATH; +} diff --git a/public/.htaccess b/public/.htaccess new file mode 100644 index 0000000..36a836b --- /dev/null +++ b/public/.htaccess @@ -0,0 +1,4 @@ +RewriteEngine on +RewriteCond %{REQUEST_FILENAME} !-f +RewriteCond %{REQUEST_FILENAME} !-d +RewriteRule ^.*$ ./index.php [NC,L,QSA] \ No newline at end of file diff --git a/public/api/.htaccess b/public/api/.htaccess new file mode 100644 index 0000000..36a836b --- /dev/null +++ b/public/api/.htaccess @@ -0,0 +1,4 @@ +RewriteEngine on +RewriteCond %{REQUEST_FILENAME} !-f +RewriteCond %{REQUEST_FILENAME} !-d +RewriteRule ^.*$ ./index.php [NC,L,QSA] \ No newline at end of file diff --git a/public/api/index.php b/public/api/index.php index 226e8f1..db14194 100644 --- a/public/api/index.php +++ b/public/api/index.php @@ -3,7 +3,6 @@ require "../../config.php"; require "../../vendor/autoload.php"; require "../../sql/database.php"; -require "../../src/index-utils.php"; use IQBall\Api\API; use IQBall\Api\Controller\APIAccountsController; @@ -19,7 +18,8 @@ use IQBall\Core\Gateway\TacticInfoGateway; use IQBall\Core\Model\AuthModel; use IQBall\Core\Model\TacticModel; -$basePath = get_public_path(__DIR__); + +$basePath = get_base_path() . "/api"; function getTacticController(): APITacticController { return new APITacticController(new TacticModel(new TacticInfoGateway(new Connection(get_database())))); @@ -41,8 +41,8 @@ function getServerController(): APIServerController { } function getRoutes(): AltoRouter { - $router = new AltoRouter(); global $basePath; + $router = new AltoRouter(); $router->setBasePath($basePath); $router->map("POST", "/auth", Action::noAuth(fn() => getAuthController()->authorize())); diff --git a/public/index.php b/public/index.php index 82dd37f..5d93c4c 100644 --- a/public/index.php +++ b/public/index.php @@ -4,7 +4,6 @@ require "../vendor/autoload.php"; require "../config.php"; require "../sql/database.php"; require "../src/App/react-display.php"; -require "../src/index-utils.php"; use IQBall\App\App; use IQBall\App\Controller\AuthController; @@ -125,6 +124,6 @@ function runMatch($match, MutableSessionHandle $session): HttpResponse { } //this is a global variable -$basePath = get_public_path(__DIR__); +$basePath = get_base_path(); App::render(runMatch(getRoutes()->match(), PhpSessionHandle::init()), fn() => getTwig()); diff --git a/src/Api/API.php b/src/Api/API.php index cc61c8d..143a838 100644 --- a/src/Api/API.php +++ b/src/Api/API.php @@ -38,7 +38,7 @@ class API { */ public static function handleMatch($match, callable $tryGetAuthorization): HttpResponse { if (!$match) { - return new JsonHttpResponse([ValidationFail::notFound("not found")]); + return new JsonHttpResponse([ValidationFail::notFound("not found")], HttpCodes::NOT_FOUND); } $action = $match['target']; diff --git a/src/Api/Controller/APITacticController.php b/src/Api/Controller/APITacticController.php index 9f71212..3cfe02e 100644 --- a/src/Api/Controller/APITacticController.php +++ b/src/Api/Controller/APITacticController.php @@ -49,6 +49,7 @@ class APITacticController { /** * @param int $id + * @param Account $account * @return HttpResponse */ public function saveContent(int $id, Account $account): HttpResponse { diff --git a/src/App/App.php b/src/App/App.php index 5f208bc..1cfe6d7 100644 --- a/src/App/App.php +++ b/src/App/App.php @@ -84,7 +84,7 @@ class App { if ($account == null) { // put in the session the initial url the user wanted to get $session->setInitialTarget($_SERVER['REQUEST_URI']); - return HttpResponse::redirect($authRoute); + return HttpResponse::redirectAbsolute($authRoute); } if ($action->getAuthType() == Action::AUTH_ADMIN && !$account->getUser()->isAdmin()) { diff --git a/src/index-utils.php b/src/index-utils.php deleted file mode 100644 index eb600bc..0000000 --- a/src/index-utils.php +++ /dev/null @@ -1,21 +0,0 @@ -