initial user-db-service
This commit is contained in:
parent
578e57ad7a
commit
c7af63a7dd
9 changed files with 1407 additions and 0 deletions
10
user-db-service/.env
Normal file
10
user-db-service/.env
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
# 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
|
||||||
|
|
||||||
|
# DATABASE_URL="postgresql://johndoe:randompassword@localhost:5432/mydb?schema=public"
|
||||||
|
# DATABASE_URL="postgres://postgres:w2eSd47GJEdqvMf@snakebyte.internal:5432"
|
||||||
|
DATABASE_URL=postgres://snakebyte:zVB7lgOiKr89dq6@localhost:5432/snakebyte?sslmode=disable
|
||||||
|
NODE_PORT=3000
|
||||||
47
user-db-service/app.js
Normal file
47
user-db-service/app.js
Normal file
|
|
@ -0,0 +1,47 @@
|
||||||
|
const express = require('express');
|
||||||
|
const { PrismaClient } = require('@prisma/client');
|
||||||
|
const app = express();
|
||||||
|
|
||||||
|
const adminRouter = require("./routes/AdminRouter");
|
||||||
|
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);
|
||||||
|
|
||||||
|
// Fetch top users (update logic as per your requirements)
|
||||||
|
app.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 });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// For new users sign-up via Google oAuth:
|
||||||
|
app.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 });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
app.listen(port, () => {
|
||||||
|
console.log(`Server running at http://localhost:${port}`);
|
||||||
|
});
|
||||||
69
user-db-service/fly.toml
Normal file
69
user-db-service/fly.toml
Normal 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 = 'snakebyte'
|
||||||
|
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
1217
user-db-service/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load diff
12
user-db-service/package.json
Normal file
12
user-db-service/package.json
Normal 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"
|
||||||
|
}
|
||||||
|
}
|
||||||
19
user-db-service/prisma/schema.prisma
Normal file
19
user-db-service/prisma/schema.prisma
Normal 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 String @id
|
||||||
|
Name String?
|
||||||
|
Email String?
|
||||||
|
Password String?
|
||||||
|
Role String?
|
||||||
|
GoogleId String?
|
||||||
|
LoginType String?
|
||||||
|
}
|
||||||
4
user-db-service/readme.md
Normal file
4
user-db-service/readme.md
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
# database microservice
|
||||||
|
- store database tables
|
||||||
|
- issue and re-issue auth tokens (jwt)
|
||||||
|
- handle password reset into db
|
||||||
12
user-db-service/routes/AdminRouter.js
Normal file
12
user-db-service/routes/AdminRouter.js
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
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.' });
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = adminRouter;
|
||||||
17
user-db-service/routes/StudentRouter.js
Normal file
17
user-db-service/routes/StudentRouter.js
Normal 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;
|
||||||
Loading…
Add table
Add a link
Reference in a new issue