michaelkri commited on
Commit
ac35357
·
1 Parent(s): 0cc1669

Integrated Turso database

Browse files
.devcontainer/devcontainer.json CHANGED
@@ -9,6 +9,11 @@
9
  // Setting it to 'false' ensures your Dockerfile's CMD command is respected.
10
  "overrideCommand": false,
11
 
 
 
 
 
 
12
  // Any ports to forward from the container to your local machine
13
  "forwardPorts": [7860],
14
 
 
9
  // Setting it to 'false' ensures your Dockerfile's CMD command is respected.
10
  "overrideCommand": false,
11
 
12
+ "runArgs": [
13
+ "--env-file",
14
+ ".env"
15
+ ],
16
+
17
  // Any ports to forward from the container to your local machine
18
  "forwardPorts": [7860],
19
 
.gitignore CHANGED
@@ -1,4 +1,5 @@
1
  */__pycache__
2
  venv
3
  .venv
4
- *.db
 
 
1
  */__pycache__
2
  venv
3
  .venv
4
+ *.db
5
+ .env
app/database.py CHANGED
@@ -2,6 +2,7 @@ from sqlalchemy import create_engine, UnicodeText, DateTime, ForeignKey
2
  from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, Session, joinedload, relationship
3
  from sqlalchemy.sql import func
4
  from contextlib import contextmanager
 
5
  import datetime
6
 
7
 
@@ -34,8 +35,22 @@ class Source(Base):
34
  article_id: Mapped[int] = mapped_column(ForeignKey('article.id'))
35
 
36
 
 
 
 
 
37
  # create an engine
38
- engine = create_engine('sqlite:///news.db', echo=True)
 
 
 
 
 
 
 
 
 
 
39
 
40
  # create tables if needed
41
  Base.metadata.create_all(engine)
 
2
  from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, Session, joinedload, relationship
3
  from sqlalchemy.sql import func
4
  from contextlib import contextmanager
5
+ import os
6
  import datetime
7
 
8
 
 
35
  article_id: Mapped[int] = mapped_column(ForeignKey('article.id'))
36
 
37
 
38
+ # get environment variables for database
39
+ TURSO_DATABASE_URL = os.getenv('TURSO_DATABASE_URL')
40
+ TURSO_AUTH_TOKEN = os.getenv('TURSO_AUTH_TOKEN')
41
+
42
  # create an engine
43
+ if not TURSO_DATABASE_URL or not TURSO_AUTH_TOKEN:
44
+ print('Using local SQLite database')
45
+ engine = create_engine('sqlite:///news.db', echo=True)
46
+ else:
47
+ engine = create_engine(
48
+ f'sqlite+{TURSO_DATABASE_URL}?secure=true',
49
+ connect_args={
50
+ 'auth_token': TURSO_AUTH_TOKEN
51
+ },
52
+ echo=True,
53
+ )
54
 
55
  # create tables if needed
56
  Base.metadata.create_all(engine)
app/main.py CHANGED
@@ -5,6 +5,22 @@ from apscheduler.schedulers.background import BackgroundScheduler
5
  from .database import get_session, retrieve_articles
6
  from .update_news import update_news
7
  import threading
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
 
10
  # for updating the news feed periodically
@@ -17,7 +33,7 @@ is_updating = threading.Lock()
17
  @asynccontextmanager
18
  async def lifespan(app: FastAPI):
19
  # update news periodically
20
- scheduler.add_job(update_news, 'interval', minutes=30)
21
  scheduler.start()
22
  yield
23
  # stop the scheduler when closing the server
 
5
  from .database import get_session, retrieve_articles
6
  from .update_news import update_news
7
  import threading
8
+ import os
9
+
10
+
11
+ UPDATE_INTERVAL_VAR = 'UPDATE_INTERVAL'
12
+
13
+ DEFAULT_UPDATE_INTERVAL = 180 # in minutes
14
+
15
+ # set update interval from environment variable if available
16
+ try:
17
+ update_interval_str = os.getenv(UPDATE_INTERVAL_VAR)
18
+ if update_interval_str is not None:
19
+ update_interval = int(update_interval_str)
20
+ else:
21
+ update_interval = DEFAULT_UPDATE_INTERVAL
22
+ except (ValueError, TypeError):
23
+ update_interval = DEFAULT_UPDATE_INTERVAL
24
 
25
 
26
  # for updating the news feed periodically
 
33
  @asynccontextmanager
34
  async def lifespan(app: FastAPI):
35
  # update news periodically
36
+ scheduler.add_job(update_news, 'interval', minutes=update_interval)
37
  scheduler.start()
38
  yield
39
  # stop the scheduler when closing the server
requirements.txt CHANGED
Binary files a/requirements.txt and b/requirements.txt differ