talimbot / resources_references /RAILWAY_SETUP_GUIDE.md
parinazAkef's picture
Fresh start: TalimBot project without binary files
2fe573b
# πŸš‚ 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! 🌐