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

Solved deployment with restapi #177

Merged
merged 17 commits into from
May 4, 2023
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/lomap_en3a.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,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 Expand Up @@ -82,7 +82,7 @@ jobs:
user: ${{ secrets.DEPLOY_USER }}
key: ${{ secrets.DEPLOY_KEY }}
command: |
wget https://raw.githubusercontent.com/arquisoft/lomap_en3a/deployment-with-https/docker-compose-deploy.yml -O docker-compose.yml
wget https://raw.githubusercontent.com/arquisoft/lomap_en3a/deployment-ivan/docker-compose-deploy.yml -O docker-compose.yml
docker-compose stop
docker-compose rm -f
docker-compose pull
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);
});
}

5 changes: 5 additions & 0 deletions restapi/src/controllers/places/PlacesController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {validationResult} from "express-validator";

const getPlaces = async (req: Request, res: Response): Promise<Response> => {
try {
res.header('Access-Control-Allow-Origin', '*');
const places: Array<PlaceType> = await Promise.resolve(Place.find());
return res.status(200).send(places);
}
Expand All @@ -15,6 +16,7 @@ const getPlaces = async (req: Request, res: Response): Promise<Response> => {

const addPlace = 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 @@ -34,6 +36,7 @@ const addPlace = async (req: Request, res: Response): Promise<Response> => {

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 @@ -50,6 +53,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 @@ -71,6 +75,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