From 5eb3d8a14410f5a9a0886ed2f50734f621208b5d Mon Sep 17 00:00:00 2001 From: Emanuele Paolini Date: Thu, 4 Jan 2024 06:10:14 +0100 Subject: [PATCH] fix migration --- .gitignore | 2 +- README.md | 35 ++--------------------------------- api/{.env => .env.sample} | 1 + api/.gitignore | 2 +- api/index.ts | 1 - api/migrate-mysql.sh | 13 +++++++++++-- api/models/CurriculumExam.js | 2 -- api/router.js | 8 +++++--- 8 files changed, 21 insertions(+), 43 deletions(-) rename api/{.env => .env.sample} (94%) diff --git a/.gitignore b/.gitignore index 78c2d020..7f2236d5 100644 --- a/.gitignore +++ b/.gitignore @@ -9,7 +9,7 @@ api/webroot/css/ api/webroot/node_modules api/webroot/index.html api/node_modules -api/env.local +api/.env db attachments-db nextjs diff --git a/README.md b/README.md index fd86535c..8bc8067d 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,8 @@ bash migrate-mysql.sh # avvio server side Il codice del server si trova nella cartella `api`. +Copiare il file `.env.sample` in `.env` e mettere almeno la password di ADMIN +nella configurazione. Avviare il server da un terminale ```bash @@ -30,39 +32,6 @@ Se dà errore `TextEncoder is not defined` bisogna aggiornare `node`. La versione 21 di node dà un deprecation warning con la libreria mongoose: conviene usare node v20.9.0. -Per testare le API si può usare una applicazione -come `postman`. -Ma alcune prove si possono fare direttamente con curl (qui si usa jq per -fare il pretty print del JSON): -```bash -$ curl -s -H 'Content-Type: application/json' \ - -X POST \ - -d '{ "name": "Analisi Numerica", "credits": 6, "sector": "MAT/08", "code": "AA112" }' \ - http://localhost:3000/api/v0/exams | jq -{ - "code": 200, - "message": "OK" -} -``` -... e poi -```bash -$ curl -s -H 'Content-Type: application/json' -X GET http://localhost:3000/api/v0/exams | jq -{ - "code": 200, - "message": "OK", - "data": [ - { - "_id": "624402d236d1e6f756a31993", - "name": "Analisi Numerica", - "code": "AA112", - "sector": "MAT/08", - "credits": 6, - "__v": 0 - } - ] -} -``` - # avvio lato client Il codice `react` del client si trova nella cartella `frontend`. diff --git a/api/.env b/api/.env.sample similarity index 94% rename from api/.env rename to api/.env.sample index 5f4bf1ce..5b9183b4 100644 --- a/api/.env +++ b/api/.env.sample @@ -4,6 +4,7 @@ CORS_ORIGIN="http://localhost:3000" # comma separated URLS SESSION_SECRET="" # set a string or leave empty for a random string PERMISSION_SECRET="" +MONGO_DB_URI="mongodb://localhost:27017" MONGO_DB="caps" ADMIN_USER="admin" # if set the user is created diff --git a/api/.gitignore b/api/.gitignore index a477aed3..73bc0713 100644 --- a/api/.gitignore +++ b/api/.gitignore @@ -1,3 +1,3 @@ db/ - +.env .env.local \ No newline at end of file diff --git a/api/index.ts b/api/index.ts index 572cdabe..9de820c1 100755 --- a/api/index.ts +++ b/api/index.ts @@ -10,7 +10,6 @@ const { randomUUID } = require('crypto') const MongoStore = require('connect-mongo') const dotenv = require('dotenv') dotenv.config() // read environment variabiles from .env -dotenv.config({ path: `.env.local`, override: true }) // override with .env.local const ApiException = require('./exceptions/ApiException'); const router = require('./router'); diff --git a/api/migrate-mysql.sh b/api/migrate-mysql.sh index 5143872f..8d06f5fa 100755 --- a/api/migrate-mysql.sh +++ b/api/migrate-mysql.sh @@ -11,14 +11,23 @@ export REMOTE_IP=$( ssh ${REMOTE_USERNAME}@${REMOTE_HOSTNAME} docker inspect cap echo "REMOTE_IP: ${REMOTE_IP}" # obtaining mysql password -export MYSQL_PASSWORD=$( ssh root@caps.dm.unipi.it "grep CAPS_DB_PASSWORD docker/caps-matematica/caps.env" | cut -f2 -d= ) +export MYSQL_PASSWORD=$( ssh ${REMOTE_USERNAME}@${REMOTE_HOSTNAME} "grep CAPS_DB_PASSWORD docker/caps-matematica/caps.env" | cut -f2 -d= ) echo "MYSQL_PASSWORD:" $(echo ${MYSQL_PASSWORD} | wc -c) "chars" # open port forwarding and run js import script migrate-mysql.js +echo "opening ssh tunnel" ssh -N -L 3306:${REMOTE_IP}:3306 root@caps.dm.unipi.it & pidof_ssh=$! + +function cleanup { + echo "closing ssh tunnel " + kill ${pidof_ssh} +} + +trap cleanup EXIT + sleep 1 echo "start js script" node migrate-mysql.js -kill ${pidof_ssh} +#kill ${pidof_ssh} diff --git a/api/models/CurriculumExam.js b/api/models/CurriculumExam.js index 43c21db3..e9dc43b1 100644 --- a/api/models/CurriculumExam.js +++ b/api/models/CurriculumExam.js @@ -67,10 +67,8 @@ const CurriculumFreeChoiceGroup = CurriculumExam.discriminator("FreeChoiceGroup" exports.CurriculumFreeChoiceGroup = CurriculumFreeChoiceGroup -/* const CurriculumFreeChoiceExam = CurriculumExam.discriminator("FreeChoiceExam", new mongoose.Schema( {} )) exports.CurriculumFreeChoiceExam = CurriculumFreeChoiceExam; -*/ \ No newline at end of file diff --git a/api/router.js b/api/router.js index dc901d36..a04e3d07 100644 --- a/api/router.js +++ b/api/router.js @@ -21,6 +21,8 @@ const router = new express.Router() // JSON parsing middleware router.use(express.json()) +const SERVER_URL = process.env.SERVER_URL || "http://localhost:3000" + function response_envelope(controller) { return async function(req, res, next) { try { @@ -66,13 +68,13 @@ if (env.OAUTH2_CLIENT_ID) { tokenURL: env.OAUTH2_TOKEN_URL, clientID: env.OAUTH2_CLIENT_ID, clientSecret: env.OAUTH2_CLIENT_SECRET, - callbackURL: `${env.SERVER_URL}/api/v0/login/oauth2/callback`, + callbackURL: `${SERVER_URL}/api/v0/login/oauth2/callback`, usernameField: env.OAUTH2_USERNAME_FIELD, })) console.log("OAUTH2 authentication enabled") console.log(`OAUTH2_AUTHORIZE_URL: ${env.OAUTH2_AUTHORIZE_URL}`) console.log(`OAUTH2_CLIENT_ID: ${env.OAUTH2_CLIENT_ID}`) - console.log(`SERVER_URL: ${env.SERVER_URL}`) + console.log(`SERVER_URL: ${SERVER_URL}`) } else { console.log("OAUTH2 authentication disabled") console.log("set OAUTH2_CLIENT_ID to enable") @@ -112,7 +114,7 @@ router.get('/login/oauth2/callback', console.log("OAUTH2 authentication callback") const user = req.user.toObject() console.log(`login ${JSON.stringify(user)}`) - res.redirect(process.env.SERVER_URL) + res.redirect(SERVER_URL) }) router.post('/logout', function(req, res) {