Skip to content

Commit

Permalink
new version for Code Engine (#5)
Browse files Browse the repository at this point in the history
* version for Code Engine

* Db2 driver version

* Db2 driver version

* old CF branch
  • Loading branch information
data-henrik authored Jul 5, 2022
1 parent f5640e2 commit 152243f
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 22 deletions.
55 changes: 55 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
########
# Python dependencies builder
#
# Full official Debian-based Python image
FROM python:3.8 AS builder

# Always set a working directory
WORKDIR /app
# Sets utf-8 encoding for Python et al
ENV LANG=C.UTF-8
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1


# Ensures that the python and pip executables used
# in the image will be those from our virtualenv.
ENV PATH="/venv/bin:$PATH"

# Install OS package dependencies.
# Do all of this in one RUN to limit final image size.
RUN apt-get update && \
apt-get install -y --no-install-recommends \
build-essential && \
rm -rf /var/lib/apt/lists/*

# Setup the virtualenv
RUN python -m venv /venv

# Install Python deps
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt


# Actual container
#
#
FROM python:3.8-slim AS app

# Extra python env
ENV PATH="/venv/bin:$PATH"

WORKDIR /app
EXPOSE 8080

# copy in Python environment
COPY --from=builder /venv /venv

RUN apt-get update && \
apt-get install -y --no-install-recommends \
libxml2 && \
rm -rf /var/lib/apt/lists/*

# copy in the rest of the app
COPY ./ ./
ENTRYPOINT ["gunicorn", "--bind", "0.0.0.0:8080","worldcities:app"]
1 change: 0 additions & 1 deletion Procfile

This file was deleted.

11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
**Note**: A former version of the tutorial deployed the app to Cloud Foundry Public on IBM Cloud. You can find the material in the [branch **cloudfoundry**](https://github.com/IBM-Cloud/cloud-sql-database/tree/cloudfoundry).

# IBM Cloud SQL Database
This tutorial shows how to provision a SQL (relational) database service, create a table and load a larger data set, city information into the database. Thereafter, we deploy a web app "worldcities" to make use of that data and show how to access the cloud database. The app is written in Python using the Flask framework.

This tutorial is part of [IBM Cloud tutorials](https://cloud.ibm.com/docs/tutorials?topic=solution-tutorials-tutorials) and discussed as [SQL Database for Cloud Data](https://cloud.ibm.com/docs/solution-tutorials?topic=solution-tutorials-sql-database).

# Up and running in few steps
To get this SQL database-backed app up and running only few steps and about 10 minutes are needed. Please follow the steps outlined in the IBM Cloud tutorial.


# Local testing

- Install the requirements to run Python directly or build and run the container image.
- Set the environment variable **DASHDB_SSLDSN** to the value obtained from the Db2 Warehouse credentials for the key **ssldsn**.

# Feedback
If you have feedback on the code or the related tutorial, please either open an issue on this repository or leave documentation feedback at the above mentioned tutorial.
If you have feedback on the code or the related tutorial, please open an issue on this repository.
7 changes: 0 additions & 7 deletions manifest.yml

This file was deleted.

6 changes: 4 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
Flask
ibm_db
Flask==2.0.3
gunicorn
ibm_db == 3.1.2
python-dotenv==0.15.0
1 change: 0 additions & 1 deletion runtime.txt

This file was deleted.

2 changes: 1 addition & 1 deletion templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{% block title %}World Cities{% endblock %}
{% block content %}
<h1>Hello Cities of the World!</h1>
<p>This app is named "{{ app["application_name"] }}". You can search for information on cities with a population over 1000. Use the local name.
<p>You can search for information on cities with a population over 1000. Use the local name.
As an alternative, you can directly access city information using "/city/name", e.g., <a href="/city/Friedrichshafen">/city/Friedrichshafen</a>.</p>
<br>
<p>
Expand Down
21 changes: 13 additions & 8 deletions worldcities.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# (C) 2017 IBM
# (C) 2017-2022 IBM
# Author: Henrik Loeser
#
# Very short sample app used with Db2 Warehouse on Cloud to demonstrate
Expand All @@ -12,22 +12,27 @@
import json
import ibm_db

# for loading .env
from dotenv import load_dotenv

# load environment
load_dotenv()

app = Flask(__name__)

# get service information if on IBM Cloud Platform
if 'VCAP_SERVICES' in os.environ:
db2info = json.loads(os.environ['VCAP_SERVICES'])['dashDB'][0]
db2cred = db2info["credentials"]
appenv = json.loads(os.environ['VCAP_APPLICATION'])
if 'DASHDB_SSLDSN' in os.environ:
db2cred = os.getenv('DASHDB_SSLDSN')
else:
raise ValueError('Expected cloud environment')
# log error, but continue - it might be before service binding
app.logger.error('No Db2 credentials configured.')

# handle database request and query city information
def city(name=None):
# connect to DB2
rows=[]
try:
db2conn = ibm_db.connect(db2cred['ssldsn'], "","")
db2conn = ibm_db.connect(db2cred, "","")
if db2conn:
# we have a Db2 connection, query the database
sql="select * from cities where name=? order by population desc"
Expand Down Expand Up @@ -55,7 +60,7 @@ def city(name=None):
# main page to dump some environment information
@app.route('/')
def index():
return render_template('index.html', app=appenv)
return render_template('index.html')

# for testing purposes - use name in URI
@app.route('/hello/<name>')
Expand Down

0 comments on commit 152243f

Please sign in to comment.