-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
73 lines (59 loc) · 2.02 KB
/
main.py
File metadata and controls
73 lines (59 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from fastapi import FastAPI
from user.routes import router as mypage_router
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI()
app.include_router(mypage_router.router, tags=['USER'])
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
if __name__ == "__main__":
from user.database.databases import init_db as make_user_db
make_user_db()
import uvicorn
uvicorn.run("main:app", host='0.0.0.0', port=2952, reload=True)
# from fastapi import FastAPI, HTTPException, Depends
# from sqlalchemy import create_engine, Column, Integer, String
# from sqlalchemy.orm import sessionmaker, declarative_base, Session
# from pydantic import BaseModel
# # FastAPI 인스턴스 생성
# app = FastAPI()
# # 데이터베이스 연결 URL 설정
# DATABASE_URL = "mysql+pymysql://username:password@localhost:3306/mydatabase"
# # SQLAlchemy 엔진 및 세션 생성
# engine = create_engine(DATABASE_URL)
# SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
# # 베이스 클래스 생성
# Base = declarative_base()
# # 데이터베이스 모델 정의
# class User(Base):
# __tablename__ = "users"
# id = Column(Integer, primary_key=True, index=True)
# name = Column(String(100))
# email = Column(String(100))
# age = Column(Integer)
# # Pydantic 모델 정의
# class UserResponse(BaseModel):
# id: int
# name: str
# email: str
# age: int
# class Config:
# orm_mode = True
# # 의존성 주입을 위한 DB 세션 함수
# def get_db():
# db = SessionLocal()
# try:
# yield db
# finally:
# db.close()
# # 특정 ID에 해당하는 유저 정보 가져오기
# @app.get("/users/{user_id}", response_model=UserResponse)
# def read_user(user_id: int, db: Session = Depends(get_db)):
# user = db.query(User).filter(User.id == user_id).first()
# if user is None:
# raise HTTPException(status_code=404, detail="User not found")
# return user