Deployment Guide
Complete guide for deploying the Toto ecosystem with Next.js API Routes and App Hosting.
π Ecosystem Architectureβ
Complete Toto Ecosystem Deploymentβ
- toto (Main App): Next.js API Routes + App Hosting
- toto-bo (Backoffice): Next.js API Routes + App Hosting
- Unified Technology Stack: Both projects use the same deployment patterns
Deployment Architectureβ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Toto Ecosystem β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β βββββββββββββββββββ βββββββββββββββββββ β
β β toto-f9d2f β β toto-bo β β
β β (Main App) β β (Backoffice) β β
β β β β β β
β β π App Hosting β β π App Hosting β β
β β π§ API Routes β β π§ API Routes β β
β β ποΈ Firestore β β ποΈ Firestore β β
β β (Main DB) β β (Local DB) β β
β βββββββββββββββββββ βββββββββββββββββββ β
β β
β π External API Integration: toto-bo β toto-f9d2f β
β π Unified Security: JWT + Session-based Auth β
β π Unified Monitoring: Performance + Health Metrics β
β π Cloud Secret Manager: Secure environment management β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
π Quick Deploymentβ
UI Screenshot Capture (for docs/marketing)β
-
Main app (
toto-app):- Local:
cd toto-app; npm run devthennpm run capture:ui:local - Prod:
cd toto-app; npm run capture:ui - Output:
toto-app/public/ui/*.png
- Local:
-
Backoffice (
toto-bo):- Local:
cd toto-bo; node scripts/capture-ui.mjs http://localhost:5000 /dashboard - Prod:
cd toto-bo; node scripts/capture-ui.mjs https://bo.betoto.pet /dashboard - Output:
toto-bo/public/ui/*.png
- Local:
-
Landing (
toto-landing):- Desktop screenshot used in Solution section:
public/bo-dashboard.png - Mobile overlay:
public/home-mobile.png
- Desktop screenshot used in Solution section:
toto (Main App) Deploymentβ
Initialize App Hosting:
# Navigate to main app
cd ../toto
# Initialize App Hosting configuration
firebase init apphosting
# Create backend (if not exists)
firebase apphosting:backends:create --project toto-f9d2f
Next.js API Routes Deployment:
# Build and deploy (API routes are part of App Hosting)
npm run build
firebase deploy --only hosting
# Verify deployment
firebase hosting:list
App Hosting Deployment (Automatic):
# Push to main branch triggers automatic deployment
git push origin main
# Check App Hosting status
firebase apphosting:backends:list
toto-bo (Backoffice) Deploymentβ
Initialize App Hosting:
# Navigate to backoffice
cd toto-bo
# Initialize App Hosting configuration
firebase init apphosting
# Create backend (if not exists)
firebase apphosting:backends:create --project toto-bo
Next.js API Routes Deployment:
# Build and deploy (API routes are part of App Hosting)
npm run build
firebase deploy --only hosting
# Verify deployment
firebase hosting:list
App Hosting Deployment (Automatic):
# Push to main branch triggers automatic deployment
git push origin main
# Check App Hosting status
firebase apphosting:backends:list
π§ Configurationβ
Environment Variablesβ
Main App (toto):
# Firebase Configuration
NEXT_PUBLIC_FIREBASE_API_KEY=your_api_key
NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN=your_auth_domain
NEXT_PUBLIC_FIREBASE_PROJECT_ID=toto-f9d2f
NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET=your_storage_bucket
NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID=your_sender_id
NEXT_PUBLIC_FIREBASE_APP_ID=your_app_id
# App Hosting Configuration
NEXT_PUBLIC_APP_HOSTING_BACKEND_ID=your_backend_id
Backoffice (toto-bo):
# Firebase Configuration
NEXT_PUBLIC_FIREBASE_API_KEY=your_api_key
NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN=your_auth_domain
NEXT_PUBLIC_FIREBASE_PROJECT_ID=toto-bo
NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET=your_storage_bucket
NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID=your_sender_id
NEXT_PUBLIC_FIREBASE_APP_ID=your_app_id
# NextAuth Configuration
NEXTAUTH_URL=https://your-backoffice-domain.com
NEXTAUTH_SECRET=your_nextauth_secret
# App Hosting Configuration
NEXT_PUBLIC_APP_HOSTING_BACKEND_ID=your_backend_id
Firebase Configurationβ
firebase.json (Main App):
{
"hosting": {
"public": "out",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "**",
"destination": "/index.html"
}
]
},
"apphosting": {
"backend": "your-backend-id"
}
}
firebase.json (Backoffice):
{
"hosting": {
"public": "out",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "**",
"destination": "/index.html"
}
]
},
"apphosting": {
"backend": "your-backend-id"
}
}
π Monitoring & Health Checksβ
App Hosting Monitoringβ
Check Backend Status:
# List all backends
firebase apphosting:backends:list
# Get backend details
firebase apphosting:backends:get your-backend-id
# View logs
firebase apphosting:backends:logs your-backend-id
Health Check Endpoints:
# Main app health check
curl https://app.betoto.pet/api/health
# Backoffice health check
curl https://your-backoffice-domain.com/api/health
Performance Monitoringβ
App Hosting Metrics:
- Response Time: Monitor API response times
- Throughput: Requests per second
- Error Rate: Failed requests percentage
- Resource Usage: CPU and memory utilization
Custom Metrics:
// Example custom metric
import { performance } from 'firebase/apphosting';
// Track custom metrics
performance.trace('api_request').start();
// ... API logic
performance.trace('api_request').stop();
π Security Configurationβ
Authentication Setupβ
Main App (JWT-based):
// Firebase Auth configuration
const auth = getAuth();
auth.useDeviceLanguage();
// JWT token validation
const validateToken = async (token: string) => {
try {
const decodedToken = await auth.verifyIdToken(token);
return decodedToken;
} catch (error) {
throw new Error('Invalid token');
}
};
Backoffice (Session-based):
// NextAuth.js configuration
export const authOptions: NextAuthOptions = {
providers: [
GoogleProvider({
clientId: process.env.GOOGLE_CLIENT_ID!,
clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
}),
],
session: {
strategy: 'jwt',
},
callbacks: {
async session({ session, token }) {
return session;
},
async jwt({ token, user }) {
return token;
},
},
};
CORS Configurationβ
API Routes CORS:
// Next.js API route with CORS
import { NextApiRequest, NextApiResponse } from 'next';
import Cors from 'cors';
const cors = Cors({
methods: ['GET', 'POST', 'PUT', 'DELETE'],
credentials: true,
origin: ['https://app.betoto.pet', 'https://bo.betoto.pet'],
});
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
await new Promise((resolve) => cors(req, res, resolve));
// Your API logic here
res.json({ message: 'API response' });
}
π Deployment Best Practicesβ
Pre-deployment Checklistβ
- Environment Variables: Verify all required environment variables are set
- Database Migration: Ensure Firestore rules are updated
- API Testing: Test all API endpoints locally
- Build Verification: Confirm successful build process
- Security Review: Validate authentication and authorization
Deployment Processβ
-
Development Testing:
npm run dev
npm run test
npm run build -
Staging Deployment:
# Deploy to staging environment
firebase use staging
firebase deploy -
Production Deployment:
# Deploy to production
firebase use production
firebase deploy
Rollback Strategyβ
Quick Rollback:
# Revert to previous deployment
firebase hosting:clone --from toto-f9d2f:live --to toto-f9d2f:live --version-id previous-version-id
Database Rollback:
# Export current data
firebase firestore:export ./backup
# Import previous data
firebase firestore:import ./backup
π Troubleshootingβ
Common Issuesβ
App Hosting Issues:
- Backend not found: Verify backend ID in firebase.json
- Deployment failed: Check build logs and environment variables
- API routes not working: Verify Next.js configuration
Authentication Issues:
- JWT validation failed: Check Firebase Auth configuration
- Session expired: Verify NextAuth.js settings
- CORS errors: Update CORS configuration
Performance Issues:
- Slow response times: Monitor App Hosting metrics
- High error rates: Check application logs
- Memory leaks: Review application code
Debug Commandsβ
# Check App Hosting status
firebase apphosting:backends:list
# View deployment logs
firebase hosting:list
# Check Firebase project
firebase projects:list
# Verify environment
firebase use --add
π Related Resourcesβ
- App Hosting Documentation: Firebase App Hosting
- Next.js Deployment: Next.js Deployment Guide
- Firebase CLI: Firebase CLI Reference
- Security Best Practices: Firebase Security