2024-03-14 10:20:30 +01:00

52 lines
1.3 KiB
Python

import logging
from flask import Flask, render_template
from redis import Redis
from pygelf import GelfTcpHandler, GelfUdpHandler, GelfTlsHandler, GelfHttpHandler
import os
app = Flask(__name__)
visitor_count = 0
# CONF
cache_port = os.getenv('CACHE_PORT')
cache_host = os.getenv('CACHE_HOST')
app_port = os.getenv('APP_PORT')
def set_logger():
log_handler = GelfUdpHandler(host='logstash' , port=12201)
app.logger.addHandler(log_handler)
# app.logger.setLevel(logging.INFO)
# GET REDIS
def get_redis_connection():
return Redis(host=cache_host, port=cache_port)
def init_visitor_count(redis_db: Redis):
key = 'visitor_count'
if not redis_db.exists(key):
redis_db.set(key, 0)
# MAIN ROUTE
@app.route('/')
def index():
try:
set_logger()
except Exception as e:
print('Error: logger not available')
try:
redis_db = get_redis_connection()
init_visitor_count(redis_db)
visitor_count = redis_db.incr('visitor_count')
app.logger.info("Visit page")
except Exception as e:
return render_template('erreur.html', message=str(e))
if visitor_count == 10:
return render_template('winner.html')
return render_template('index.html', visitor_count=visitor_count)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=app_port)