generated from dataforgoodfr/d4g-project-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
190 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -160,4 +160,5 @@ dmypy.json | |
cython_debug/ | ||
|
||
# Precommit hooks: ruff cache | ||
.ruff_cache | ||
.ruff_cache | ||
.env* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from http.server import HTTPServer | ||
|
||
from peer_session import upserver | ||
|
||
host_name = "localhost" | ||
server_port = 8080 | ||
|
||
if __name__ == "__main__": | ||
web_server = HTTPServer((host_name, server_port), upserver.UpServer) | ||
print(f"Server listening http://{host_name}:{server_port}") | ||
|
||
try: | ||
web_server.serve_forever() | ||
except KeyboardInterrupt: | ||
pass | ||
|
||
web_server.server_close() | ||
print("Server stopped.") |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import logging | ||
import os | ||
import time | ||
|
||
import psycopg2 | ||
|
||
|
||
class DB: | ||
def __init__(self) -> None: | ||
self.connection = None | ||
self.configure() | ||
self.connect() | ||
|
||
def configure(self) -> None: | ||
"""Configure the connection to the database.""" | ||
self.database_host = os.environ.get("DATABASE_HOST") | ||
self.database_port = os.environ.get("DATABASE_PORT") | ||
self.database_name = os.environ.get("DATABASE_NAME") | ||
self.database_user = os.environ.get("DATABASE_USER") | ||
self.database_password = os.environ.get("DATABASE_PASSWORD") | ||
|
||
logging.debug(f"Connecting to host {self.database_host}") | ||
logging.debug(f"port : {self.database_port}") | ||
logging.debug(f"database : {self.database_name}") | ||
logging.debug(f"user : {self.database_user}") | ||
|
||
def connect(self) -> None: | ||
"""Connect to a PostgreSQL database.""" | ||
try: | ||
if self.connection is None: | ||
self.connection = psycopg2.connect( | ||
database=self.database_name, | ||
user=self.database_user, | ||
password=self.database_password, | ||
host=self.database_host, | ||
port=self.database_port, | ||
) | ||
except psycopg2.DatabaseError: | ||
logging.exception | ||
finally: | ||
logging.info("Database connection opened successfully.") | ||
|
||
def healthcheck(self) -> float: | ||
"""Make sure the Postgres database is reachable.""" | ||
start_time = time.perf_counter() | ||
self.connection.cursor().execute("SELECT 1") | ||
end_time = time.perf_counter() | ||
|
||
elapsed_time = round((end_time - start_time) * 1000, 3) | ||
return elapsed_time |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from http.server import BaseHTTPRequestHandler | ||
|
||
from peer_session.db import DB | ||
|
||
|
||
class UpServer(BaseHTTPRequestHandler): | ||
def do_get(self) -> None: | ||
if self.path == "/up": | ||
self.do_UP() | ||
return | ||
|
||
self.send_response(420) | ||
self.send_header("Content-type", "text/html") | ||
self.end_headers() | ||
self.wfile.write( | ||
bytes("He who breaks a thing has lost the path of wisdom.", "utf-8"), | ||
) | ||
|
||
def do_up(self) -> None: | ||
database = DB() | ||
try: | ||
time_ms = database.healthcheck() | ||
self.send_response(200) | ||
self.send_header("Content-type", "json") | ||
self.end_headers() | ||
self.wfile.write(bytes(f'{{"up": true, "time": "{time_ms}ms"}}', "utf-8")) | ||
except Exception: | ||
self.send_response(500) | ||
self.send_header("Content-type", "json") | ||
self.end_headers() | ||
self.wfile.write(bytes('{"up": false, "time": 0}', "utf-8")) | ||
return |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.