File size: 9,272 Bytes
2fe573b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# πŸš‚ Railway Deployment Guide for TalimBot

## πŸ“‹ Overview
This guide will help you deploy your complete TalimBot application (Frontend + Backend) to Railway for free, making it accessible from anywhere.

## βœ… What We've Done (Preparation Complete!)

### 1. **Restructured the Project**
- βœ… Created `backend/static/` folder
- βœ… Moved all frontend files (HTML, CSS, JS, Icons) to `backend/static/`
- βœ… Updated `main.py` to serve static files
- βœ… Updated `data.js` to use relative API paths for Railway

### 2. **Updated FastAPI Configuration**
Your `backend/main.py` now:
- Serves static files from `backend/static/` folder
- Uses environment variable for OpenRouter API key
- Handles both frontend (HTML pages) and backend (API endpoints) from the same server
- Works seamlessly on Railway's deployment platform

### 3. **Project Structure**
```

talimbot/

β”œβ”€β”€ .env                     # LOCAL ONLY - Contains your API key (NEVER commit this!)

β”œβ”€β”€ .env.example             # Template for environment variables

β”œβ”€β”€ .gitignore               # Ensures .env is not committed

β”œβ”€β”€ Procfile                 # Tells Railway how to start the server

β”œβ”€β”€ runtime.txt              # Specifies Python version

β”œβ”€β”€ README.md                

β”œβ”€β”€ RAILWAY_DEPLOYMENT.md    

β”œβ”€β”€ backend/

β”‚   β”œβ”€β”€ main.py              # βœ… UPDATED - Serves static files + API endpoints

β”‚   β”œβ”€β”€ grouping_logic.py    

β”‚   β”œβ”€β”€ requirements.txt     

β”‚   β”œβ”€β”€ data/

β”‚   β”‚   └── students.json    

β”‚   └── static/              # βœ… NEW - All frontend files

β”‚       β”œβ”€β”€ index.html       

β”‚       β”œβ”€β”€ assets/          # CSS and JS files

β”‚       β”‚   β”œβ”€β”€ css/

β”‚       β”‚   β”‚   └── styles.css

β”‚       β”‚   └── js/

β”‚       β”‚       β”œβ”€β”€ data.js  # βœ… UPDATED - Uses relative API paths

β”‚       β”‚       └── grouping.js

β”‚       β”œβ”€β”€ pages/           # All HTML pages

β”‚       β”‚   β”œβ”€β”€ login.html

β”‚       β”‚   β”œβ”€β”€ student-dashboard.html

β”‚       β”‚   β”œβ”€β”€ teacher-dashboard.html

β”‚       β”‚   β”œβ”€β”€ ams-questionnaire.html

β”‚       β”‚   β”œβ”€β”€ cooperative-questionnaire.html

β”‚       β”‚   └── group-view.html

β”‚       └── Icons/           # Logo and icons

```

---

## πŸš€ Deployment Steps

### **Step 1: Verify Local Setup**

1. **Create `.env` file** (if you haven't already):
   ```bash

   # In the project root (talimbot/) folder

   echo OPENROUTER_API_KEY=sk-or-v1-your-actual-key-here > .env

   ```

2. **Test locally**:
   ```bash

   cd backend

   python main.py

   ```
   
3. **Open browser** to `http://localhost:8000`
   - You should see the index.html page
   - All pages should work (login, dashboards, questionnaires)
   - API calls should work (grouping, data saving)

---

### **Step 2: Commit Changes to GitHub**

⚠️ **IMPORTANT**: Make sure `.env` is in `.gitignore` (it already is!)

```bash

# From the talimbot/ directory

git add .

git status  # Verify .env is NOT listed (should only see modified files)



git commit -m "Restructure project for Railway deployment - serve frontend from backend"

git push origin main

```

---

### **Step 3: Deploy to Railway**

#### A. **Sign Up / Log In**
1. Go to [railway.app](https://railway.app)
2. Click **"Start a New Project"**
3. Sign in with your GitHub account

#### B. **Create New Project**
1. Click **"Deploy from GitHub repo"**
2. Select your `talimbot` repository
3. Railway will automatically detect it's a Python project

#### C. **Configure Environment Variables**
1. In the Railway dashboard, go to your project
2. Click on the **"Variables"** tab
3. Click **"+ New Variable"**
4. Add:
   - **Key**: `OPENROUTER_API_KEY`
   - **Value**: `sk-or-v1-your-actual-openrouter-api-key`
5. Click **"Add"**

#### D. **Verify Deployment Settings**
Railway auto-detects settings from your files:
- βœ… **Build Command**: None needed (Python dependencies auto-installed)
- βœ… **Start Command**: From `Procfile` β†’ `cd backend && uvicorn main:app --host 0.0.0.0 --port $PORT`
- βœ… **Python Version**: From `runtime.txt` β†’ `python-3.11.0`

#### E. **Deploy!**
1. Click **"Deploy"**
2. Wait 2-3 minutes for deployment
3. Railway will show deployment logs
4. When complete, you'll see: βœ… **"Deployment successful"**

#### F. **Get Your URL**
1. In Railway dashboard, click **"Settings"** tab
2. Scroll to **"Networking"** section
3. Click **"Generate Domain"**
4. Copy your URL (e.g., `https://talimbot-production-abc123.up.railway.app`)

---

### **Step 4: Test Your Deployed Application**

1. **Open your Railway URL** in a browser
2. **Test all features**:
   - βœ… Main page loads (`index.html`)
   - βœ… Login page works (`/pages/login.html`)
   - βœ… Student dashboard loads
   - βœ… Teacher dashboard loads
   - βœ… Questionnaires work (AMS, Cooperative)
   - βœ… Grouping functionality works
   - βœ… Data saves correctly

---

## πŸ”§ How It Works

### **Single Server Architecture**
Railway runs ONE server that handles BOTH:

1. **Frontend (Static Files)**:
   - `GET /` β†’ Serves `index.html`
   - `GET /pages/login.html` β†’ Serves login page
   - `GET /assets/css/styles.css` β†’ Serves CSS
   - `GET /assets/js/data.js` β†’ Serves JavaScript

2. **Backend (API Endpoints)**:
   - `POST /api/grouping` β†’ AI grouping logic
   - `GET /api/students` β†’ Get all students
   - `PUT /api/students/{id}` β†’ Update student
   - All other API routes in `main.py`

### **How Requests Are Routed**

```

User Browser β†’ Railway URL

              ↓

        FastAPI Server (main.py)

              ↓

    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”

    ↓                   ↓

  /api/*            Everything else

(API Endpoints)     (Static Files)

    ↓                   ↓

grouping_logic.py   backend/static/

OpenRouter API        (HTML/CSS/JS)

```

### **Environment Variable Flow**

```

Local Development:

.env file β†’ load_dotenv() β†’ os.getenv("OPENROUTER_API_KEY")



Railway Production:

Railway Variables β†’ os.getenv("OPENROUTER_API_KEY")

```

---

## πŸ“Š Monitoring & Management

### **View Logs**
1. Go to Railway dashboard
2. Click on your project
3. Click **"Deployments"** tab
4. Click on the latest deployment
5. View real-time logs

### **Check Usage**
- Railway free tier: **$5 credit/month**
- Your app should use: **~$2-3/month**
- Monitor usage in **"Usage"** tab

### **Redeploy (After Code Changes)**
1. Make changes locally
2. Test locally (`python main.py`)
3. Commit and push to GitHub:
   ```bash

   git add .

   git commit -m "Your changes"

   git push origin main

   ```
4. Railway **auto-deploys** within 1-2 minutes!

---

## πŸ†˜ Troubleshooting

### **Problem: API calls fail (404 errors)**
**Solution**: API routes must start with `/api/`
- βœ… Correct: `POST /api/grouping`
- ❌ Wrong: `POST /grouping`

### **Problem: Static files not loading (CSS/JS missing)**
**Solution**: 
1. Verify files are in `backend/static/` folder
2. Check browser console for 404 errors
3. Ensure paths in HTML are relative (e.g., `/assets/css/styles.css`)

### **Problem: OpenRouter API errors**
**Solution**:
1. Verify API key is correct in Railway Variables
2. Check you have credits in your OpenRouter account
3. View logs in Railway to see exact error message

### **Problem: Server won't start**
**Solution**:
1. Check Railway logs for error messages
2. Verify `requirements.txt` has all dependencies
3. Ensure `Procfile` command is correct

---

## 🎯 Success Checklist

After deployment, verify:

- [ ] Railway URL loads the main page
- [ ] All navigation links work
- [ ] Login system works (student/teacher)
- [ ] Student dashboard loads
- [ ] Teacher dashboard loads
- [ ] AMS questionnaire works and saves
- [ ] Cooperative questionnaire works and saves
- [ ] AI grouping creates groups successfully
- [ ] Student data persists after refresh
- [ ] API calls complete without errors
- [ ] No console errors in browser DevTools

---

## πŸ’‘ Next Steps

Once deployed successfully:

1. **Share the Railway URL** with your teacher
2. **Test from different devices** (phone, tablet)
3. **Monitor Railway dashboard** for any errors
4. **Keep your OpenRouter API key secure**
5. **Consider upgrading Railway plan** if you exceed free tier

---

## πŸ“ž Support Resources

- **Railway Docs**: https://docs.railway.app
- **OpenRouter Docs**: https://openrouter.ai/docs
- **FastAPI Docs**: https://fastapi.tiangolo.com
- **Your Deployment Guide**: `RAILWAY_DEPLOYMENT.md`

---

## πŸŽ‰ Congratulations!

Your TalimBot is now a **real, independent website** accessible from anywhere! πŸš€

**Your app URL**: `https://your-project.up.railway.app`

Teachers and students can access it from:
- βœ… Home computers
- βœ… School computers
- βœ… Phones (any device with internet)
- βœ… Tablets

No need for localhost, no need for running Python locally - it's fully online! 🌐