Skip to content

Commit

Permalink
add: custom config & gunicorn config update
Browse files Browse the repository at this point in the history
  • Loading branch information
WEGFan committed Jul 27, 2020
1 parent be52944 commit 5495c92
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 20 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
data/
config/custom_config_local.py

.vscode/
.idea/
Expand Down
31 changes: 21 additions & 10 deletions app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,33 @@
import os
from pathlib import Path

try:
import config.custom_config_local as custom_config
except ImportError as err:
import config.custom_config as custom_config


class Config(object):
APP_PATH: str = str(Path(__file__).parent.resolve())
PROJECT_PATH: str = str(Path(APP_PATH).parent.resolve())

LOG_PATH: str = str(Path(PROJECT_PATH, 'data', 'logs').resolve())
LOG_FILENAME: str = 'app.log'
LOG_PATH: str = str(Path(PROJECT_PATH, custom_config.LOG_PATH).resolve())
LOG_FILENAME: str = custom_config.LOG_FILENAME
LOG_FORMAT: str = '[%(asctime)s] %(levelname)s - [%(module)s] [%(filename)s:%(lineno)s]: %(message)s'
LOG_LEVEL: int = logging.DEBUG if os.getenv('FLASK_DEBUG') == '1' else logging.INFO

CACHE_TYPE = 'redis'
CACHE_REDIS_URL = 'redis://localhost:6379/0'
CACHE_KEY_PREFIX = 'codestats_readme:'
CACHE_DIR = str(Path(PROJECT_PATH, 'data', 'cache').resolve())
CACHE_THRESHOLD = 500
CACHE_DEFAULT_TIMEOUT = 15
CACHE_REDIS_URL: str = custom_config.REDIS_URL

if CACHE_REDIS_URL:
CACHE_TYPE: str = 'redis'
CACHE_KEY_PREFIX: str = 'codestats_readme:'
else:
# if redis url is empty just use filesystem cache
CACHE_TYPE: str = 'filesystem'
CACHE_DIR: str = str(Path(PROJECT_PATH, './data/cache').resolve())
CACHE_THRESHOLD: int = 500
CACHE_DEFAULT_TIMEOUT: int = 15

SVGO_PATH = 'svgo'
SVGO_CONFIG_PATH = str(Path(APP_PATH, 'svgo.yml').resolve())
SVG_OPTIMIZE_ENABLE: bool = custom_config.SVG_OPTIMIZE_ENABLE
SVGO_PATH: str = custom_config.SVGO_PATH
SVGO_CONFIG_PATH: str = str(Path(PROJECT_PATH, custom_config.SVGO_CONFIG_PATH).resolve())
5 changes: 3 additions & 2 deletions app/controllers/history_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,9 @@ def get_history_graph(username: str):
graph = get_graph(day_language_xp_list, config)

svg = graph.to_image('svg', engine='kaleido', width=config.width, height=config.height, scale=1)
optimized_svg = try_optimize_svg(svg.decode('utf-8'))
if current_app.config['SVG_OPTIMIZE_ENABLE']:
svg = try_optimize_svg(svg.decode('utf-8'))

return Response(optimized_svg, mimetype='image/svg+xml', headers={
return Response(svg, mimetype='image/svg+xml', headers={
'Cache-Control': 'max-age=0'
})
2 changes: 2 additions & 0 deletions app/utils/svgo.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@


def try_optimize_svg(original: str) -> str:
if not current_app.config['SVG_OPTIMIZE_ENABLE']:
return original
args = [
current_app.config['SVGO_PATH'],
'--input', '-',
Expand Down
Empty file added config/__init__.py
Empty file.
31 changes: 31 additions & 0 deletions config/custom_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# -*- coding: utf-8 -*-

# NOTE:
# You can duplicate this file as custom_config_local.py in this folder, the application will try to load config in
# custom_config_local.py before custom_config.py.
#
# All paths below are relative to the project folder (which is where run.py at)
# Absolute paths also supported. e.g. /var/log/

# WORKERS: The number of worker processes for handling requests.
WORKERS: int = 4

# LOG_PATH: Where log file stores in. Default is ./data/logs
LOG_PATH: str = r'./data/logs'

# LOG_FILENAME: The log file's filename.
LOG_FILENAME: str = 'app.log'

# REDIS_URL: The URL to connect to a Redis server.
# If empty, a filesystem cache will be used under ./data/cache
REDIS_URL: str = 'redis://localhost:6379/0'

# SVG_OPTIMIZE_ENABLE: Whether or not to optimize SVGs with SVGO before response.
# npm and SVGO is needed to be installed.
SVG_OPTIMIZE_ENABLE: bool = False

# SVGO_PATH: Specifies the path to SVGO.
SVGO_PATH: str = r'/usr/bin/svgo'

# SVGO_CONFIG_PATH: Specifies the path to the config file of SVGO.
SVGO_CONFIG_PATH: str = r'./config/svgo.yml'
File renamed without changes.
20 changes: 13 additions & 7 deletions gunicorn_config.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
# -*- coding: utf-8 -*-
import os
from pathlib import Path

os.makedirs('data/logs', exist_ok=True)
try:
import config.custom_config_local as custom_config
except ImportError as err:
import config.custom_config as custom_config

workers = 4
threads = 2
os.makedirs(custom_config.LOG_PATH, exist_ok=True)

workers = custom_config.WORKERS
worker_class = 'gevent'
bind = '127.0.0.1:2012'
daemon = True
worker_connections = 100
accesslog = './data/logs/gunicorn_app_access.log'
errorlog = './data/logs/gunicorn_app_error.log'
loglevel = 'warning'
accesslog = str(Path(custom_config.LOG_PATH, 'gunicorn_access.log').resolve())
errorlog = str(Path(custom_config.LOG_PATH, 'gunicorn.log').resolve())
loglevel = 'info'
reload = True
timeout = 60
access_log_format = '%(t)s %(l)s %({X-Real-IP}i)s "%(r)s" %(s)s %(b)s "%(f)s" "%(a)s"'
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ kaleido~=0.0.1
marshmallow~=3.7.0
plotly~=4.9.0
gunicorn~=20.0.4; platform_system != 'Windows'
redis~=3.5.3
redis~=3.5.3
gevent~=20.6.2

0 comments on commit 5495c92

Please sign in to comment.