Skip to content

Commit

Permalink
fix migration
Browse files Browse the repository at this point in the history
  • Loading branch information
paolini committed Jan 4, 2024
1 parent 79c0eff commit 5eb3d8a
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 43 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 2 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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`.
Expand Down
1 change: 1 addition & 0 deletions api/.env → api/.env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion api/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
db/

.env
.env.local
1 change: 0 additions & 1 deletion api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
13 changes: 11 additions & 2 deletions api/migrate-mysql.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 [email protected] "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 [email protected] &
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}
2 changes: 0 additions & 2 deletions api/models/CurriculumExam.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,8 @@ const CurriculumFreeChoiceGroup = CurriculumExam.discriminator("FreeChoiceGroup"

exports.CurriculumFreeChoiceGroup = CurriculumFreeChoiceGroup

/*
const CurriculumFreeChoiceExam = CurriculumExam.discriminator("FreeChoiceExam", new mongoose.Schema(
{}
))

exports.CurriculumFreeChoiceExam = CurriculumFreeChoiceExam;
*/
8 changes: 5 additions & 3 deletions api/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 5eb3d8a

Please sign in to comment.