Merge branch 'user-db-service' into auth-deploy

This commit is contained in:
JBB0807 2025-04-21 10:28:38 -07:00
commit a625b077cc
11 changed files with 1416 additions and 1 deletions

1
.gitignore vendored
View file

@ -22,4 +22,3 @@ dist-ssr
*.njsproj
*.sln
*.sw?

12
user-db-service/.env Normal file
View file

@ -0,0 +1,12 @@
# Environment variables declared in this file are automatically made available to Prisma.
# See the documentation for more detail: https://pris.ly/d/prisma-schema#accessing-environment-variables-from-the-schema
# Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server, MongoDB and CockroachDB.
# See the documentation for all the connection string options: https://pris.ly/d/connection-strings
#use this when testing local, remmber to run the proxy command
DATABASE_URL="postgresql://postgres:wly9H8gjjmxYfg1@localhost:15432/postgres?schema=public"
# DATABASE_URL="postgres://postgres:w2eSd47GJEdqvMf@snakebyte.internal:5432"
# DATABASE_URL=postgres://snakebyte:zVB7lgOiKr89dq6@localhost:5432/snakebyte?sslmode=disable
NODE_PORT=3000

24
user-db-service/app.js Normal file
View file

@ -0,0 +1,24 @@
const express = require('express');
const { PrismaClient } = require('@prisma/client');
const app = express();
const adminRouter = require("./routes/AdminRouter");
const instructorRouter = require("./routes/InstructorRouter");
const studentRouter = require("./routes/StudentRouter");
// require('dotenv').config(); // prisma client already loads .env apparently, double check before deploying
const port = process.env.NODE_PORT; // Use env for port
console.log('NODE_PORT:', port);
const prisma = new PrismaClient();
app.use(express.json());
//use routes of other pages
app.use("/student", studentRouter);
app.use("/admin", adminRouter);
app.use("/instructor", instructorRouter);
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});

69
user-db-service/fly.toml Normal file
View file

@ -0,0 +1,69 @@
# fly.toml app configuration file generated for snakebyte on 2025-01-04T01:06:05-08:00
#
# See https://fly.io/docs/reference/configuration/ for information about how to use this file.
#
app = 'db-user-service'
primary_region = 'sea'
[env]
PRIMARY_REGION = 'sea'
[[mounts]]
source = 'pg_data'
destination = '/data'
[[services]]
protocol = 'tcp'
internal_port = 5432
auto_start_machines = false
[[services.ports]]
port = 5432
handlers = ['pg_tls']
[services.concurrency]
type = 'connections'
hard_limit = 1000
soft_limit = 1000
[[services]]
protocol = 'tcp'
internal_port = 5433
auto_start_machines = false
[[services.ports]]
port = 5433
handlers = ['pg_tls']
[services.concurrency]
type = 'connections'
hard_limit = 1000
soft_limit = 1000
[checks]
[checks.pg]
port = 5500
type = 'http'
interval = '15s'
timeout = '10s'
path = '/flycheck/pg'
[checks.role]
port = 5500
type = 'http'
interval = '15s'
timeout = '10s'
path = '/flycheck/role'
[checks.vm]
port = 5500
type = 'http'
interval = '15s'
timeout = '10s'
path = '/flycheck/vm'
[[metrics]]
port = 9187
path = '/metrics'
https = false

1217
user-db-service/package-lock.json generated Normal file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,12 @@
{
"dependencies": {
"@prisma/client": "^6.1.0",
"express": "^5.1.0",
"nodemon": "^3.1.9",
"prisma": "^6.1.0"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "nodemon app.js"
}
}

View file

@ -0,0 +1,19 @@
generator client {
provider = "prisma-client-js"
binaryTargets = ["native", "windows"]
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model users {
UserId Int @id @default(autoincrement())
Name String?
Email String?
Password String?
Role String?
GoogleId String?
LoginType String?
}

View file

@ -0,0 +1,4 @@
# database microservice
- store database tables
- issue and re-issue auth tokens (jwt)
- handle password reset into db

View file

@ -0,0 +1,25 @@
const express = require("express");
const adminRouter = express.Router();
// Endpoint to fetch custom query (avoid raw queries if possible)
adminRouter.get('/query', async (req, res) => {
// double check if user is admin first from jwt
const query = req.body.query
const response = prisma.$queryRaw`${query}`;
res.status(400).json({ error: 'Custom queries are not supported.' });
});
// Fetch top users (update logic as per your requirements)
adminRouter.get('/update', async (req, res) => {
try {
const users = await prisma.user.findMany({
orderBy: { username: 'asc' }, // Example sorting
});
res.json(users);
} catch (err) {
res.status(500).json({ error: err.message });
}
});
module.exports = adminRouter;

View file

@ -0,0 +1,17 @@
const express = require("express");
const instructorRouter = express.Router();
// For new users sign-up via Google oAuth:
instructorRouter.post('/register-user', async (req, res) => {
try {
const { username, password } = req.body;
const newUser = await prisma.user.create({
data: { username, email, password },
});
res.json({ message: 'User added successfully', user: newUser });
} catch (err) {
res.status(500).json({ error: err.message });
}
});
module.exports = instructorRouter;

View file

@ -0,0 +1,17 @@
const express = require("express");
const studenRouter = express.Router();
// Add a new student
studenRouter.post('/add-user', async (req, res) => {
try {
const { username, email } = req.body;
const newUser = await prisma.user.create({
data: { username, email },
});
res.json({ message: 'User added successfully', user: newUser });
} catch (err) {
res.status(500).json({ error: err.message });
}
});
module.exports = studenRouter;