Skip to content

Commit

Permalink
cleaner Docker and wsgi runners
Browse files Browse the repository at this point in the history
  • Loading branch information
JulienParis committed Jun 6, 2019
1 parent f7f5fa9 commit ec7b774
Show file tree
Hide file tree
Showing 25 changed files with 851 additions and 267 deletions.
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
venv
.git
nginx
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ celerybeat-schedule

# Environments
.env
.env.*
.venv
env/
venv/
Expand Down
66 changes: 66 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
export APP=toktok

export D_FOLDER=docker-files
export DC_FOLDER=dockercomposes
export DF_FOLDER=dockerfiles

#other variable definition
# DC := docker-compose
# DF := Dockerfile.

export DC=docker-compose
export DF=Dockerfile.

export DC_PREFIX= $(shell pwd)/${DC}
export DC_PREFIX_FOLDER= $(shell pwd)/${D_FOLDER}/${DC_FOLDER}/${DC}

export DC_FOLDER_SUBPATH= ${D_FOLDER}/${DC_FOLDER}
export DF_FOLDER_SUBPATH= ${D_FOLDER}/${DF_FOLDER}

export APP_PATH := $(shell pwd)

export BACKEND=${APP_PATH}
export FRONTEND=${APP_PATH}



### ============ ###
### network
### ============ ###

network-stop:
docker network rm ${APP}
network:
@docker network create ${APP} 2> /dev/null; true


### ============ ###
### backend
### ============ ###

# dev
gunicorn-dev:
${DC} -f ${DC_PREFIX_FOLDER}-gunicorn-dev.yml up --build
gunicorn-dev-stop:
${DC} -f ${DC_PREFIX_FOLDER}-gunicorn-dev.yml down

# prod
gunicorn-prod:
${DC} -f ${DC_PREFIX_FOLDER}-gunicorn-prod.yml up --build -d
gunicorn-prod-stop:
${DC} -f ${DC_PREFIX_FOLDER}-gunicorn-prod.yml down


### ============================= ###
### main make / docker commands
### ============================= ###

# dev
up: network gunicorn-dev
down: gunicorn-dev-stop network-stop
restart: down up

# prod
up-prod: network gunicorn-prod
down-prod: gunicorn-prod-stop network-stop
restart-prod: down-prod up-prod
Empty file removed admin/__init__.py
Empty file.
84 changes: 61 additions & 23 deletions appserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"""

import os
from dotenv import load_dotenv
from pathlib import Path # python3 only

### + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + ###
### IMPORT COMMAND LINE INTERFACE (CLI)
Expand All @@ -30,16 +32,17 @@
### + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + ###

@click.command()
@click.option('--mode', default="dev", nargs=1, help="The <mode> you need to run the app : dev (default), dev_email, prod, preprod" )
@click.option('--docker', default="docker_off", nargs=1, help="Are you running the app with <docker> : docker_off | docker_on" )
@click.option('--host', default="None", nargs=1, help="The <host> name you want the app to run on : localhost(default=None) | <IP_NUMBER> " )
@click.option('--port', default="None", nargs=1, help="The <port> number you want the app to run on : 4100 (default=None) | <PORT_NUMBER>")
@click.option('--mongodb', default="local", nargs=1, help="The <mongodb> you need to run the app : local | distant | server" )
@click.option('--rsa', default="no", nargs=1, help="The <rsa> mode (RSA encrypt/decrypt for forms), protects '/login' + '/register' + '/password_forgotten' + '/reset_password': 'no' (default), 'yes'" )
@click.option('--anojwt', default="no", nargs=1, help="The <anojwt> mode (needs an anonymous JWT for login and register routes), affects '/login' + '/register' + '/password_forgotten' : 'no' (default), 'yes'" )
@click.option('--antispam', default="no", nargs=1, help="The <antispam> mode (add hidden field check for forms) protects '/login' + '/register' + '/password_forgotten' : 'no' (default), 'yes'" )
@click.option('--antispam_val', default="", nargs=1, help="The <antispam_val> to check in forms against spams : '' (default), <your-string-to-check>" )
def app_runner(mode, docker, host, port, mongodb, rsa, anojwt, antispam, antispam_val) :
@click.option('--mode', default="dev", nargs=1, help="The <mode> you need to run the app : dev (default), dev_email, prod, preprod" )
@click.option('--docker', default="docker_off", nargs=1, help="Are you running the app with <docker> : docker_off | docker_on" )
@click.option('--host', default="localhost", nargs=1, help="The <host> name you want the app to run on : localhost(default) | <IP_NUMBER> " )
@click.option('--port', default="4100", nargs=1, help="The <port> number you want the app to run on : 4100 (default) | <PORT_NUMBER>")
@click.option('--mongodb', default="local", nargs=1, help="The <mongodb> you need to run the app : local | distant | server" )
@click.option('--rsa', default="no", nargs=1, help="The <rsa> mode (RSA encrypt/decrypt for forms), protects '/login' + '/register' + '/password_forgotten' + '/reset_password': 'no' (default), 'yes'" )
@click.option('--anojwt', default="no", nargs=1, help="The <anojwt> mode (needs an anonymous JWT for login and register routes), affects '/login' + '/register' + '/password_forgotten' : 'no' (default), 'yes'" )
@click.option('--antispam', default="no", nargs=1, help="The <antispam> mode (add hidden field check for forms) protects '/login' + '/register' + '/password_forgotten' : 'no' (default), 'yes'" )
@click.option('--antispam_val', default="", nargs=1, help="The <antispam_val> to check in forms against spams : '' (default), <your-string-to-check>" )
@click.option('--https', default="false", nargs=1, help="The <https> mode you want the app to run on : true | false")
def app_runner(mode, docker, host, port, mongodb, rsa, anojwt, antispam, antispam_val, https) :

"""
runner for the TOKTOK backend Flask app
Expand All @@ -48,15 +51,15 @@ def app_runner(mode, docker, host, port, mongodb, rsa, anojwt, antispam, antispa
"python appserver.py"
you can also enter some arguments in the command line :
--mode : dev | prod | dev_email
--mode : dev | dev_email | prod | preprod
--docker : docker_off | docker_on
--host : localhost | <your_IP>
--port : <your_favorite_port>
--mongodb : local | distant | server
--anojwt : yes | no
--rsa : yes | no
--antispam : yes | no
--antispam_val : '' | <your-string-to-check>
--antispam_val : '' | <your-string-to-check>
"""

Expand All @@ -78,35 +81,70 @@ def app_runner(mode, docker, host, port, mongodb, rsa, anojwt, antispam, antispa
log.debug("=== anojwt : %s", anojwt)
log.debug("=== antispam : %s", antispam)
log.debug("=== antispam_val : %s", antispam_val)
log.debug("=== https : %s", https)
print()

### READ ENV VARS DEPENDING ON MODE
if mode in ['dev', 'dev_email']:
env_path_global = Path('.') / 'example.env.global'

if mongodb in ['local'] :
env_path_mongodb = Path('.') / 'example.env.mongodb'
else :
env_path_mongodb = Path('.') / '.env.mongodb'

if mode == 'dev_email' :
env_path_mailing = Path('.') / '.env.mailing'
else :
env_path_mailing = Path('.') / 'example.env.mailing'

else :
env_path_global = Path('.') / '.env.global'
env_path_mongodb = Path('.') / '.env.mongodb'
env_path_mailing = Path('.') / '.env.mailing'

load_dotenv(env_path_global, verbose=True)
load_dotenv(env_path_mongodb, verbose=True)
load_dotenv(env_path_mailing, verbose=True)

### OVERIDE AND SET UP ENV VARS FROM CLI
# os.environ["HTTPS_MODE"] = https
# if https == "true" :
# http_mode = "https"
# else :
# http_mode = "http"
os.environ["DOMAIN_ROOT"] = host
os.environ["DOMAIN_PORT"] = port
os.environ["DOCKER_MODE"] = docker
os.environ["MONGODB_MODE"] = mongodb

# os.environ["SERVER_NAME"] = host + ":" + port
# os.environ["DOMAIN_NAME"] = http_mode + "://" + host + ":" + port


log.debug("\n--- STARTING TOKTOK AUTH API ---\n")

from auth_api.application import create_app

app = create_app(
app_name='AUTH_API',

run_mode=mode,
docker_mode=docker,
mongodb_mode=mongodb,

RSA_mode=rsa,
anojwt_mode=anojwt,
antispam_mode=antispam,
antispam_value=antispam_val
)

### apply / overwrites host configuration
if host == "None" :
app_host = app.config["DOMAIN_ROOT"]
else :
app_host = host

### apply / overwrites port configuration
if port == "None" :
app_port = int(app.config["DOMAIN_PORT"])
else :
app_port = port
if mode != "dev" :
log.debug("=== mode : %s", mode)
os.environ["FLASK_CONFIGURATION"] = str(mode)
config_name = os.getenv('FLASK_CONFIGURATION', 'dev') ### 'default' for local dev
log.debug("=== config_name : %s", config_name)

app_debug = app.config["DEBUG"]

Expand All @@ -119,7 +157,7 @@ def app_runner(mode, docker, host, port, mongodb, rsa, anojwt, antispam, antispa
print("=== "*40)
print("=== "*40)
print()
app.run( debug=app_debug, host=app_host, port=app_port, threaded=True )
app.run( debug=app_debug, host=host, port=int(port), threaded=True )



Expand Down
Loading

0 comments on commit ec7b774

Please sign in to comment.