Containerize the app

master
Nikola Forró 7 years ago
parent 7ef3a0fc81
commit 9134fa9adf

2
.gitignore vendored

@ -1,3 +1 @@
get_api_key.py get_api_key.py
quotes.db*
settings.cfg

@ -0,0 +1,21 @@
FROM node:alpine as builder
WORKDIR /build
COPY frontend/package.json frontend/package-lock.json ./
RUN npm install && mkdir /ng-app && cp -r node_modules/ /ng-app
WORKDIR /ng-app
COPY frontend .
RUN $(npm bin)/ng build --prod --build-optimizer
FROM nginx:alpine
COPY nginx/nginx.conf /etc/nginx/nginx.conf
COPY --from=builder /ng-app/dist /srv/http
EXPOSE 80
ENTRYPOINT ["nginx", "-g", "daemon off;"]

@ -0,0 +1,10 @@
FROM python:alpine
WORKDIR /app
COPY . .
RUN pip install --no-cache-dir --requirement requirements.txt
EXPOSE 5000
ENTRYPOINT ["python", "app.py"]

@ -1,3 +1,5 @@
import os
import flask import flask
import flask_login import flask_login
import flask_restful import flask_restful
@ -10,7 +12,11 @@ from db import db, Quote
app = flask.Flask(__name__) app = flask.Flask(__name__)
app.config.from_envvar('SETTINGS') app.config.update(
ERROR_404_HELP=False,
SQLALCHEMY_TRACK_MODIFICATIONS=False,
SQLALCHEMY_DATABASE_URI=os.getenv('SQLALCHEMY_DATABASE_URI'),
SECRET_KEY=os.getenv('SECRET_KEY'))
db.init_app(app) db.init_app(app)
db.create_all(app=app) db.create_all(app=app)
@ -105,6 +111,7 @@ class QuotesResource(flask_restful.Resource):
if args['filter']: if args['filter']:
q = q.filter(Quote.text.ilike('%{}%'.format(args['filter']))) q = q.filter(Quote.text.ilike('%{}%'.format(args['filter'])))
count = q.count() count = q.count()
# TODO: random order
if args['sort_by']: if args['sort_by']:
col = getattr(Quote, args['sort_by'], None) col = getattr(Quote, args['sort_by'], None)
if col: if col:
@ -144,3 +151,7 @@ class QuotesResource(flask_restful.Resource):
api.add_resource(QuoteResource, '/quotes/<int:id>') api.add_resource(QuoteResource, '/quotes/<int:id>')
api.add_resource(QuotesResource, '/quotes') api.add_resource(QuotesResource, '/quotes')
if __name__ == '__main__':
app.run(host='0.0.0.0', threaded=True, debug=False)

@ -0,0 +1,5 @@
Flask
Flask-Login
Flask-RESTful
Flask-SQLAlchemy
itsdangerous

@ -1,9 +0,0 @@
DESTINATION = /srv/http/quotes/quotes
BUILD_FLAGS = --prod --build-optimizer
.PHONY: all quotes
all: quotes
quotes:
ng build $(BUILD_FLAGS) --output-path $(DESTINATION)

@ -23,7 +23,7 @@ export class QuotesService {
filter = '', sortBy = 'id', sortOrder = 'desc', filter = '', sortBy = 'id', sortOrder = 'desc',
pageNumber = 0, pageSize = 10): Observable<Quotes> { pageNumber = 0, pageSize = 10): Observable<Quotes> {
return this.http.get('/quotes/api/quotes', { return this.http.get('/api/quotes', {
observe: 'response', observe: 'response',
params: new HttpParams() params: new HttpParams()
.set('filter', filter) .set('filter', filter)

@ -3,7 +3,7 @@
<head> <head>
<meta charset="utf-8"> <meta charset="utf-8">
<title>Quotes</title> <title>Quotes</title>
<base href="/quotes/"> <base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico"> <link rel="icon" type="image/x-icon" href="favicon.ico">

@ -0,0 +1,64 @@
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
gzip on;
server {
listen 80;
server_name localhost;
location / {
root /srv/http;
index index.html;
}
location /api {
rewrite ^/api(/.*)$ $1 break;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_http_version 1.1;
tcp_nodelay on;
proxy_pass http://api:5000/;
}
location ~ /\.ht {
deny all;
}
error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
}
Loading…
Cancel
Save