| # π 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! π | |