-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
85 lines (64 loc) · 2.23 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import logging
import os
import socket
import sys
from pathlib import Path
import yaml
from flask import Flask, g, render_template, request, jsonify
from flask_cors import CORS
from waitress import serve
from utils import system, router
app = Flask(__name__)
@app.before_request
def before_request():
#
# The request handler requires having access to the app context
#
g.app = app
app.logger.info(f"{request.method} {request.path}")
return router.validate_token(os.getenv('AUTH_TOKEN'))
@app.route('/')
def index():
return render_template('index.html')
if __name__ == '__main__':
hostname = socket.gethostname()
logging.basicConfig(
level=logging.INFO, # Set the desired logging level
format=f'%(asctime)s [%(levelname)s] {hostname} %(message)s',
handlers=[
logging.StreamHandler()
]
)
with app.app_context():
app.config.update({"APP_HOME": os.path.dirname(Path(__file__).resolve())})
#
# Checking environment variables
#
app.logger.info('Checking environment...')
if not system.check_env([
'AUTH_TOKEN',
'OPENAI_API_KEY',
'DB_HOST',
'DB_DATABASE',
'DB_USERNAME',
'DB_PASSWORD'
]):
app.logger.error('Missing environment variables')
sys.exit(1)
app.logger.info('Checking config...')
if os.path.exists('config.yaml'):
with open('config.yaml', 'r') as config_file:
config = yaml.safe_load(config_file)
app.config.update(config)
cors_config = app.config.get('cors', {})
CORS(app, resources={r"/*": cors_config})
app.logger.info(f'Loading routes ({os.getcwd()}/routes)...')
router.load_routes(app, "routes")
app.logger.info('Routes loaded.')
open_ai_model = config.get('openai', {}).get('model') or 'gpt-3.5-turbo-1106'
app.logger.info(f'Using OpenAI model: {open_ai_model}')
# Specify the host and port
host = os.getenv('BIND') or config.get('server', {}).get('bind') or '0.0.0.0'
port = int(os.getenv('PORT') or config.get('server', {}).get('port') or '5000')
# Start the Waitress server
serve(app, host=host, port=port)