diff --git a/.github/workflows/lomap_en3a.yml b/.github/workflows/lomap_en3a.yml index 576c26a3..5dce3a04 100644 --- a/.github/workflows/lomap_en3a.yml +++ b/.github/workflows/lomap_en3a.yml @@ -48,7 +48,7 @@ jobs: uses: elgohr/Publish-Docker-Github-Action@v5 #Rest Api redirect env: - API_URI: http://${{ secrets.DEPLOY_HOST }}:5000/api + API_URI: https://${{ secrets.DEPLOY_HOST }}:5000/api with: name: arquisoft/lomap_en3a/webapp username: ${{ github.actor }} diff --git a/docker-compose-deploy.yml b/docker-compose-deploy.yml index 4a1d02de..da840ea6 100644 --- a/docker-compose-deploy.yml +++ b/docker-compose-deploy.yml @@ -2,6 +2,9 @@ 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" webapp: diff --git a/restapi/Dockerfile b/restapi/Dockerfile index 6d6bcf11..4219401b 100644 --- a/restapi/Dockerfile +++ b/restapi/Dockerfile @@ -4,4 +4,5 @@ COPY . /app WORKDIR /app #Install the dependencies RUN npm install -CMD [ "npm", "start" ] \ No newline at end of file +CMD [ "npm", "run", "prod" ] +#CMD [ "npm", "start" ] \ No newline at end of file diff --git a/restapi/api.ts b/restapi/api.ts index e87b5959..b63f0606 100644 --- a/restapi/api.ts +++ b/restapi/api.ts @@ -1,9 +1,20 @@ import express, { Router } from 'express'; import {getPlaces, addPlace, deletePlace, updatePlace, findPlaceByTitle} from "./src/controllers/places/PlacesController"; import {addPlaceChecks, deletePlaceChecks, updatePlaceChecks, findPlaceByTitleChecks} from "./src/controllers/checks" +import cors from "cors"; const api:Router = express.Router() +let hostIp: string = "20.168.251.141"; +api.use( + cors({ + credentials: true, + origin: ["https://20.168.251.141", "https://lomapen3a.cloudns.ph", "https://localhost", + "https://20.168.251.141:443", "https://lomapen3a.cloudns.ph:443", "https://localhost:443"], + allowedHeaders: ["Content-Type", "Authorization"], + preflightContinue: true, + }) +); api.get("/places/list", getPlaces); api.post("/places/add", addPlaceChecks, addPlace); diff --git a/restapi/package-lock.json b/restapi/package-lock.json index 7ad98916..87a6dacc 100644 --- a/restapi/package-lock.json +++ b/restapi/package-lock.json @@ -15,6 +15,7 @@ "express-prom-bundle": "^6.6.0", "express-validator": "^6.14.2", "mongoose": "^7.0.3", + "nodemailer": "^6.9.1", "prom-client": "^14.1.1" }, "devDependencies": { @@ -3781,6 +3782,14 @@ "integrity": "sha512-dFSmB8fFHEH/s81Xi+Y/15DQY6VHW81nXRj86EMSL3lmuTmK1e+aT4wrFCkTbm+gSwkw4KpX+rT/pMM2c1mF+A==", "dev": true }, + "node_modules/nodemailer": { + "version": "6.9.1", + "resolved": "https://registry.npmjs.org/nodemailer/-/nodemailer-6.9.1.tgz", + "integrity": "sha512-qHw7dOiU5UKNnQpXktdgQ1d3OFgRAekuvbJLcdG5dnEo/GtcTHRYM7+UfJARdOFU9WUQO8OiIamgWPmiSFHYAA==", + "engines": { + "node": ">=6.0.0" + } + }, "node_modules/normalize-path": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/normalize-path/-/normalize-path-3.0.0.tgz", diff --git a/restapi/package.json b/restapi/package.json index 520247f9..0f0c9c0d 100644 --- a/restapi/package.json +++ b/restapi/package.json @@ -16,6 +16,7 @@ "express-prom-bundle": "^6.6.0", "express-validator": "^6.14.2", "mongoose": "^7.0.3", + "nodemailer": "^6.9.1", "prom-client": "^14.1.1" }, "devDependencies": { diff --git a/restapi/server.ts b/restapi/server.ts index 005b661a..cc16fa6a 100644 --- a/restapi/server.ts +++ b/restapi/server.ts @@ -4,9 +4,12 @@ import bp from 'body-parser'; import promBundle from 'express-prom-bundle'; import api from "./api"; import db_uri from "./settings"; +import { readFileSync } from "fs"; +import { createServer } from "https" const app: Application = express(); -const port: number = 5000; +const portHttp: number = 5000; +const portHttps: number = 5000; const mongoose = require("mongoose") const uri: string = db_uri; @@ -18,14 +21,37 @@ mongoose.connect(uri, options).then( const metricsMiddleware:RequestHandler = promBundle({includeMethod: true}); app.use(metricsMiddleware); -app.use(cors()); app.use(bp.json()); app.use("/api", api) -app.listen(port, ():void => { - console.log('Restapi listening on '+ port); -}).on("error",(error:Error)=>{ - console.error('Error occured: ' + error.message); -}); +try { + let privateKey = readFileSync("claves/privkey.pem"); + let certificate = readFileSync("claves/fullchain.pem"); + let credentials = { key: privateKey, cert: certificate }; + + app.all('*', function(req, res, next){ + if (req.secure) { + return next(); + } + res.redirect('https://'+ "lomapen3a.cloudns.ph" + ":" + portHttps + req.url); + }); + + createServer(credentials, app) + .listen(portHttps, (): void => { + console.log("Restapi listening on " + portHttps); + }) + .on("error", (error: Error) => { + console.error("Error occured: " + error.message); + }); +} catch (e) { + + app + .listen(portHttp, (): void => { + console.log("Restapi listening on " + portHttp); + }) + .on("error", (error: Error) => { + console.error("Error occured: " + error.message); + }); +} diff --git a/restapi/src/controllers/places/PlacesController.ts b/restapi/src/controllers/places/PlacesController.ts index 324ab991..b3eaacc5 100644 --- a/restapi/src/controllers/places/PlacesController.ts +++ b/restapi/src/controllers/places/PlacesController.ts @@ -5,23 +5,25 @@ import {validationResult} from "express-validator"; import { Document } from "mongoose"; const getPlaces = async (req: Request, res: Response): Promise => { - try { - const documents: Array> = await Promise.resolve( - Place.find() - ); - const places: Array = documents.map((doc) => - doc.toObject() as Placemark - ); - return res.status(200).send(places); - } catch (error) { - return res - .status(500) - .send("An error has occurred while retrieving the list of places: \n\n" + error); - } -}; + try { + res.header('Access-Control-Allow-Origin', '*'); + const documents: Array> = await Promise.resolve( + Place.find() + ); + const places: Array = documents.map((doc) => + doc.toObject() as Placemark + ); + return res.status(200).send(places); + } catch (error) { + return res + .status(500) + .send("An error has occurred while retrieving the list of places: \n\n" + error); + } +} const addPlace = async (req: Request, res: Response): Promise => { try { + res.header('Access-Control-Allow-Origin', '*'); console.log(req.body); const errors = validationResult(req); if (!errors.isEmpty()) { @@ -39,10 +41,11 @@ const addPlace = async (req: Request, res: Response): Promise => { catch (error) { return res.status(500).send("An error has occurred while adding a place: " + error) } -} +}; const deletePlace = async (req: Request, res: Response): Promise => { try { + res.header('Access-Control-Allow-Origin', '*'); const errors = validationResult(req); if (!errors.isEmpty()) { return res.status(400).json({errors: errors.array()}) @@ -59,6 +62,7 @@ const deletePlace = async (req: Request, res: Response): Promise => { const updatePlace = async (req: Request, res: Response): Promise => { try { + res.header('Access-Control-Allow-Origin', '*'); const errors = validationResult(req); if (!errors.isEmpty()) { return res.status(400).json({errors: errors.array()}) @@ -81,6 +85,7 @@ const updatePlace = async (req: Request, res: Response): Promise => { const findPlaceByTitle = async (req: Request, res: Response): Promise => { try { + res.header('Access-Control-Allow-Origin', '*'); const errors = validationResult(req); if (!errors.isEmpty()) { return res.status(400).json({errors: errors.array()}) diff --git a/webapp/Dockerfile b/webapp/Dockerfile index 3cd26560..659c2edf 100644 --- a/webapp/Dockerfile +++ b/webapp/Dockerfile @@ -5,7 +5,7 @@ WORKDIR /app #Install the dependencies RUN npm install -ARG API_URI="http://localhost:5000/api" +ARG API_URI="https://localhost:5000/api" ENV REACT_APP_API_URI=$API_URI #Create an optimized version of the webapp