Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with the connection with the restAPI #172

Closed
Pelayo-Reguera opened this issue May 3, 2023 · 1 comment · Fixed by #177
Closed

Problem with the connection with the restAPI #172

Pelayo-Reguera opened this issue May 3, 2023 · 1 comment · Fixed by #177
Assignees
Labels
bug Something isn't working

Comments

@Pelayo-Reguera
Copy link
Contributor

The restAPI must be upgraded from HTTP to HTTPS, as the webapp consuming its service is working on HTTPS.

@Pelayo-Reguera Pelayo-Reguera added the bug Something isn't working label May 3, 2023
@Pelayo-Reguera Pelayo-Reguera added this to the Final presentation milestone May 3, 2023
@uo282892
Copy link
Contributor

uo282892 commented May 3, 2023

restapi/server.js

var fs = require('fs');
var https = require('https');
const express = require('express');
const path = require('path');
const cors = require("cors");
const bp = require("body-parser");
const api = require("./api");

var app = express();
const port = 5000;
const portHttps = 5443;

//Load certificates
var privateKey  = fs.readFileSync('claves/privkey.pem');
var certificate = fs.readFileSync('claves/fullchain.pem');
var credentials = {key: privateKey, cert: certificate};

//This will make sure that we will serve everything through https
app.all('*', function(req, res, next){
    if (req.secure) {
        return next();
    }
    res.redirect('https://'+req.hostname + ":" + portHttps + req.url);
});

const metricsMiddleware: RequestHandler = promBundle({ includeMethod: true });
app.use(metricsMiddleware);

app.use(cors());
app.use(bp.json());

app.use("/api", api)
const mongoose = require("mongoose")
const db_uri = require("./settings");
const uri: string = db_uri;
const options = { useNewUrlParser: true, useUnifiedTopology: true};
mongoose.connect(uri, options).then(
    () => console.log("Database is listening")).catch((error: Error) =>
    console.log("Error while connecting to the database:" + error))

app.listen(port, ():void => {
    console.log('Restapi listening on '+ port);
}).on("error",(error:Error)=>{
    console.error('Error occured: ' + error.message);
});

var httpsServer = https.createServer(credentials, app);
httpsServer.listen(portHttps, (): void => {
    console.log("Restapi listening on " + portHttps);
})
.on("error", (error: Error) => {
    console.error("Error occured: " + error.message);
});

restapi/Dockerfile: change the cmd line for this:

CMD node server.js

docker-compose-deploy.yml

version: '3.5'
services:
  restapi:
    image: ghcr.io/arquisoft/lomap_en3a/restapi:latest
    volumes:
      - /etc/letsencrypt/live/lomapen3a.cloudns.ph/privkey.pem:/app/claves/privkey.pem
      - /etc/letsencrypt/live/lomapen3a.cloudns.ph/fullchain.pem:/app/claves/fullchain.pem
    ports:
      - "5000:5000"
      - "5443:5443"
  webapp:
    image: ghcr.io/arquisoft/lomap_en3a/webapp:latest
    volumes:
      - /etc/letsencrypt/live/lomapen3a.cloudns.ph/privkey.pem:/app/claves/privkey.pem
      - /etc/letsencrypt/live/lomapen3a.cloudns.ph/fullchain.pem:/app/claves/fullchain.pem
    ports:
      - "443:443"
    depends_on: 
      - restapi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants