|
1 | | -# Deploy Guide |
| 1 | +# Freemail Version 2 Deployment Guide |
| 2 | + |
| 3 | +This guide provides step-by-step instructions to deploy the Freemail Version 2 application to Cloudflare Workers. It covers everything from setting up your environment to rigorous verification. |
2 | 4 |
|
3 | 5 | ## Prerequisites |
4 | 6 |
|
5 | | -- Node.js & npm installed |
6 | | -- Cloudflare Wrangler CLI installed (`npm install -g wrangler`) |
7 | | -- A Cloudflare account |
| 7 | +Before starting, ensure you have the following installed: |
| 8 | + |
| 9 | +1. **Node.js & npm**: Download and install from [Node.js official website](https://nodejs.org/). (Version 18 or higher recommended). |
| 10 | +2. **Git**: Download and install from [Git official website](https://git-scm.com/). |
| 11 | +3. **Cloudflare Account**: Sign up at [Cloudflare](https://dash.cloudflare.com/sign-up) if you don't have one. |
| 12 | + |
| 13 | +--- |
| 14 | + |
| 15 | +## Automated Deployment (Recommended) |
| 16 | + |
| 17 | +We provide a script to automate the entire setup process. |
| 18 | + |
| 19 | +1. **Install & Login**: |
| 20 | + ```bash |
| 21 | + npm install -g wrangler |
| 22 | + wrangler login |
| 23 | + ``` |
| 24 | +2. **Run Setup**: |
| 25 | + ```bash |
| 26 | + cd "version 2" |
| 27 | + npm run deploy:setup |
| 28 | + ``` |
| 29 | + Follow the interactive prompts to set up your database, secrets, and deploy. |
| 30 | + |
| 31 | +--- |
| 32 | + |
| 33 | +## Manual Deployment |
| 34 | + |
| 35 | +If you prefer to set up everything manually, follow these steps. |
| 36 | + |
| 37 | +## Step 1: Install Wrangler CLI |
| 38 | + |
| 39 | +The Wrangler CLI is the command-line tool for building and managing Cloudflare Workers. |
| 40 | + |
| 41 | +1. Open your terminal (Command Prompt, PowerShell, or macOS Terminal). |
| 42 | +2. Install Wrangler globally: |
| 43 | + ```bash |
| 44 | + npm install -g wrangler |
| 45 | + ``` |
| 46 | +3. Verify the installation: |
| 47 | + ```bash |
| 48 | + wrangler --version |
| 49 | + ``` |
| 50 | + |
| 51 | +--- |
| 52 | + |
| 53 | +## Step 2: Login to Cloudflare |
8 | 54 |
|
9 | | -## Setup |
| 55 | +Connect your local environment to your Cloudflare account. |
10 | 56 |
|
11 | | -1. **Install dependencies**: |
| 57 | +1. Run the login command: |
| 58 | + ```bash |
| 59 | + wrangler login |
| 60 | + ``` |
| 61 | +2. A browser window will open asking you to authorize Wrangler. |
| 62 | +3. Click **"Allow"**. |
| 63 | +4. Close the browser tab once you see the success message. |
| 64 | +5. In your terminal, you should see: `Successfully logged in`. |
| 65 | + |
| 66 | +--- |
| 67 | + |
| 68 | +## Step 3: Project Setup |
| 69 | + |
| 70 | +Navigate to the project directory and install dependencies. |
| 71 | + |
| 72 | +1. Open your terminal in the `version 2` folder of your project: |
| 73 | + ```bash |
| 74 | + cd "path/to/freemail/version 2" |
| 75 | + ``` |
| 76 | +2. Install project dependencies: |
12 | 77 | ```bash |
13 | 78 | npm install |
14 | 79 | ``` |
15 | 80 |
|
16 | | -2. **Configure Environment**: |
17 | | - - Copy `.env.example` to `.env` (for local development using Vite's proxy, though `wrangler.toml` handles production vars). |
18 | | - - Update `wrangler.toml` with your D1 Database ID and Name. |
19 | | - - Set secrets in Cloudflare dashboard OR via wrangler: |
20 | | - ```bash |
21 | | - wrangler secret put ADMIN_PASSWORD |
22 | | - wrangler secret put JWT_TOKEN |
23 | | - wrangler secret put RESEND_API_KEY |
24 | | - # ... other variables |
25 | | - ``` |
26 | | -
|
27 | | -3. **Database Initialization**: |
28 | | - - If you haven't created the database yet: |
29 | | - ```bash |
30 | | - wrangler d1 create maill_free_db |
31 | | - ``` |
32 | | - - Initialize the schema: |
33 | | - ```bash |
34 | | - wrangler d1 execute maill_free_db --file=d1-init.sql |
35 | | - ``` |
36 | | - |
37 | | -## Development |
38 | | - |
39 | | -1. **Frontend & Backend**: |
40 | | - - Run `npm run dev` to start the Vite dev server. |
41 | | - - Note: The Vite dev server proxies API requests to `http://localhost:8787`. You will need to run the worker locally separately or adjust the proxy target. |
42 | | - - **Better Approach**: Run `wrangler dev` to serve both the worker and the static assets (after building). |
| 81 | +--- |
43 | 82 |
|
| 83 | +## Step 4: Create Cloudflare Resources |
| 84 | + |
| 85 | +You need two main resources: a **D1 Database** (for storing user/mailbox data) and an **R2 Bucket** (for storing full email contents). |
| 86 | + |
| 87 | +### 4.1 Create D1 Database |
| 88 | + |
| 89 | +1. Run the creation command: |
44 | 90 | ```bash |
45 | | - npm run build |
46 | | - wrangler dev |
| 91 | + wrangler d1 create maill_free_db |
47 | 92 | ``` |
| 93 | +2. **Copy the output!** You will see something like this: |
| 94 | + ```toml |
| 95 | + [[d1_databases]] |
| 96 | + binding = "DB" # i.e. available in your Worker on env.DB |
| 97 | + database_name = "maill_free_db" |
| 98 | + database_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" |
| 99 | + ``` |
| 100 | +3. Open `wrangler.toml` in your text editor. |
| 101 | +4. Find the `[[d1_databases]]` section. |
| 102 | +5. Update the `database_id` with the one you just copied. **Do not change the `binding` name (`TEMP_MAIL_DB`)**, as the code relies on it. |
| 103 | + |
| 104 | +### 4.2 Initialize Database Schema |
48 | 105 |
|
49 | | -## Deployment |
| 106 | +Apply the database structure (tables) to your new D1 database. |
50 | 107 |
|
51 | | -1. **Build Frontend**: |
| 108 | +1. Run the execute command: |
| 109 | + ```bash |
| 110 | + wrangler d1 execute maill_free_db --file=d1-init.sql |
| 111 | + ``` |
| 112 | +2. Type `y` if prompted to confirm (for remote databases). |
| 113 | + |
| 114 | +### 4.3 Create R2 Bucket |
| 115 | + |
| 116 | +1. Run the create command: |
| 117 | + ```bash |
| 118 | + wrangler r2 bucket create mail-eml |
| 119 | + ``` |
| 120 | +2. Confirm it matches the `bucket_name` in your `wrangler.toml` (default is `mail-eml`). |
| 121 | + |
| 122 | +--- |
| 123 | + |
| 124 | +## Step 5: Configure Secrets |
| 125 | + |
| 126 | +Set up secure environment variables for your application. |
| 127 | + |
| 128 | +1. **Strict Admin Password** (Required): |
| 129 | + ```bash |
| 130 | + wrangler secret put ADMIN_PASSWORD |
| 131 | + # Enter your desired rigorous password when prompted |
| 132 | + ``` |
| 133 | + |
| 134 | +2. **JWT Signing Token** (Required): |
| 135 | + ```bash |
| 136 | + wrangler secret put JWT_TOKEN |
| 137 | + # Enter a long, random string (e.g., generated via `openssl rand -hex 32`) |
| 138 | + ``` |
| 139 | + |
| 140 | +3. **Resend API Key** (Optional, for sending emails): |
| 141 | + ```bash |
| 142 | + wrangler secret put RESEND_API_KEY |
| 143 | + # Enter your key from https://resend.com |
| 144 | + ``` |
| 145 | + |
| 146 | +--- |
| 147 | + |
| 148 | +## Step 6: Build and Deploy |
| 149 | + |
| 150 | +Now, build the frontend and deploy the full stack to Cloudflare. |
| 151 | + |
| 152 | +1. **Build the Frontend**: |
| 153 | + This compiles your React code into static assets in the `dist/` folder. |
52 | 154 | ```bash |
53 | 155 | npm run build |
54 | 156 | ``` |
55 | 157 |
|
56 | 158 | 2. **Deploy to Cloudflare Workers**: |
| 159 | + This uploads your backend code and the static assets. |
57 | 160 | ```bash |
58 | 161 | wrangler deploy |
59 | 162 | ``` |
60 | 163 |
|
61 | | -## Verification |
| 164 | +3. **Success!** |
| 165 | + The terminal will output your Worker's URL, e.g., `https://freemail-v2.your-subdomain.workers.dev`. |
| 166 | +
|
| 167 | +--- |
| 168 | +
|
| 169 | +## Step 7: Post-Deployment Configuration |
| 170 | +
|
| 171 | +### 7.1 Setup Email Routing (Catch-All) |
| 172 | +
|
| 173 | +To receive emails, you must configure Cloudflare Email Routing. |
| 174 | +
|
| 175 | +1. Go to the [Cloudflare Dashboard](https://dash.cloudflare.com/). |
| 176 | +2. Select your domain. |
| 177 | +3. Navigate to **Email** > **Email Routing**. |
| 178 | +4. Click **"Get Started"** if not already enabled. |
| 179 | +5. Go to the **Routes** tab. |
| 180 | +6. Click **Create Custom Address**. |
| 181 | + - **Custom Address**: Enter `*` (Catch-all) to receive emails for any address (e.g., `anything@yourdomain.com`). |
| 182 | + - **Action**: `Send to a Worker`. |
| 183 | + - **Destination**: Select your newly deployed Worker (`freemail-v2`). |
| 184 | +7. Click **Save**. |
| 185 | +
|
| 186 | +### 7.2 Verify Deployment |
| 187 | +
|
| 188 | +1. Open your Worker URL in a browser. |
| 189 | +2. You should see the Freemail login page. |
| 190 | +3. Login with `admin` and the password you set in **Step 5**. |
| 191 | +4. Try creating a random mailbox to test database connectivity. |
| 192 | +5. Try sending an email to that mailbox (from an external account like Gmail) to test Email Routing. |
| 193 | +
|
| 194 | +--- |
| 195 | +
|
| 196 | +## Troubleshooting |
62 | 197 |
|
63 | | -- Visit your worker URL (e.g., `https://freemail-v2.your-subdomain.workers.dev`). |
64 | | -- Log in with the credentials you set. |
| 198 | +- **Frontend loads blank**: Open your browser's Developer Tools (F12) -> Console. Check for errors. |
| 199 | +- **Database errors**: Ensure you ran the `d1 execute` command (Step 4.2). |
| 200 | +- **"Uncaught (in promise)"**: Check your `wrangler secret list` to ensure all secrets are set. |
| 201 | +- **Email not received**: Verify your domain's MX records in Cloudflare DNS settings match what Email Routing requires. |
0 commit comments