Skip to content

Commit

Permalink
Merge pull request #48 from Arquisoft/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
pelazas committed Mar 11, 2024
2 parents b07cdc1 + 156f40a commit 9943579
Show file tree
Hide file tree
Showing 78 changed files with 13,934 additions and 1,453 deletions.
43 changes: 41 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:
- run: npm --prefix gatewayservice install
- run: npm --prefix webapp install
- run: npm --prefix webapp run build
- run: npm --prefix webapp run test:e2e
#- run: npm --prefix webapp run test:e2e
docker-push-webapp:
name: Push webapp Docker Image to GitHub Packages
runs-on: ubuntu-latest
Expand All @@ -59,6 +59,43 @@ jobs:
registry: ghcr.io
workdir: webapp
buildargs: API_URI

docker-push-qgservice:
name: Push question generator service Docker Image to GitHub Packages
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
needs: [e2e-tests]
steps:
- uses: actions/checkout@v4
- name: Publish to Registry
uses: elgohr/Publish-Docker-Github-Action@v5
with:
name: arquisoft/wiq_en2a/questiongenerator
username: ${{ github.actor }}
password: ${{ secrets.GH_PAT }}
registry: ghcr.io
workdir: game/qgservice

docker-push-gameservice:
name: Push game service Docker Image to GitHub Packages
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
needs: [e2e-tests]
steps:
- uses: actions/checkout@v4
- name: Publish to Registry
uses: elgohr/Publish-Docker-Github-Action@v5
with:
name: arquisoft/wiq_en2a/gameservice
username: ${{ github.actor }}
password: ${{ secrets.GH_PAT }}
registry: ghcr.io
workdir: game/gameservice

docker-push-authservice:
name: Push auth service Docker Image to GitHub Packages
runs-on: ubuntu-latest
Expand All @@ -76,6 +113,7 @@ jobs:
password: ${{ secrets.GITHUB_TOKEN }}
registry: ghcr.io
workdir: users/authservice

docker-push-userservice:
name: Push user service Docker Image to GitHub Packages
runs-on: ubuntu-latest
Expand All @@ -93,6 +131,7 @@ jobs:
password: ${{ secrets.GITHUB_TOKEN }}
registry: ghcr.io
workdir: users/userservice

docker-push-gatewayservice:
name: Push gateway service Docker Image to GitHub Packages
runs-on: ubuntu-latest
Expand All @@ -113,7 +152,7 @@ jobs:
deploy:
name: Deploy over SSH
runs-on: ubuntu-latest
needs: [docker-push-userservice,docker-push-authservice,docker-push-gatewayservice,docker-push-webapp]
needs: [docker-push-userservice,docker-push-authservice,docker-push-gatewayservice,docker-push-webapp, docker-push-qgservice]
steps:
- name: Deploy over SSH
uses: fifsky/ssh-action@master
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
coverage
docs/build
docs/build
game/qgservice/.env
32 changes: 32 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,34 @@ services:
- mynetwork
environment:
MONGODB_URI: mongodb://mongodb:27017/userdb

questiongeneratorservice:
container_name: qgservice-${teamname:-defaultASW}
image: ghcr.io/arquisoft/wiq_en2a/questiongenerator:latest
profiles: ["dev", "prod"]
build: ./game/qgservice
depends_on:
- mongodb
ports:
- "8003:8003"
networks:
- mynetwork
environment:
MONGODB_URI: mongodb://mongodb:27017/userdb

gameservice:
container_name: gameservice-${teamname:-defaultASW}
image: ghcr.io/arquisoft/wiq_en2a/gameservice:latest
profiles: ["dev", "prod"]
build: ./game/gameservice
depends_on:
- mongodb
ports:
- "8004:8004"
networks:
- mynetwork
environment:
MONGODB_URI: mongodb://mongodb:27017/userdb

gatewayservice:
container_name: gatewayservice-${teamname:-defaultASW}
Expand All @@ -48,13 +76,17 @@ services:
- mongodb
- userservice
- authservice
- questiongeneratorservice
- gameservice
ports:
- "8000:8000"
networks:
- mynetwork
environment:
AUTH_SERVICE_URL: http://authservice:8002
USER_SERVICE_URL: http://userservice:8001
QG_SERVICE_URL: http://questiongeneratorservice:8003
GAME_SERVICE_URL: http://gameservice:8004

webapp:
container_name: webapp-${teamname:-defaultASW}
Expand Down
2 changes: 2 additions & 0 deletions game/gameservice/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
coverage
20 changes: 20 additions & 0 deletions game/gameservice/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Use an official Node.js runtime as a parent image
FROM node:20

# Set the working directory in the container
WORKDIR /usr/src/gameservice

# Copy package.json and package-lock.json to the working directory
COPY package*.json ./

# Install app dependencies
RUN npm install

# Copy the app source code to the working directory
COPY . .

# Expose the port the app runs on
EXPOSE 8004

# Define the command to run your app
CMD ["node", "gameservice.js"]
28 changes: 28 additions & 0 deletions game/gameservice/GameController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
let Game = require('./game-model');
const { createGame } = require('./queries/CreateGame');
const mongoose = require('mongoose');

let GameController = {
/* HACER EN USER - GET LAST GAME BY USER
findByUsername: async (req, res) => {
let game = await User.find({username: req.params.username}).populate("lastGame")
let response = await Game.findById(game[0].lastGame.id).populate("questions").populate("players")
res.json(response);
},*/
create: async (req, res) => {
const { questions, players } = req.body;
console.log(questions, players)
const game = await createGame(questions, players);
res.json(game);
},
delete: async (req, res) => {
await Game.findOneAndDelete({uuid: req.params.id});
res.json({message: "Game deleted"});
},
getById: async (req, res) => {
let game = await Game.find({uuid: req.params.id})
res.json(game);
}
}

module.exports = GameController;
22 changes: 22 additions & 0 deletions game/gameservice/game-model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const mongoose = require('mongoose');

const gameSchema = new mongoose.Schema({
uuid: {
type: String,
required: true,
},
players:[{
type: String,
required: true,
}],
questions:[
{
type: String,
required: true,
}
],
});

const Game = mongoose.model('Game', gameSchema);

module.exports = Game
40 changes: 40 additions & 0 deletions game/gameservice/game-service.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const request = require('supertest');
const { MongoMemoryServer } = require('mongodb-memory-server');

let mongoServer;
let app;

beforeAll(async () => {
mongoServer = await MongoMemoryServer.create();
const mongoUri = mongoServer.getUri();
process.env.MONGODB_URI = mongoUri;
app = require('./gameservice');
});

afterAll(async () => {
app.close();
await mongoServer.stop();
});

describe('Game service', () => {

// mock data
const questions = [{ _id: 'question1_id' }, { _id: 'question2_id' }];
const users = [{ _id: 'user1_id' }, { _id: 'user2_id' }];

it('should create a game and update user lastGame field', async () => {
const response = await request(app).post('/creategame').send({questions,users});
expect(response.status).toBe(200);
expect(response.body).toHaveProperty('_id');

const gameId = response.body._id;
const gameFromDB = await mongoose.model('Game').findById(gameId);
const user1FromDB = await mongoose.model('User').findById('user1_id');
const user2FromDB = await mongoose.model('User').findById('user2_id');

// Assertions for the database state
expect(gameFromDB).toBeTruthy();
expect(user1FromDB.lastGame.toString()).toBe(gameId);
expect(user2FromDB.lastGame.toString()).toBe(gameId);
});
});
32 changes: 32 additions & 0 deletions game/gameservice/gameservice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// gameservice.js
const express = require('express');
const axios = require('axios');
const mongoose = require('mongoose');
const { createGame } = require('./queries/CreateGame');
const GameController = require('./GameController');

const app = express();
const port = 8004;

// app.use(bodyParser.json());
app.use(express.json());

const mongoUri = process.env.MONGODB_URI || 'mongodb://localhost:27017/userdb';
mongoose.connect(mongoUri);

app.get('/', (req, res) => {
res.json({
"hi": "game service"
});
});

// Routes
app.post('/createGame', GameController.create);
app.delete('/deleteGame/:id', GameController.delete);
app.get('/getGame/:id', GameController.getById);

const server = app.listen(port, () => {
console.log(`Question generator Service listening at http://localhost:${port}`);
});

module.exports = server;
Loading

0 comments on commit 9943579

Please sign in to comment.