Skip to content

Commit

Permalink
Solved deployment with restapi (#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
uo282892 authored May 4, 2023
2 parents c314322 + 5b76ba0 commit 12781b5
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lomap_en3a.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
3 changes: 3 additions & 0 deletions docker-compose-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion restapi/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ COPY . /app
WORKDIR /app
#Install the dependencies
RUN npm install
CMD [ "npm", "start" ]
CMD [ "npm", "run", "prod" ]
#CMD [ "npm", "start" ]
11 changes: 11 additions & 0 deletions restapi/api.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
9 changes: 9 additions & 0 deletions restapi/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions restapi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
40 changes: 33 additions & 7 deletions restapi/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
});
}

35 changes: 20 additions & 15 deletions restapi/src/controllers/places/PlacesController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,25 @@ import {validationResult} from "express-validator";
import { Document } from "mongoose";

const getPlaces = async (req: Request, res: Response): Promise<Response> => {
try {
const documents: Array<Document<unknown, {}, Placemark>> = await Promise.resolve(
Place.find()
);
const places: Array<Placemark> = 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<Document<unknown, {}, Placemark>> = await Promise.resolve(
Place.find()
);
const places: Array<Placemark> = 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<Response> => {
try {
res.header('Access-Control-Allow-Origin', '*');
console.log(req.body);
const errors = validationResult(req);
if (!errors.isEmpty()) {
Expand All @@ -39,10 +41,11 @@ const addPlace = async (req: Request, res: Response): Promise<Response> => {
catch (error) {
return res.status(500).send("An error has occurred while adding a place: " + error)
}
}
};

const deletePlace = async (req: Request, res: Response): Promise<Response> => {
try {
res.header('Access-Control-Allow-Origin', '*');
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({errors: errors.array()})
Expand All @@ -59,6 +62,7 @@ const deletePlace = async (req: Request, res: Response): Promise<Response> => {

const updatePlace = async (req: Request, res: Response): Promise<Response> => {
try {
res.header('Access-Control-Allow-Origin', '*');
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({errors: errors.array()})
Expand All @@ -81,6 +85,7 @@ const updatePlace = async (req: Request, res: Response): Promise<Response> => {

const findPlaceByTitle = async (req: Request, res: Response): Promise<Response> => {
try {
res.header('Access-Control-Allow-Origin', '*');
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({errors: errors.array()})
Expand Down
2 changes: 1 addition & 1 deletion webapp/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 12781b5

Please sign in to comment.