Skip to content

Commit ebceaa9

Browse files
committed
Feature: health and version
1 parent 1c43ae7 commit ebceaa9

7 files changed

Lines changed: 66 additions & 0 deletions

File tree

requirements.txt

Whitespace-only changes.

src/api/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'''
2+
Публичный интерфейс сервера
3+
'''
4+
5+
__author__ = "gogobobo11"

src/api/api.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from fastapi import FastAPI
2+
from pydantic import BaseModel
3+
4+
5+
class URL(BaseModel):
6+
port: int
7+
host: str
8+
9+
class PublicAPI:
10+
title = "API Server"
11+
version = "0.0.1"
12+
description="ParkTrack server API built with FastAPI"
13+
14+
def __init__(self, db_manager):
15+
self.db_manager = db_manager
16+
self.db_manager.connect()
17+
18+
self.app = FastAPI(
19+
title=self.title,
20+
version=self.version,
21+
description=self.description
22+
)
23+
self._setup_routes()
24+
25+
def run(self, listen_on: URL):
26+
import uvicorn
27+
uvicorn.run(self.app, host=listen_on.host, port=listen_on.port)
28+
29+
def _setup_routes(self):
30+
31+
@self.app.get("/health")
32+
def get_health():
33+
db_ok = self.db_manager.check_connection()
34+
return {"status": "healthy" if db_ok else "degraded"}
35+
36+
@self.app.get("/version")
37+
def get_version():
38+
return {self.version}

src/db_manager/__init__.py

Whitespace-only changes.

src/db_manager/db_manager.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import sqlite3
2+
3+
class DBManager:
4+
def __init__(self, db_name):
5+
self.db_name = db_name
6+
self.conn = None
7+
self.cursor = None
8+
9+
def connect(self):
10+
self.conn = sqlite3.connect(self.db_name)
11+
self.cursor = self.conn.cursor()
12+
13+
def check_connection(self) -> bool:
14+
return True

src/main.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from api.api import PublicAPI, URL
2+
from db_manager.db_manager import DBManager
3+
4+
def main():
5+
api_server = PublicAPI(db_manager=DBManager("mock"))
6+
api_server.run(URL(host="0.0.0.0", port=8000))
7+
8+
if __name__ == "__main__":
9+
main()

src/mock

Whitespace-only changes.

0 commit comments

Comments
 (0)