From 5179ab904bbb7a2b0676f54cc00892560a6b45ce Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 19 May 2025 18:21:30 -0400 Subject: [PATCH] update: Laravel API setup for DigitalOcean hosting and Frontend usage now --- .github/workflows/backend.yml | 72 +++++++------------ API/docker/production/Dockerfile | 48 +++++++++++++ API/docker/production/build.sh | 1 - API/docker/production/docker-compose.yml | 29 -------- API/docker/production/nginx/Dockerfile | 10 --- .../production/nginx/conf.d/default.conf | 34 --------- .../nginx/snippets/fastcgi-php.conf | 5 -- API/docker/production/php/Dockerfile | 51 ------------- Frontend/shared/.env | 2 +- .../src/hooks/AuthContext/AuthContext.tsx | 5 +- 10 files changed, 79 insertions(+), 178 deletions(-) create mode 100644 API/docker/production/Dockerfile delete mode 100755 API/docker/production/build.sh delete mode 100644 API/docker/production/docker-compose.yml delete mode 100644 API/docker/production/nginx/Dockerfile delete mode 100644 API/docker/production/nginx/conf.d/default.conf delete mode 100644 API/docker/production/nginx/snippets/fastcgi-php.conf delete mode 100644 API/docker/production/php/Dockerfile diff --git a/.github/workflows/backend.yml b/.github/workflows/backend.yml index 2c517ff..bc33965 100644 --- a/.github/workflows/backend.yml +++ b/.github/workflows/backend.yml @@ -1,71 +1,51 @@ name: Backend Deployment on: + # Allows manual deployment + workflow_dispatch: push: # Triggers only when code is pushed to the development branch # that modifies files in the API directory branches: - - development + - deployment # production + - development # development paths: - 'API/**' jobs: - build: + build-and-push: runs-on: ubuntu-latest steps: - name: Checkout code - uses: actions/checkout@v2 - - - name: Set up PHP - uses: shivammathur/setup-php@v2 - with: - php-version: '8.2' # Adjust PHP version as needed - - - name: Create .env file - run: | - cd API - echo "${{ secrets.LARAVEL_ENV }}" > .env - - - name: Install Composer dependencies - run: | - cd API - composer install --no-interaction --prefer-dist --optimize-autoloader + uses: actions/checkout@v3 - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v1 + uses: docker/setup-buildx-action@v2 - name: Login to Docker Hub - uses: docker/login-action@v1 + uses: docker/login-action@v2 with: username: ${{ secrets.DOCKER_HUB_USERNAME }} password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }} - - name: Build and push nginx image - run: | - cd API/docker/production - docker compose build nginx - docker tag ${{ secrets.DOCKER_HUB_USERNAME }}/mynginx:latest ${{ secrets.DOCKER_HUB_USERNAME }}/mynginx:latest - docker push ${{ secrets.DOCKER_HUB_USERNAME }}/mynginx:latest - - - name: Build and push app image - run: | - cd API/docker/production - docker compose build app - docker tag ${{ secrets.DOCKER_HUB_USERNAME }}/myapp:latest ${{ secrets.DOCKER_HUB_USERNAME }}/myapp:latest - docker push ${{ secrets.DOCKER_HUB_USERNAME }}/myapp:latest - - - name: SSH and Deploy - uses: appleboy/ssh-action@master + - name: Build and push Docker image + uses: docker/build-push-action@v4 with: - host: ${{ secrets.SSH_HOST }} - username: ${{ secrets.SSH_USER }} - key: ${{ secrets.SSH_PRIVATE_KEY }} - debug: true - script: | - cd API - php artisan migrate - cd ${{ secrets.DOCKER_COMPOSE_DIRECTORY }} - docker-compose pull - docker-compose up -d + context: . + push: true + # If the branch is deployment, tag the image with prod (otherwise, tag it with dev) + tags: | + ${{ secrets.IMAGE_NAME }}:${{ github.ref == 'refs/heads/deployment' && 'prod' || 'dev' }} + + trigger-do-deployment: + needs: build-and-push + runs-on: ubuntu-latest + steps: + - name: Trigger DigitalOcean App Platform redeploy + # We have two apps, one for prod and one for dev + run: | + APP_ID=${{ github.ref == 'refs/heads/deployment' && secrets.DO_APP_ID_PROD || secrets.DO_APP_ID_DEV }} + curl -X POST "https://api.digitalocean.com/v2/apps/$APP_ID/deployments" \ + -H "Authorization: Bearer ${{ secrets.DO_API_TOKEN }}" diff --git a/API/docker/production/Dockerfile b/API/docker/production/Dockerfile new file mode 100644 index 0000000..34354b5 --- /dev/null +++ b/API/docker/production/Dockerfile @@ -0,0 +1,48 @@ +FROM php:8.2-apache + +# Set working directory +WORKDIR /var/www/html + +# Install system dependencies +RUN apt-get update && apt-get install -y \ + libjpeg62-turbo-dev \ + libfreetype6-dev \ + locales \ + git \ + curl \ + libpng-dev \ + libonig-dev \ + libxml2-dev \ + zip \ + unzip \ + libzip-dev \ + --no-install-recommends \ + && apt-get clean && rm -rf /var/lib/apt/lists/* + +# Install PHP extensions +RUN docker-php-ext-install pdo pdo_mysql mbstring exif pcntl bcmath gd intl zip soap + +# Enable Apache Rewrite module (required by Laravel) +RUN a2enmod rewrite + +# Copy existing application directory contents +COPY . /var/www/html + +# Copy existing application directory permissions +RUN chown -R www-data:www-data /var/www/html \ + && chmod -R 755 /var/www/html + +# Set recommended PHP.ini settings +RUN cp /usr/local/etc/php/php.ini-production /usr/local/etc/php/php.ini + +# Set Laravel permissions +RUN chown -R www-data:www-data /var/www/html/ + +# Install Composer +COPY --from=composer:2.6 /usr/bin/composer /usr/bin/composer + +# Install composer dependencies +RUN composer install --no-interaction --prefer-dist --optimize-autoloader + +# Expose Apache port +EXPOSE 80 diff --git a/API/docker/production/build.sh b/API/docker/production/build.sh deleted file mode 100755 index 5957b94..0000000 --- a/API/docker/production/build.sh +++ /dev/null @@ -1 +0,0 @@ -sudo docker-compose down && sudo docker-compose build app && sudo docker-compose up -d \ No newline at end of file diff --git a/API/docker/production/docker-compose.yml b/API/docker/production/docker-compose.yml deleted file mode 100644 index 8ae9cd8..0000000 --- a/API/docker/production/docker-compose.yml +++ /dev/null @@ -1,29 +0,0 @@ -version: '3.8' -services: - nginx: - build: - context: ../../ - dockerfile: ./docker/production/nginx/Dockerfile - image: velocities/mynginx:latest # Use the remote image - volumes: - - /etc/letsencrypt:/etc/letsencrypt # Mount live certs into the container - - ./nginx/conf.d:/etc/nginx/conf.d # Assuming your nginx configs are in ./docker/nginx/conf.d - - ./nginx/snippets:/etc/nginx/snippets # If you have snippets to include - ports: - - "8080:80" # Map host port 8080 to container port 80 - - "8443:443" # Map host port 8443 to container port 443 - depends_on: - - app - app: - build: - context: ../../ - dockerfile: ./docker/production/php/Dockerfile - image: velocities/myapp:latest # Use the remote image - env_file: - - ../../.env - volumes: - - ../../database:/var/www/SpeedCart/API/database - - ../../storage:/var/www/SpeedCart/API/storage - - ../../bootstrap/cache:/var/www/SpeedCart/API/bootstrap/cache - expose: - - "9000" diff --git a/API/docker/production/nginx/Dockerfile b/API/docker/production/nginx/Dockerfile deleted file mode 100644 index c3f080b..0000000 --- a/API/docker/production/nginx/Dockerfile +++ /dev/null @@ -1,10 +0,0 @@ -# nginx Dockerfile - -FROM nginx:alpine - -WORKDIR /etc/nginx - -# No need to COPY SSL certificates or other configuration info - -# Expose ports -EXPOSE 80 443 diff --git a/API/docker/production/nginx/conf.d/default.conf b/API/docker/production/nginx/conf.d/default.conf deleted file mode 100644 index 2608b38..0000000 --- a/API/docker/production/nginx/conf.d/default.conf +++ /dev/null @@ -1,34 +0,0 @@ -server { - listen 80; - listen [::]:80; - server_name api.speedcartapp.com; - - # Redirect all HTTP requests to HTTPS - return 301 https://$host$request_uri; -} - -server { - listen 443 ssl; - listen [::]:443 ssl; - server_name api.speedcartapp.com; - - # SSL certificates - ssl_certificate /etc/letsencrypt/live/api.speedcartapp.com/fullchain.pem; - ssl_certificate_key /etc/letsencrypt/live/api.speedcartapp.com/privkey.pem; - - root /var/www/SpeedCart/API/public; - index index.php index.html index.htm; - - location / { - try_files $uri $uri/ /index.php?$query_string; - } - - location ~ \.php$ { - include snippets/fastcgi-php.conf; # Include fastcgi-php.conf - fastcgi_pass app:9000; # Define fastcgi_pass here - } - - location ~ /\.ht { - deny all; - } -} diff --git a/API/docker/production/nginx/snippets/fastcgi-php.conf b/API/docker/production/nginx/snippets/fastcgi-php.conf deleted file mode 100644 index 4cb2c3a..0000000 --- a/API/docker/production/nginx/snippets/fastcgi-php.conf +++ /dev/null @@ -1,5 +0,0 @@ -fastcgi_split_path_info ^(.+\.php)(/.+)$; -fastcgi_index index.php; -include fastcgi_params; -fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; -fastcgi_param PATH_INFO $fastcgi_path_info; diff --git a/API/docker/production/php/Dockerfile b/API/docker/production/php/Dockerfile deleted file mode 100644 index 96f929f..0000000 --- a/API/docker/production/php/Dockerfile +++ /dev/null @@ -1,51 +0,0 @@ -# Use the official PHP image as a base image -FROM php:8.2-fpm - -# Set working directory (all subsequent commands will be run in this directory) -WORKDIR /var/www/SpeedCart/API - -# Install system dependencies -RUN apt-get update && apt-get install -y \ - libjpeg62-turbo-dev \ - libfreetype6-dev \ - locales \ - jpegoptim optipng pngquant gifsicle \ - vim \ - git \ - curl \ - libpng-dev \ - libonig-dev \ - libxml2-dev \ - zip \ - unzip \ - libzip-dev \ - sqlite3 \ - libsqlite3-dev \ - --no-install-recommends - -# Clear cache (This cleans up the package cache to reduce the size of the Docker image) -RUN apt-get clean && rm -rf /var/lib/apt/lists/* - -# Install PHP extensions required by Laravel -RUN docker-php-ext-install pdo pdo_sqlite mbstring exif pcntl bcmath gd intl zip soap - -# Install Composer (copies the composer binary from the latest Composer image -# into your Docker image, making Composer available for dependency management) -COPY --from=composer:2.1.3 /usr/bin/composer /usr/bin/composer - -# Copy existing application directory contents from API directory -# on host to /var/www/SpeedCart/API in the container -# Note: Remember that the context is the API directory (see docker-compose.yml) -COPY . /var/www/SpeedCart/API - -# Copy existing application directory permissions -COPY --chown=www-data:www-data . /var/www/SpeedCart/API - -# Change current user to www-data (used by web servers to improve security) -USER www-data - -# Expose port 9000 (the default port for PHP-FPM to listen on) and start php-fpm server -# (Even if you are using Nginx as a reverse proxy, PHP-FPM will still need to be exposed -# internally within the Docker network, which is why we need EXPOSE 9000) -EXPOSE 9000 -CMD ["php-fpm"] diff --git a/Frontend/shared/.env b/Frontend/shared/.env index 62dcc2a..83bf5b5 100644 --- a/Frontend/shared/.env +++ b/Frontend/shared/.env @@ -1,3 +1,3 @@ API_DOMAIN=api.speedcartapp.com -API_PORT=8443 +API_PORT=80 TESTING_MODE=false \ No newline at end of file diff --git a/Frontend/shared/src/hooks/AuthContext/AuthContext.tsx b/Frontend/shared/src/hooks/AuthContext/AuthContext.tsx index 91c2ea8..4a07770 100644 --- a/Frontend/shared/src/hooks/AuthContext/AuthContext.tsx +++ b/Frontend/shared/src/hooks/AuthContext/AuthContext.tsx @@ -28,11 +28,14 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children const login = (token: string) => { const userInfo: GoogleToken = jwtDecode(JSON.parse(token).credential); + if (TESTING_MODE) { + console.log(`TESTING_MODE === true`); + } // Initialize CSRF protection for the application fetch(`${BASE_URL}/sanctum/csrf-cookie`, { method: 'GET', - credentials: 'include', // Important: include credentials to allow the cookie to be set + credentials: TESTING_MODE ? 'omit' : 'include', // Important: include credentials to allow the cookie to be set headers: { 'Content-Type': 'application/json', 'Accept': 'application/json',