In this tutorial we will show you how to run a Nginx frontend in conjunction with a Python Flask backend. You will also find the whole example on github https://github.com/sloppyio/flask. To install it locally, please clone the repository, like this:

git clone https://github.com/sloppyio/flask.git

The example JSON has three variables, which we will replace using the change command in the sloppy CLI:

* PROJECT: the name of our sloppy project
* URI: the URL of your sloppy project (e.g. my-project.sloppy-zone)

After you have pulled the Example JSON, use the following command to build and deploy it to sloppy:

sloppy start -var=PROJECT:your-project-name -var=URI:my-project.sloppy.zone flask.json

The sloppy CLI will replace the variables, marked with a $ symbol in the example JSON file below. As soon as the application has started, you can reach it under the defined URL (e.g. http://my-project.sloppy.zone).

You should see the following in your web browser:

Message: Hello world on sloppy.io
Backend: flask.backend.test.marc

Example JSON

Let’s have a closer look at the JSON file, that we have used in this example:

{
    "project": "$PROJECT",
    "services": [
        {
            "id": "frontend",
            "apps": [
                {
                    "id": "nginx",
                    "domain": {
                        "uri": "$URI"
                    },
                    "port_mappings": [
                        {
                            "container_port": 80
                        }
                    ],
                    "mem": 256,
                    "image": "sloppy/nginx",
                    "instances": 1,
                    "env": {
                        "URI": "$URI",
                        "URIBACKEND": "flask.backend.$PROJECT"
                    },
                    "dependencies": [
                        "../backend/flask"
                    ],
                    "health_checks": [
                        {
                            "timeout_seconds": 10,
                            "interval_seconds": 10,
                            "max_consecutive_failures": 2,
                            "path": "/",
                            "type": "HTTP",
                            "grace_period_seconds": 3
                        }
                    ]
                }
            ]
        },
        {
            "id": "backend",
            "apps": [
                {
                    "id": "flask",
                    "mem": 128,
                    "image": "sloppy/flask",
                    "instances": 1,
                    "env": {
                        "URIBACKEND": "flask.backend.$PROJECT",
                        "MESSAGE": "Hello world on sloppy.io"
                    }
                }
            ]
        }
    ]
}

The JSON file defines a frontend and a backend service. In the frontend service the nginx container is configured with the two environment variables URI and URIBACKEND:

This 2 variables are necessary to replace the variables in the nginx server configuration. For that I have created a simple supervisor configuration, which will replace the variables in the nginx configuration with sed. (It is a hack, I know.)

{"env": {
"URI": "$URI",
"URIBACKEND": "flask.backend.$PROJECT"
}
[supervisord]
nodaemon=true

[program:prepare]
command=/bin/bash -c "sed -i s/URIBACKEND/${URIBACKEND}/g /etc/nginx/conf.d/flask.conf && sed -i s/URI/${URI}/g /etc/nginx/conf.d/flask.conf"
priority=1
autorestart=false
startsecs=0
redirect_stderr=True
stdout_logfile=NONE

[program:nginx]
priority=99
command=/bin/bash -c "nginx -g 'daemon off;'"

With these two variables you can simply use the JSON for another project. For that you only have to start the project with different variables:

sloppy start -var=PROJECT:testneu -var=URI:testneu.sloppy.zone flask.json
Did this answer your question?