Skip to content

Instantly share code, notes, and snippets.

@ipedrazas

ipedrazas/app.py

Created Jun 12, 2020
Embed
What would you like to do?
Gitops Training - python
from flask import Flask, jsonify, request
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
@app.route('/ip', methods=["GET"])
def ip():
pips= request.headers.get('X-Forwarded-For')
pip = pips.split(",")
return jsonify({'ip': pip[0]}), 200
@app.route('/api')
def api():
twitter_handles = [
"GianArb",
"ipedrazas",
"perezpardojc",
"gitirabassi",
"errordeveloper",
"LachlanEvenson",
"Phisc0",
"sublimino"
]
return jsonify(results=twitter_handles)
@app.route('/')
def hello():
return "Hello API!"
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')
#
# Docker image for flask
#
FROM python:3.8.3-alpine as base
# Builder
FROM base as builder
RUN mkdir /install
WORKDIR /install
COPY requirements.txt /requirements.txt
RUN pip install --install-option="--prefix=/install" -r /requirements.txt
# App
FROM base as app
COPY --from=builder /install /usr/local
RUN \
addgroup --system --gid 30000 user && \
adduser --system --shell /sbin/nologin -u 30000 -G user user
WORKDIR /home/user
COPY . /home/user
# Unit test
FROM app as test
ENV GOSS_VERSION="v0.3.6"
USER root
RUN \
set -x; \
\
apk add curl && \
curl --fail -L "https://github.com/aelsabbahy/goss/releases/download/${GOSS_VERSION}/goss-linux-amd64" \
-o /usr/local/bin/goss \
&& chmod +rx /usr/local/bin/goss
USER user
COPY goss-flask.yaml goss.yaml
RUN goss -g - validate < goss.yaml
# Metadata
FROM app as final
CMD ["gunicorn", "-w 4", "-b 0.0.0.0:8000", "app:app"]
ARG vcs_ref=unespecied
LABEL org.label-schema.name="Control Plane Flask image" \
org.label-schema.description="Control Plane Base Flask image" \
org.label-schema.build-date="${build_date}" \
org.label-schema.vcs-url="https://github.com/controlplaneio/docker-base-images" \
org.label-schema.vcs-ref="${vcs_ref}" \
io.control-plane.ci-agent="circleci" \
io.control-plane.test="goss-passed"
package:
python:
installed: false
port:
tcp:8000:
listening: false
ip: []
user:
user:
exists: true
uid: 30000
gid: 30000
groups:
- user
home: /home/user
shell: /sbin/nologin
command:
pip list:
exit-status: 0
stdout:
- Package Version
- '-------------- -------'
- aniso8601 8.0.0
- click 7.1.2
- Flask 1.1.2
- Flask-Admin 1.5.6
- flask-restplus 0.13.0
- Flask-SSLify 0.1.5
- gunicorn 19.10.0
- itsdangerous 1.1.0
- Jinja2 2.11.2
- MarkupSafe 1.1.1
- pip 20.1
- pyrsistent 0.16.0
- pytz 2020.1
- setuptools 46.3.0
- six 1.14.0
- Werkzeug 1.0.1
- wheel 0.34.2
- WTForms 2.3.1
stderr: []
timeout: 10000
Flask>=1.1.2,<1.2
flask-restplus>=0.13,<0.14
Flask-SSLify>=0.1.5,<0.2
Flask-Admin>=1.5.3,<1.6
gunicorn>=19,<20
flask-cors>=3.0.6,<3.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.