Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
itiel authored and itiel committed Mar 18, 2017
0 parents commit c616166
Show file tree
Hide file tree
Showing 14 changed files with 192 additions and 0 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SANIC_LOGO="OVERRIDE LOGO USING CONFIG"
66 changes: 66 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#Me
.github
.idea
venv
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*,cover
.hypothesis/

# Translations
*.mo
*.pot

# Django stuff:
*.log

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# pyenv python configuration file
.python-version
Empty file added app/.env
Empty file.
1 change: 1 addition & 0 deletions app/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FROM python:3.6-onbuild
Empty file added app/__init__.py
Empty file.
1 change: 1 addition & 0 deletions app/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sanic
24 changes: 24 additions & 0 deletions app/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import os

from sanic import Sanic
from sanic.response import json

from app.utils.helper import hello_world

SANIC_PREFIX = "SANIC_"

app = Sanic()


@app.route("/hello-world")
async def test(request):
return json(hello_world())


app.static('/static', './static') # while in docker files from static will be served by ngnix
if __name__ == "__main__":
for k, v in os.environ.items():
if k.startswith(SANIC_PREFIX):
_, config_key = k.split(SANIC_PREFIX, 1)
app.config[config_key] = v
app.run(host="0.0.0.0", port=8000, debug=True)
10 changes: 10 additions & 0 deletions app/static/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hello World</title>
</head>
<body>
SANIC HTML - HELLO WORLD :)
</body>
</html>
Empty file added app/utils/__init__.py
Empty file.
2 changes: 2 additions & 0 deletions app/utils/helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def hello_world():
return {"hello": "world"}
22 changes: 22 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
app:
restart: always
build: ./app
expose:
- "8000"
volumes:
- /usr/src/app/static
env_file: .env
command: bash -c "cd /usr/src && APP_SETTING=.env python -m app.server"

nginx:
restart: always
build: ./nginx/
ports:
- "80:80"
volumes:
- /www/static
volumes_from:
- app
links:
- app:app

3 changes: 3 additions & 0 deletions nginx/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM tutum/nginx
RUN rm /etc/nginx/sites-enabled/default
ADD sites-enabled/ /etc/nginx/sites-enabled
17 changes: 17 additions & 0 deletions nginx/sites-enabled/sanic_project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
server {

listen 80;
server_name example.org;
charset utf-8;

location /static {
alias /usr/src/app/static/;
}

location / {
proxy_pass http://app:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
45 changes: 45 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Sanic & Nginx & docker-compose example
# About
Simple and easy to use skelton-project using [Sanic] behined [nginx].

Using docker and docker-compose to orchestrate everything.

The app is based on the (great) blog-post for flask:
https://realpython.com/blog/python/dockerizing-flask-with-compose-and-machine-from-localhost-to-the-cloud/

# How to run?
- git clone the project
- Start a new docker-machine:
`docker-machine create -d virtualbox sanic;`
- Attach to the machine:
`eval "$(docker-machine env sanic)"`
- Build the containers (This will take a while for the first time!):
`docker-compose build`
- Create and start containers:
`docker-compose up -d`

## Ok what now?
- Use `docker-machine ip sanic` to get the machine IP
- Go to `MACHINE-IP/hello-world` or `MACHINE-IP/static/index.html` and validate everything worked.
- That's It!

## Features:
- /static/* files are served using the nginx.
- Use the .env file to override or add config values (i override the logo):
- Just add SANIC_ prefix before var name.

### Development

Want to contribute? Great!
Feel free to open PR/Issue :)

License
----

MIT - **Free Software, Hell Yeah!**

[//]: #URLs

[sanic]: <https://github.com/channelcat/sanic>
[nginx]: <https://www.nginx.com/resources/wiki/>

0 comments on commit c616166

Please sign in to comment.