Skip to content

Commit a7a408b

Browse files
committed
structure setup
1 parent 2ea93d6 commit a7a408b

9 files changed

Lines changed: 49 additions & 13 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ celerybeat.pid
150150

151151
# Environments
152152
.env
153+
# Note: .env is ignored to avoid committing DB credentials.
153154
.envrc
154155
.venv
155156
env/

app/api/v1/routes/blog.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
from fastapi import APIRouter, Depends, HTTPException, status
2-
2+
from app.schemas.blog import BlogPost
33

44
blog_route = APIRouter(prefix="/blogs", tags=["Blogs"])
55

66

77
# create a blog
88
@blog_route.post("/")
9-
async def create_blog():
10-
pass
9+
async def create_blog(request: BlogPost):
10+
return request
1111

1212

1313
# get all blogs

app/core/config.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
1-
from pydantic_settings import BaseSettings, SettingsConfigDict
1+
from pathlib import Path
2+
import os
3+
from dotenv import load_dotenv
24

3-
class Settings(BaseSettings):
4-
DATABASE_URL: str
5-
SECRET_KEY: str
6-
ALGORITHM: str = "HS256"
7-
ACCESS_TOKEN_EXPIRE_MINUTES: int = 30
5+
BASE_DIR = Path(__file__).resolve().parent.parent.parent
86

9-
model_config = SettingsConfigDict(env_file=".env", env_file_encoding="utf-8")
7+
# Load .env from project root if present
8+
env_path = BASE_DIR / ".env"
9+
if env_path.exists():
10+
load_dotenv(env_path)
11+
12+
class Settings:
13+
# Keep the same attribute names used elsewhere in the project
14+
DATABASE_URL: str = os.getenv("DATABASE_URL", "")
15+
SECRET_KEY: str = os.getenv("SECRET_KEY", "")
16+
ALGORITHM: str = os.getenv("ALGORITHM", "HS256")
17+
ACCESS_TOKEN_EXPIRE_MINUTES: int = int(os.getenv("ACCESS_TOKEN_EXPIRE_MINUTES", "30"))
18+
19+
@property
20+
def sqlalchemy_database_url(self) -> str:
21+
return self.DATABASE_URL
1022

1123
settings = Settings()

app/core/database.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ def get_db():
1414
try:
1515
yield db
1616
finally:
17-
db.close()
17+
db.close()

app/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from fastapi import FastAPI
2-
from .api.v1.routes import user, blog
3-
from .core.database import engine, Base
2+
from app.api.v1.routes import user, blog
3+
from app.core.database import engine, Base
44

55
Base.metadata.create_all(bind=engine)
66

app/models/__init__.py

Whitespace-only changes.

app/models/blog.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from app.core.database import Base
2+
from sqlalchemy import Column, Integer, String, ForeignKey
3+
4+
class Blog(Base):
5+
__tablename__ = "blogs"
6+
7+
id = Column(Integer, primary_key=True, index=True)
8+
title = Column(String, index=True)
9+
content = Column(String)
10+
11+
12+
class User(Base):
13+
__tablename__ = "users"
14+
15+
id = Column(Integer, primary_key=True, index=True)
16+
name = Column(String, index=True)
17+
email = Column(String, unique=True, index=True)
18+
password = Column(String)

app/schemas/__init__.py

Whitespace-only changes.

app/schemas/blog.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from pydantic import BaseModel
2+
3+
class BlogPost(BaseModel):
4+
title: str
5+
content: str

0 commit comments

Comments
 (0)