Skip to content

Commit

Permalink
fix: fastapi pg issue
Browse files Browse the repository at this point in the history
  • Loading branch information
andreiionutdamian committed Feb 8, 2024
1 parent bc1e048 commit 60e111a
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 10 deletions.
6 changes: 5 additions & 1 deletion demo-basic-fastapi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ kubectl apply -f ns.yaml

## Secrets

Copy `.secret.yaml` to `secrets.yaml` and fill in the base64 encoded values. Then apply the secrets to the cluster with the following command:
Copy `.secret.yaml` to `secrets.yaml` and fill in the base64 encoded values using:
```bash
echo -n "Some password or some other secret" | base64
```
Then apply the secrets to the cluster with the following command:

```bash
kubectl apply -f secrets.yaml
Expand Down
15 changes: 15 additions & 0 deletions demo-basic-fastapi/deploy3_postgres/deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,21 @@ spec:
secretKeyRef:
name: basic-test-app-secrets
key: redis-password
- name: POSTGRES_PASSWORD
valueFrom:
secretKeyRef:
name: basic-test-app-secrets
key: postgres-password
- name: POSTGRES_USER
valueFrom:
secretKeyRef:
name: basic-test-app-secrets
key: postgres-user
- name: POSTGRES_DB
valueFrom:
secretKeyRef:
name: basic-test-app-secrets
key: postgres-db
---
apiVersion: v1
kind: Service
Expand Down
8 changes: 4 additions & 4 deletions demo-basic-fastapi/src/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@
from mixins.postgres_mixin import _PostgresMixin
from mixins.redis_mixin import _RedisMixin

__VER__ = '0.4.0'
__VER__ = '0.4.2'


class AppPaths:
PATH_ROOT = {"PATH": "/", "FUNC": "root"}
PATH_STAT = {"PATH": "/stats", "FUNC": "stats" }

class AppHandler(
_PostgresMixin,
_RedisMixin
):
_PostgresMixin,
_RedisMixin,
):
def __init__(self):
self.log = None
self.__setup()
Expand Down
11 changes: 8 additions & 3 deletions demo-basic-fastapi/src/mixins/postgres_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,28 @@ def __maybe_create_tables(self):
self.__pg.commit()

return


def _maybe_setup_postgres(self):
def __get_postgres_config(self):
dct_pg = {k : v for k, v in os.environ.items() if k.startswith("POSTGRES_")}
return dct_pg

def _maybe_setup_postgres(self):
dct_pg = self.__get_postgres_config()
self._has_postgres = False
if len(dct_pg) > 0:
self.P("Setting up Postgres with configuration:\n{}".format(safe_jsonify(dct_pg)))
postgres_host = dct_pg.get("POSTGRES_SERVICE_HOST")
postgres_port = dct_pg.get("POSTGRES_SERVICE_PORT", 5432)
postgres_user = dct_pg.get("POSTGRES_USER")
postgres_password = dct_pg.get("POSTGRES_PASSWORD")
postgres_db = dct_pg.get("POSTGRES_DB")
self.P("Connecting to Postgres at {}:{} with user: {}".format(
postgres_host, postgres_port, postgres_user
))
self.__pg = psycopg2.connect(
host=postgres_host, port=postgres_port,
user=postgres_user, password=postgres_password
user=postgres_user, password=postgres_password,
dbname=postgres_db,
)
pg_server_info = self.__pg.get_dsn_parameters()
self.P("Connected to Postgres at {}:{} with user: {} to database: {}".format(
Expand Down
8 changes: 6 additions & 2 deletions demo-basic-fastapi/src/mixins/redis_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ def __init__(self, *args, **kwargs):
self.__redis = None
return

### Redis

def __get_redis_config(self):
dct_redis = {k : v for k, v in os.environ.items() if k.startswith("REDIS_")}
return dct_redis


def _maybe_setup_redis(self):
self._has_redis = False
dct_redis = {k : v for k, v in os.environ.items() if k.startswith("REDIS_")}
dct_redis = self.__get_redis_config()
if len(dct_redis) > 0:
if "REDIS_MASTER_SERVICE_HOST" in dct_redis:
# this is a redis master/slave setup
Expand Down

0 comments on commit 60e111a

Please sign in to comment.