Skip to content

Commit faa2fe2

Browse files
committed
Merge branch 'recover-latest' into feature/STR-56-DataModels
2 parents f98488c + 53b233f commit faa2fe2

File tree

20 files changed

+665
-75
lines changed

20 files changed

+665
-75
lines changed

.github/workflows/backend.yml

Lines changed: 0 additions & 38 deletions
This file was deleted.

.github/workflows/cd-backend.yml

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
name: CD - Deploy Backend
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
build_number:
7+
description: 'CI Build Number'
8+
required: true
9+
type: string
10+
11+
jobs:
12+
deploy:
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
- name: Checkout code
17+
uses: actions/checkout@v4
18+
19+
- name: Download build artifact from CI workflow
20+
env:
21+
GH_TOKEN: ${{ secrets.PAT_TOKEN }}
22+
run: |
23+
echo "Fetching artifact for build #${{ inputs.build_number }}"
24+
25+
CI_RUN_ID=$(gh run list --workflow "CI - Build Backend" --json databaseId,number -q ".[] | select(.number == ${{ inputs.build_number }}) | .databaseId")
26+
27+
if [ -z "$CI_RUN_ID" ]; then
28+
echo "Could not find CI run with build number ${{ inputs.build_number }}"
29+
exit 1
30+
fi
31+
32+
echo "Found CI run ID: $CI_RUN_ID"
33+
gh run download $CI_RUN_ID --name node-backend-${{ inputs.build_number }}
34+
ls -l
35+
36+
- name: Extract artifact
37+
run: |
38+
tar -xzf node-backend-${{ inputs.build_number }}.tar.gz
39+
ls -la output
40+
41+
- name: Setup Node.js
42+
uses: actions/setup-node@v4
43+
with:
44+
node-version: "20"
45+
46+
- name: Install Vercel CLI
47+
run: npm install -g vercel
48+
49+
- name: Deploy to Vercel
50+
env:
51+
VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }}
52+
VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID_BACKEND }}
53+
VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID_BACKEND }}
54+
run: |
55+
cd output
56+
vercel pull --yes --environment=production --token=$VERCEL_TOKEN
57+
vercel deploy --prod --token=$VERCEL_TOKEN

.github/workflows/ci-backend.yml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
name: CI - Build Backend
2+
3+
on:
4+
push:
5+
branches: [main, backend]
6+
paths:
7+
- 'shatter-backend/**'
8+
9+
pull_request:
10+
branches: [main, backend]
11+
paths:
12+
- 'shatter-backend/**'
13+
14+
jobs:
15+
build:
16+
runs-on: ubuntu-latest
17+
18+
steps:
19+
- name: Checkout code
20+
uses: actions/checkout@v4
21+
22+
- name: Setup Node.js
23+
uses: actions/setup-node@v4
24+
with:
25+
node-version: "20"
26+
27+
- name: Install dependencies
28+
run: |
29+
cd shatter-backend
30+
npm ci
31+
32+
- name: Build project
33+
run: |
34+
cd shatter-backend
35+
npm run build
36+
37+
- name: Package build artifacts
38+
run: |
39+
cd shatter-backend
40+
mkdir output
41+
cp -r dist package.json package-lock.json vercel.json output/
42+
tar -czf node-backend-${{ github.run_number }}.tar.gz output
43+
mv node-backend-${{ github.run_number }}.tar.gz $GITHUB_WORKSPACE/
44+
45+
- name: Upload artifact
46+
uses: actions/upload-artifact@v4
47+
with:
48+
name: node-backend-${{ github.run_number }}
49+
path: node-backend-${{ github.run_number }}.tar.gz

shatter-backend/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@ dist-ssr
2424
*.sw?
2525

2626
.env
27+
.vercel

shatter-backend/api/index.ts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import 'dotenv/config';
2+
import mongoose from 'mongoose';
3+
import app from '../src/app';
4+
5+
const MONGODB_URI = process.env.MONGO_URI;
6+
7+
let connectionPromise: Promise<void> | null = null;
8+
9+
async function connectDB() {
10+
if (mongoose.connection.readyState === 1) {
11+
return;
12+
}
13+
14+
if (mongoose.connection.readyState !== 0) {
15+
await mongoose.disconnect();
16+
connectionPromise = null;
17+
}
18+
19+
if (connectionPromise) {
20+
return connectionPromise;
21+
}
22+
23+
if (!MONGODB_URI) {
24+
throw new Error('MONGO_URI is not set in environment variables');
25+
}
26+
27+
connectionPromise = (async () => {
28+
try {
29+
await mongoose.connect(MONGODB_URI, {
30+
maxPoolSize: 10,
31+
minPoolSize: 2,
32+
maxIdleTimeMS: 30000,
33+
serverSelectionTimeoutMS: 5000,
34+
socketTimeoutMS: 45000,
35+
});
36+
37+
mongoose.connection.on('error', (err) => {
38+
console.error('MongoDB connection error:', err);
39+
connectionPromise = null;
40+
});
41+
42+
mongoose.connection.on('disconnected', () => {
43+
console.log('MongoDB disconnected');
44+
connectionPromise = null;
45+
});
46+
47+
} catch (error) {
48+
console.error('Failed to connect to MongoDB:', error);
49+
connectionPromise = null;
50+
throw error;
51+
}
52+
})();
53+
54+
return connectionPromise;
55+
}
56+
57+
connectDB().catch(console.error);
58+
59+
app.use(async (req, res, next) => {
60+
try {
61+
await connectDB();
62+
next();
63+
} catch (error: any) {
64+
res.status(500).json({
65+
error: 'Database connection failed',
66+
message: error.message
67+
});
68+
}
69+
});
70+
71+
export default app;

shatter-backend/package-lock.json

Lines changed: 21 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

shatter-backend/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@
1313
"license": "ISC",
1414
"description": "",
1515
"dependencies": {
16+
"bcryptjs": "^3.0.3",
1617
"dotenv": "^17.2.3",
1718
"express": "^5.1.0",
1819
"mongoose": "^8.19.2",
1920
"zod": "^4.1.12"
2021
},
2122
"devDependencies": {
2223
"@eslint/js": "^9.38.0",
24+
"@types/bcryptjs": "^2.4.6",
2325
"@types/express": "^5.0.5",
2426
"@types/node": "^24.9.2",
2527
"eslint": "^9.38.0",

shatter-backend/src/app.ts

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,18 @@
1-
// Express framework gives us fucnctions to creeate web server, handle routes, process HTTP req and send responses
21
import express from 'express';
3-
import userRoutes from './routes/user_route.ts'; // these routes define how to handel requests to /api/users
2+
import userRoutes from './routes/user_route'; // these routes define how to handle requests to /api/users
3+
import authRoutes from './routes/auth_routes';
4+
import eventRoutes from './routes/event_routes';
45

5-
// Creating express application as a single "app" object, started in server.ts
6-
// This object represents entire web server and will be used to:
7-
// register Middleware, define routes ....
86
const app = express();
97

10-
// Middleware setup
11-
// responsible for parsing incoming JSON request bodies, making req.body usable in controllers
128
app.use(express.json());
139

14-
// Defining routes
15-
// a simple route that sends back plain text "Hello" when anyone visits the app in browser
1610
app.get('/', (_req, res) => {
1711
res.send('Hello');
1812
});
1913

20-
// Mounts the user routes under the path 'api/users'
21-
// any routes defined in user_route.ts will be accessible with URLs starting with /api/users
2214
app.use('/api/users', userRoutes);
15+
app.use('/api/auth', authRoutes);
16+
app.use('/api/events', eventRoutes);
2317

24-
25-
// Export the configured Express app, so that server.ts can import it
26-
// Keeping app setup separate from server startup makes the code modular and testable
2718
export default app;

0 commit comments

Comments
 (0)