Преглед на файлове

Merge branch 'main' of https://github.com/Snouzy/workout-cool

Taio_O преди 3 седмици
родител
ревизия
b92a734f7f
променени са 3 файла, в които са добавени 133 реда и са изтрити 7 реда
  1. 97 2
      docs/SELF-HOSTING.md
  2. 34 5
      public/sw.js
  3. 2 0
      src/components/pwa/ServiceWorkerRegistration.tsx

+ 97 - 2
docs/SELF-HOSTING.md

@@ -12,6 +12,7 @@ Deploy **Workout-Cool** on your own server using Docker. This guide provides two
   - [Option 1: Docker Compose (All-in-One)](#option-1-docker-compose-all-in-one)
   - [Option 2: Docker Only (External Database)](#option-2-docker-only-external-database)
 - [Management Commands](#️-management-commands)
+- [Mapping Your Domain & Enabling HTTPS](#-mapping-your-domain--enabling-https)
 - [Troubleshooting](#-troubleshooting)
 - [Resources & Support](#-resources--support)
 
@@ -61,8 +62,8 @@ nano .env
 
 ```bash
 # Application Configuration (Required for both options)
-NEXTAUTH_SECRET=your-secret-key-here
-NEXTAUTH_URL=http://your-server-ip:3000
+BETTER_AUTH_URL=http://your-server-ip:3000
+BETTER_AUTH_SECRET=your-secret-key-here
 
 # Optional: Seed sample data on first run
 SEED_SAMPLE_DATA=true
@@ -131,6 +132,99 @@ Visit `http://your-server-ip:3000`
 
 ---
 
+## 🌐 Mapping Your Domain & Enabling HTTPS
+
+Make your app accessible via your own domain and secure it with HTTPS. Here's how:
+
+### 1. Point Your Domain
+
+1. Log in to your domain registrar.
+2. Create an **A record** for `yourdomain.com` pointing to your server's IP.
+3. (Optional) Create a **CNAME** record for `www` pointing to `yourdomain.com`.
+
+### 2. Set Up HTTPS with a Reverse Proxy
+
+#### Option A: Caddy (Recommended)
+
+Caddy provides automatic HTTPS and a simpler config.
+
+1. **Install Caddy:**
+    ```bash
+    sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https
+    curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
+    echo "deb [signed-by=/usr/share/keyrings/caddy-stable-archive-keyring.gpg] https://dl.cloudsmith.io/public/caddy/stable/deb/debian all main" | sudo tee /etc/apt/sources.list.d/caddy-stable.list
+    sudo apt update
+    sudo apt install caddy
+    ```
+2. **Configure Caddy:**
+    ```bash
+    sudo nano /etc/caddy/Caddyfile
+    ```
+    Add:
+    ```
+    yourdomain.com {
+        reverse_proxy localhost:3000
+    }
+    ```
+3. **Reload Caddy:**
+    ```bash
+    sudo systemctl reload caddy
+    ```
+
+✅ Caddy will:
+- Automatically request and install an SSL certificate via Let’s Encrypt
+- Renew it automatically
+- Proxy requests to your app running on port `3000`
+
+#### Option B: Nginx + Let's Encrypt
+
+Use this if you're more familiar with Nginx.
+
+1. **Install Nginx & Certbot:**
+    ```bash
+    sudo apt update && sudo apt install nginx certbot python3-certbot-nginx
+    ```
+2. **Create Nginx config:**
+
+    ```bash
+    sudo nano /etc/nginx/sites-available/workout-cool
+    ```
+
+    Add:
+    ```
+    server {
+        listen 80;
+        server_name yourdomain.com www.yourdomain.com;
+
+        location / {
+            proxy_pass http://localhost:3000;
+            proxy_set_header Host $host;
+            proxy_set_header X-Real-IP $remote_addr;
+            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+            proxy_set_header X-Forwarded-Proto $scheme;
+        }
+    }
+    ```
+3.  **Enable and reload:**
+    ```bash
+    sudo ln -s /etc/nginx/sites-available/workout-cool /etc/nginx/sites-enabled/
+    sudo nginx -t && sudo systemctl reload nginx
+    ```
+4. **Get an HTTPS certificate:**
+    ```bash
+    sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
+    ```
+
+### 3. Update Environment
+
+Update your `.env` file with the new domain
+
+```env
+BETTER_AUTH_URL=https://yourdomain.com
+```
+
+---
+
 ## 🛠️ Management Commands
 
 ### Docker Compose Commands
@@ -220,6 +314,7 @@ If you encounter issues:
 - [Docker Compose Documentation](https://docs.docker.com/compose/)
 - [PostgreSQL Documentation](https://www.postgresql.org/docs/)
 - [Next.js Deployment Guide](https://nextjs.org/docs/deployment)
+- [Caddy Documentation](https://caddyserver.com/docs/install)
 
 ### Community & Support
 - **GitHub**: [Repository](https://github.com/Snouzy/workout-cool) | [Issues](https://github.com/Snouzy/workout-cool/issues)

+ 34 - 5
public/sw.js

@@ -1,4 +1,4 @@
-const CACHE_NAME = "workout-cool-v1";
+const CACHE_NAME = "1.2.3";
 const urlsToCache = [
   "/",
   "/manifest.json",
@@ -11,6 +11,7 @@ const urlsToCache = [
 
 // Install event - cache resources
 self.addEventListener("install", (event) => {
+  self.skipWaiting(); // 🔥 force install
   event.waitUntil(
     caches.open(CACHE_NAME).then((cache) => {
       return cache.addAll(urlsToCache);
@@ -18,12 +19,38 @@ self.addEventListener("install", (event) => {
   );
 });
 
-// Fetch event - serve from cache when offline
+// Fetch event - network first with cache fallback
 self.addEventListener("fetch", (event) => {
   event.respondWith(
-    caches.match(event.request).then((response) => {
-      return response || fetch(event.request);
-    }),
+    fetch(event.request)
+      .then((response) => {
+        // If we get a valid response, clone it and update the cache
+        if (response && response.status === 200) {
+          const responseClone = response.clone();
+          caches.open(CACHE_NAME).then((cache) => {
+            cache.put(event.request, responseClone);
+          });
+        }
+        return response;
+      })
+      .catch(() => {
+        // If network fails, try to serve from cache
+        return caches.match(event.request).then((cachedResponse) => {
+          if (cachedResponse) {
+            return cachedResponse;
+          }
+          // If no cache available, return a custom offline page or error
+          if (event.request.destination === "document") {
+            return new Response("Application hors ligne - Veuillez vérifier votre connexion internet", {
+              status: 503,
+              statusText: "Service Unavailable",
+              headers: new Headers({
+                "Content-Type": "text/html; charset=utf-8",
+              }),
+            });
+          }
+        });
+      }),
   );
 });
 
@@ -40,4 +67,6 @@ self.addEventListener("activate", (event) => {
       );
     }),
   );
+
+  self.clients.claim();
 });

+ 2 - 0
src/components/pwa/ServiceWorkerRegistration.tsx

@@ -9,6 +9,8 @@ export function ServiceWorkerRegistration() {
         .register("/sw.js")
         .then((registration) => {
           console.log("SW registered: ", registration);
+          // Check for updates
+          registration.update();
         })
         .catch((registrationError) => {
           console.log("SW registration failed: ", registrationError);