Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TS-1-Implement-stabilize-database-code #79

Merged
merged 6 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 0 additions & 51 deletions requirements-complete.txt

This file was deleted.

4 changes: 1 addition & 3 deletions run_gunicorn.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#!/bin/sh
#
# Init database if defined
# export FLASK_ENV=development
# export FLASK_APP=test_server
# export DB_TYPE=sqlite
export FLASK_APP=test_server
flask init-db

# Start the application server
Expand Down
38 changes: 25 additions & 13 deletions test_server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ def create_app(test_config=None):
Returns:
a Flask application object
"""

print(
f"INFO!Flask app {__name__} started by PID {os.getpid()}."
)
app = Flask(__name__, instance_relative_config=False)

# Initialize Prometheus metrics
Expand Down Expand Up @@ -53,17 +55,27 @@ def create_app(test_config=None):

# Modify database name if SQLite is used.
if 'sqlite' == app.config['DB_TYPE']:
if app.config['DB_PATH'] is None:
app.config['DB_PATH'] = app.instance_path
app.config['DATABASE'] = os.path.join(
app.config['DB_PATH'],
app.config['DB_NAME'] + '.sqlite')
print(f"Database: {app.config['DB_PATH']}/{app.config['DB_NAME']}.{app.config['DB_TYPE']}")
# ensure the instance folder exists
try:
os.makedirs(app.config['DB_PATH'])
except FileExistsError:
print("WARNING! File already exists, reusing.")
# Prevent duplicate initialization.
if None is app.config.get('DATABASE'):
if app.config['DB_PATH'] is None:
app.config['DB_PATH'] = app.instance_path
if app.config['DB_NAME'] is None:
app.config['DB_NAME'] = app.name
app.config['DATABASE'] = os.path.join(
app.config['DB_PATH'],
app.config['DB_NAME'] + '.sqlite')
print(
f"INFO! Setting database {app.config['DATABASE']} ."
)

# ensure the instance folder exists
try:
os.makedirs(app.config['DB_PATH'])
print(f"INFO! Creating SQLite Path {app.config['DB_PATH']} .")
except FileExistsError:
print(
f"WARNING! SQLite Path {app.config['DB_PATH']} already exists, reusing."
)



Expand All @@ -79,6 +91,7 @@ def health():
from . import db
db.init_app(app)


# Echo page returns the call with some additional information
from . import echo
app.register_blueprint(echo.bp)
Expand All @@ -101,5 +114,4 @@ def health():
from . import echo_api
app.register_blueprint(echo_api.bp)


return app
3 changes: 1 addition & 2 deletions test_server/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ def get_db():
if 'db' not in g:
my_db= new_db()
if my_db:
# g.db= new_db()
g.db= my_db
else:
raise NameError("No valid database configured.")
Expand Down Expand Up @@ -84,7 +83,7 @@ def init_db():
with current_app.open_resource('sql/schema.sql') as schema:
my_db.executescript(schema.read().decode('utf8'))
except sqlite3.OperationalError as exc:
print(f"ERROR! Initializing database. Caught {exc}")
print(f"WARNING! Initializing database. Caught {exc}")


@click.command('init-db')
Expand Down
9 changes: 3 additions & 6 deletions test_server/echo_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class EchoData():
headers = {}

def __init__(self, my_request):
# self.local_data = self.set_local_data()
self.remote_data = self.set_remote_data(my_request)

def get_remote_data(self):
Expand All @@ -28,23 +27,21 @@ def get_remote_data(self):

def set_remote_data(self, my_request):
""" Set class data from http request."""
# self.remote_data = my_request.headers
return {
'remote_addr': self.get_remote_ip(my_request),
'url': my_request.url,
'url_charset': my_request.url_charset,
'url_charset': my_request.content_encoding,
'referrer': my_request.referrer,
'user_agent': my_request.user_agent.string,
}

@staticmethod
def get_http_headers(my_request):
""" Get HTTP headers from request."""
# self.headers = self.get_remote_data(my_request)
result = {}
for header in my_request.headers.envrion:
for header in my_request.headers.environ:
if header.startswith('HTTP_'):
result[header]=my_request.headers.envrion[header]
result[header]=my_request.headers.environ[header]

return result

Expand Down
2 changes: 0 additions & 2 deletions test_server/local_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,12 @@ def set_server_state():
"""
hostname = socket.gethostname()
sys_metrics = subprocess.check_output("uptime").decode("utf-8")
# uptime = re.search(r' up (.+?), \d* users', sys_metrics).group(1)
uptime = ""
uptime_re = re.search(r' up (.+?), \d* users', sys_metrics)
if uptime_re:
uptime = uptime_re.group(1)

load = ""
# load = re.search(r' averages: ((\d*\.\d+ ?)+)', sys_metrics).group(1)
load_re = re.search(r' averages: ((\d*\.\d+ ?)+)', sys_metrics)
if load_re:
load = load_re.group(1)
Expand Down
2 changes: 1 addition & 1 deletion test_server/test_server.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ REDIS_API_COUNTER = os.environ.get('REDIS_API_COUNTER') or'api_srv_counter'
DB_TYPE = os.environ.get("DB_TYPE") or 'sqlite'
DB_USER = os.environ.get("DB_USER") or 'root'
DB_USER_PASSWORD = os.environ.get("DB_USER_PASSWORD") or 'password'
DATABASE = os.environ.get("DATABASE") or 'test_server'
DATABASE = os.environ.get("DATABASE") or None
DB_NAME = os.environ.get("DB_NAME") or 'testserver'
DB_PATH = os.environ.get("DB_PATH") or None
DB_HOST = os.environ.get("DB_HOST") or '127.0.0.1'
12 changes: 11 additions & 1 deletion tests/test_factory.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
'''
Test general app creation functions
'''
from test_server import create_app


def test_config():
''' Test app config loading. '''
assert not create_app().testing
assert create_app({'TESTING': True, 'DB_TYPE': None}).testing

def test_app_defaults():
''' Test default db name.'''
app = create_app()
assert 'sqlite' == app.config['DB_TYPE']
assert 'testserver' == app.config['DB_NAME']

def test_hello(client):
''' Test health endpoint. '''
response = client.get('/health')
assert response.data == b'test_server is healthy.'
assert response.data == b'test_server is healthy.'
9 changes: 5 additions & 4 deletions wsgi.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
""" Wsgi definition for Flask application."""
import sys
project_home = u'/home/web/test_server'
if project_home not in sys.path:
sys.path = [project_home] + sys.path
PROJECT_HOME = u'/home/web/test_server'
if PROJECT_HOME not in sys.path:
sys.path = [PROJECT_HOME] + sys.path

from test_server import create_app
app = create_app()

if __name__ == "__main__":
app.run()
app.run()

Loading