Update: save Docker nginx Angular Node Flask

This commit is contained in:
Yûki VACHOT 2022-02-01 19:38:39 +01:00
parent 1a3bee1846
commit fcedd7534c
16 changed files with 89 additions and 84 deletions

View file

@ -1,50 +0,0 @@
version: '3.8'
services:
nginx:
image: nginx:latest
volumes:
- ./frontend/conf/nginx.conf:/etc/nginx/nginx.conf
- ./frontend/logs/nginx:/etc/nginx/logs
- ./frontend/cert:/etc/nginx/cert
networks:
- net
ports:
- 80:80
depends_on:
- server
server:
container_name: server
build: frontend/backend
command: python -m flask run
ports:
- "4200:4200"
volumes:
- ./frontend/backend:/data/frontend/backend
- ./frontend/app.py:/data/frontend/app.py
- ./frontend/config.py:/data/frontend/config.py
environment:
- FLASK_APP=app.py
- FLASK_ENV=production
- FLASK_DEBUG=1
- FLASK_RUN_PORT=4200
- PYTHONUNBUFFERED=1
networks:
- net
depends_on:
- frontend
frontend:
container_name: frontend
build: frontend
command: npm run build
volumes:
- ./frontend/src:/data/frontend
- ./frontend/node_modules:/data/frontend/node_modules
environment:
- NODE_ENV=production
networks:
net:
driver: bridge

View file

@ -20,7 +20,7 @@
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"outputPath": "backend/dist/",
"outputPath": "dist/frontend",
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": "src/polyfills.ts",

View file

@ -1,7 +0,0 @@
from backend import create_app
import os
app = create_app(os.environ.get('FLASK_ENV'))
if __name__ == "__main__":
app.run(host='0.0.0.0', DEBUG=True)

View file

@ -1,7 +1,5 @@
FROM python:latest
WORKDIR /data/frontend/backend
COPY requirements.txt requirements.txt
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
COPY . .
WORKDIR /data/frontend

9
frontend/backend/app.py Normal file
View file

@ -0,0 +1,9 @@
print("Import backend")
from application import create_app
print("Import os")
import os
app = create_app(os.environ.get('FLASK_ENV'))
if __name__ == "__main__":
app.run(host='0.0.0.0', DEBUG=True)

View file

@ -1,8 +1,11 @@
print("Import Flask")
from flask import Flask
print("Import Flask-Cors")
from flask_cors import CORS
def create_app(flask_env='development'):
print("create app")
app = Flask(__name__, instance_relative_config=False, static_url_path='')
origin = app.config.get('ALLOW_ORIGIN')
if origin is None:

View file

@ -1,4 +1,6 @@
print("Import responses Flask")
from flask import current_app as app
print("Import Json")
import json

View file

@ -1,5 +1,8 @@
print("Import Flask")
from flask import request, Blueprint, send_from_directory, current_app as app
print("Import Requests")
import requests
print("Import werkzeug")
from werkzeug.exceptions import HTTPException
from .responses import send_message, send_error
from .sessionJWT import create_auth_token, check_auth_token
@ -33,21 +36,6 @@ def handle_exception(e):
return send_error(e.code, e.name)
@bp.route('/', methods=['GET'])
def root():
return send_from_directory("dist", "index.html")
@bp.route('/frontend/backend/dist/<path:path>', methods=['GET'])
def static(path):
return send_from_directory("dist", path)
@bp.route('/assets/<path:path>', methods=['GET'])
def assets(path):
return send_from_directory("dist/assets", path)
# Login
@bp.route('/api/login', methods=['POST'])
def login():

View file

@ -1,5 +1,8 @@
print("Import datetime")
from datetime import datetime, timedelta
print("Import sessionJWT Flask")
from flask import current_app as app
print("Import jwt")
import jwt

View file

@ -1,3 +1,4 @@
print("Import os")
import os
basedir = os.path.abspath(os.path.dirname(__file__))
@ -10,9 +11,9 @@ class Config(object):
FLASK_APP = os.environ.get('FLASK_APP', None)
FLASK_ENV = os.environ.get('FLASK_ENV', None)
FLASK_RUN_PORT = os.environ.get('FLASK_RUN_PORT', 4200)
FLASK_RUN_PORT = os.environ.get('FLASK_RUN_PORT', 5000)
API_URL = os.environ.get('API_URL', 'http://127.0.0.1:5000/api/')
API_URL = os.environ.get('API_URL', 'http://127.0.0.1:5001/api/')
SECRET_KEY = os.environ.get('SECRET_KEY', 'default_secret_key')
ALLOW_ORIGIN = os.environ.get('ALLOW_ORIGIN', None)

View file

@ -17,8 +17,8 @@ http {
proxy_read_timeout 90;
proxy_buffers 32 4k;
upstream server {
server server:4200;
upstream frontend {
server frontend:4200;
}
@ -28,7 +28,7 @@ http {
access_log logs/access.log;
location / {
proxy_pass http://server;
proxy_pass http://frontend;
}
}

View file

@ -0,0 +1,45 @@
version: '3.8'
services:
nginx:
image: nginx:latest
volumes:
- ./conf/nginx.conf:/etc/nginx/nginx.conf
- ./logs/nginx:/etc/nginx/logs
- ./cert:/etc/nginx/cert
networks:
- net
ports:
- 80:80
depends_on:
- frontend
flask-frontend:
container_name: flask-frontend
build: backend
command: python -m flask run
volumes:
- ./backend:/data/backend
environment:
- FLASK_APP=app.py
- FLASK_ENV=development
- FLASK_DEBUG=0
- FLASK_RUN_PORT=5000
depends_on:
- frontend
frontend:
container_name: frontend
build: .
command: npm start
volumes:
- ./src:/data/frontend
- ./node_modules:/data/frontend/node_modules
environment:
- NODE_ENV=production
networks:
- net
networks:
net:
driver: bridge

View file

@ -3,8 +3,8 @@
"version": "0.0.0",
"scripts": {
"ng": "ng",
"start": "ng build --build-optimizer --baseHref=frontend/backend/dist/ && cd .. && python -m flask run",
"build": "ng build --build-optimizer --baseHref=frontend/backend/dist/",
"start": "ng build && node server.js",
"build": "ng build",
"watch": "ng build --watch --configuration development",
"test": "ng test"
},

13
frontend/server.js Normal file
View file

@ -0,0 +1,13 @@
const path = require('path');
const express = require('express');
const app = express();
const port = process.env.PORT || 4200;
app.use(express.static(__dirname + '/dist/frontend'));
app.get('/*', function(req,res) {
res.sendFile(path.join(__dirname+ '/dist/frontend/index.html'));
});
app.listen(port, '0.0.0.0',() => {
console.log (`listening on port ${port}`);
});

View file

@ -1,4 +1,4 @@
export const environment = {
production: true,
debutUrl: 'http://127.0.0.1:4200/api/'
debutUrl: 'http://127.0.0.1:5000/api/'
};

View file

@ -5,7 +5,7 @@
export const environment = {
production: false,
debutUrl: 'http://127.0.0.1:4200/api/'
debutUrl: 'http://127.0.0.1:5000/api/'
};
/*