<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: ADITYA KESHARI</title>
    <description>The latest articles on DEV Community by ADITYA KESHARI (@aditya_keshari_1172178be6).</description>
    <link>https://dev.to/aditya_keshari_1172178be6</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2046607%2F426dc415-7e00-4401-befd-04418f2f7728.jpg</url>
      <title>DEV Community: ADITYA KESHARI</title>
      <link>https://dev.to/aditya_keshari_1172178be6</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/aditya_keshari_1172178be6"/>
    <language>en</language>
    <item>
      <title>Secure FastAPI WebSocket: Fixing Dependency Injection Errors</title>
      <dc:creator>ADITYA KESHARI</dc:creator>
      <pubDate>Fri, 13 Sep 2024 07:37:36 +0000</pubDate>
      <link>https://dev.to/aditya_keshari_1172178be6/secure-fastapi-websocket-fixing-dependency-injection-errors-1jno</link>
      <guid>https://dev.to/aditya_keshari_1172178be6/secure-fastapi-websocket-fixing-dependency-injection-errors-1jno</guid>
      <description>&lt;p&gt;Hey there!&lt;/p&gt;

&lt;p&gt;So, you’re trying to secure your WebSocket, and these dependency injection errors pop up. Annoying, right? Don’t sweat it — I’ve got a quick and easy solution that’ll sort you out.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Problem: Dependency Injection Errors&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You’re all excited about securing your WebSocket, but boom! Dependency injection errors show up.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fypnphofbn8czb5g56q0y.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fypnphofbn8czb5g56q0y.jpg" alt="Image description" width="800" height="435"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But here’s a straightforward fix.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Solution: JWT in the Request Header&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here’s the trick: use a JSON Web Token (JWT). Pop that token into the request header, and you’re golden. It lets you do some cool stuff — like figuring out who the current user is right there in your WebSocket route. Simple and effective.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxltw3npsk0p7bepctz12.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxltw3npsk0p7bepctz12.jpg" alt="Image description" width="800" height="249"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;No need for fancy jargon. Check out this quick code snippet:&lt;/p&gt;

&lt;p&gt;`@router.websocket("/create")&lt;br&gt;
async def create_room(websocket: WebSocket, db: Session = Depends(get_db)):&lt;br&gt;
    request_header_dict = dict(websocket.headers)&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# check if access_token is in the header
if('access_token' not in request_header_dict.keys()):
    ic("No access token")
    return HTTPException(status_code=status.HTTP_401_UNAUTHORIZED)

# else get access token
access_token = request_header_dict['access_token']

current_user = oauth2.get_current_user(access_token)

# websocket route logic ##
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h1&gt;
  
  
  oauth2/py
&lt;/h1&gt;

&lt;p&gt;def verify_access_token(token: str, credentials_exception):&lt;br&gt;
    ic("verify_access_token")&lt;br&gt;
    try:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
    id: str = payload.get("user_id")

    if id is None:
        raise credentials_exception
    # token_data = schemas.TokenData(id=id)
except JWTError:
    ic("Error occured")
    raise credentials_exception

# return token_data
return id
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;def get_current_user(token: str):&lt;br&gt;
    credentials_exception = HTTPException(status_code=status.HTTP_401_UNAUTHORIZED,&lt;br&gt;
                                          detail=f"Could not validate credentials", headers={"WWW-Authenticate": "Bearer"})&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;db = SessionLocal()
user_id = verify_access_token(token, credentials_exception)  
user = db.query(models.User).filter(models.User.id == user_id).first()  
db.close()
return user`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;`# database.py&lt;br&gt;
from sqlalchemy import create_engine&lt;br&gt;
from sqlalchemy.ext.declarative import declarative_base&lt;br&gt;
from sqlalchemy.orm import sessionmaker&lt;/p&gt;

&lt;p&gt;SQLALCHEMY_DATABASE_URL = 'postgresql+psycopg://:@/'&lt;/p&gt;

&lt;p&gt;engine = create_engine(SQLALCHEMY_DATABASE_URL)&lt;/p&gt;

&lt;p&gt;SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)&lt;/p&gt;

&lt;p&gt;Base = declarative_base()&lt;/p&gt;

&lt;p&gt;def get_db():&lt;br&gt;
    db = SessionLocal()&lt;br&gt;
    try:&lt;br&gt;
        yield db&lt;br&gt;
    finally:&lt;br&gt;
        db.close()`&lt;/p&gt;

&lt;p&gt;It’s not rocket science; it’s just a quick solution.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftgc6j7mgp2dnocz5heo3.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftgc6j7mgp2dnocz5heo3.jpg" alt="Image description" width="800" height="364"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu3g8ycfdf8dpkhgj1jwp.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu3g8ycfdf8dpkhgj1jwp.jpg" alt="Image description" width="800" height="249"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzkme7gs0maftbzrykiad.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzkme7gs0maftbzrykiad.jpg" alt="Image description" width="800" height="373"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Just to prove it works, we’ve got screenshots from Postman.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Dive Deeper: ChatRoom Project&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you want the full scoop, head over to my “chatRoom” project on &lt;a href="https://github.com/aditya292002/ChatRoom-FastAPI" rel="noopener noreferrer"&gt;Github&lt;/a&gt;. You’ll find everything there — no secrets, just a straightforward guide and the whole deal.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Big Thanks&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Thanks for hanging in there! Your time matters, and we appreciate you giving this a read. Keep it simple, keep it secure.&lt;/p&gt;

&lt;p&gt;Cheers,&lt;br&gt;
Aditya Keshari&lt;/p&gt;

</description>
      <category>python</category>
      <category>fastapi</category>
      <category>websocket</category>
    </item>
    <item>
      <title>Simplest Way to Set Up the Drogon C++ Framework</title>
      <dc:creator>ADITYA KESHARI</dc:creator>
      <pubDate>Mon, 09 Sep 2024 10:34:38 +0000</pubDate>
      <link>https://dev.to/aditya_keshari_1172178be6/simplest-way-to-set-up-the-drogon-c-framework-1jei</link>
      <guid>https://dev.to/aditya_keshari_1172178be6/simplest-way-to-set-up-the-drogon-c-framework-1jei</guid>
      <description>&lt;p&gt;Recently, I tried this C++ web framework, which claims to be very fast. They mention, “Drogon is multi-threaded. On a single core of a Ryzen 3700X, Drogon can process more than 150K HTTP requests per second,” which I find incredible.&lt;/p&gt;

&lt;p&gt;You can visit the official Drogon documentation and follow the installation steps here: &lt;a href="https://drogonframework.github.io/drogon-docs/#/ENG/ENG-02-Installation" rel="noopener noreferrer"&gt;Drogon Installation Guide.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;However, if you want to avoid the hassle of installing numerous packages and libraries, follow along. The only prerequisite is that Docker must be installed on your machine.&lt;/p&gt;

&lt;p&gt;Here’s a quick breakdown of how you can set up a Drogon environment and start working with it.&lt;/p&gt;

&lt;p&gt;In this blog, I’ll show you how to set up Drogon using Docker, and don’t worry — this setup will provide a seamless development experience. So, let’s get started!Part 1&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Part 1&lt;/strong&gt;&lt;br&gt;
Step1: Pull the Drogon image from docker hub.&lt;br&gt;
&lt;code&gt;docker pull drogonframework/drogon&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Step2: Start the container and run drogon_ctl&lt;br&gt;
&lt;code&gt;docker run -it --name drogon_dev drogonframework/drogon /bin/bash&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Step3: Inside the container, create a Drogon project.&lt;br&gt;
&lt;code&gt;drogon_ctl create project test_project&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Step4: Copy the project files from the container to the host.&lt;br&gt;
In another terminal, copy the files from the container to your local machine (outside the container).&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker cp drogon_dev:/path/to/test_project /path/to/local/folder&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Step5: Stop the container: Once you’ve copied the files, stop the container for the next step:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker stop drogon_dev&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Part 2 — Set Up a Docker Volume for Auto-sync&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now that you have the project outside the container, you can mount the directory on the host to the container. This way, any changes you make outside the container will be reflected inside it.&lt;/p&gt;

&lt;p&gt;Step1: Run the container with a volume mount: Mount the local project directory into the container:&lt;br&gt;
&lt;code&gt;docker run -it --name drogon_dev \&lt;br&gt;
-v /path/to/local/folder/test_project:/path/in/container/test_project \&lt;br&gt;
drogonframework/drogon /bin/bash&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Replace /path/to/local/folder/test_project with the actual path where you copied the project files, and /path/in/container/test_project with the container path (e.g., /root/test_project or any appropriate path inside the container).&lt;/p&gt;

&lt;p&gt;Step2: Work inside the container: Inside the container, you can now build or run the Drogon project:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;cd /path/in/container/test_project&lt;br&gt;
mkdir build&lt;br&gt;
cd build&lt;br&gt;
cmake ..&lt;br&gt;
make&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Auto-reflection of Changes&lt;/strong&gt;&lt;br&gt;
Since the project directory is mounted as a volume, any changes you make in the project folder outside the container (on your host system) will be reflected inside the container instantly.&lt;/p&gt;

&lt;p&gt;This setup allows you to use your preferred editor outside the container while the build and runtime environment remains inside Docker.&lt;/p&gt;

&lt;p&gt;Let me know if you need help with any specific step!&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>drogon</category>
      <category>docker</category>
    </item>
  </channel>
</rss>
