<ol dir="auto">
<li>How to convert from given Flask to FastAPI:</li>
<li>Will I get better performance doing so in this Kakfa context?</li>
</ol>
<p dir="auto">producer.py:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="from flask import Flask, request, jsonify
from kafka import KafkaProducer
import json

app = Flask(__name__)
producer = KafkaProducer(bootstrap_servers='kafka:9092')
topic_name = 'messages'

@app.route('/', methods=[&quot;POST&quot;])
def send():
    app.logger.info(&quot;received message&quot;)
    content = request.get_json()
    jd = json.dumps(content).encode('utf-8')
    producer.send(topic_name, jd)
    app.logger.info(&quot;sent message&quot;)
    return jsonify({&quot;status&quot;:&quot;ok&quot;})

if __name__ == '__main__':
  app.logger.info('Starting server...')
  app.run(host='0.0.0.0', port=8080)"><pre class="notranslate"><code class="notranslate">from flask import Flask, request, jsonify
from kafka import KafkaProducer
import json

app = Flask(__name__)
producer = KafkaProducer(bootstrap_servers='kafka:9092')
topic_name = 'messages'

@app.route('/', methods=["POST"])
def send():
    app.logger.info("received message")
    content = request.get_json()
    jd = json.dumps(content).encode('utf-8')
    producer.send(topic_name, jd)
    app.logger.info("sent message")
    return jsonify({"status":"ok"})

if __name__ == '__main__':
  app.logger.info('Starting server...')
  app.run(host='0.0.0.0', port=8080)
</code></pre></div>
<p dir="auto">consumer.py:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="from kafka import KafkaConsumer

topic_name = 'messages'

if __name__ == '__main__':
  print('Starting consumer...')
  consumer = KafkaConsumer(topic_name, bootstrap_servers='kafka:9092')
  
  print('receiving messages...')
  for message in consumer:
      print(f'received message {message}')

  print('exiting...')"><pre class="notranslate"><code class="notranslate">from kafka import KafkaConsumer

topic_name = 'messages'

if __name__ == '__main__':
  print('Starting consumer...')
  consumer = KafkaConsumer(topic_name, bootstrap_servers='kafka:9092')
  
  print('receiving messages...')
  for message in consumer:
      print(f'received message {message}')

  print('exiting...')
</code></pre></div>
<p dir="auto">producer/okteto.yml:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="name: producer
labels:
  stack.okteto.com/service: producer
command: bash
environment:
- FLASK_DEBUG=1
volumes:
- /root/.cache/pip
sync:
- .:/usr/src/app
forward:
- 8080:8080
reverse:
- 9000:9000
resources:
  limits:
    cpu: &quot;1&quot;
    memory: 2Gi"><pre class="notranslate"><code class="notranslate">name: producer
labels:
  stack.okteto.com/service: producer
command: bash
environment:
- FLASK_DEBUG=1
volumes:
- /root/.cache/pip
sync:
- .:/usr/src/app
forward:
- 8080:8080
reverse:
- 9000:9000
resources:
  limits:
    cpu: "1"
    memory: 2Gi
</code></pre></div>
<p dir="auto">producer/Dockerfile:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="FROM okteto/python:3
ENV PYTHONUNBUFFERED=1
WORKDIR /usr/src/app
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
COPY app.py app.py
EXPOSE 8080
CMD [&quot;python&quot;, &quot;app.py&quot; ]
"><pre class="notranslate"><code class="notranslate">FROM okteto/python:3
ENV PYTHONUNBUFFERED=1
WORKDIR /usr/src/app
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
COPY app.py app.py
EXPOSE 8080
CMD ["python", "app.py" ]

</code></pre></div>
<p dir="auto">consumer/okteto.yml:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="name: consumer
labels:
  stack.okteto.com/service: consumer
command: bash
volumes:
- /root/.cache/pip
sync:
- .:/usr/src/app
reverse:
- 9000:9000
resources:
  limits:
    cpu: &quot;1&quot;
    memory: 2Gi"><pre class="notranslate"><code class="notranslate">name: consumer
labels:
  stack.okteto.com/service: consumer
command: bash
volumes:
- /root/.cache/pip
sync:
- .:/usr/src/app
reverse:
- 9000:9000
resources:
  limits:
    cpu: "1"
    memory: 2Gi
</code></pre></div>
<p dir="auto">consumer/Dockerfile:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="FROM okteto/python:3
ENV PYTHONUNBUFFERED=1
WORKDIR /usr/src/app
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
COPY app.py app.py
EXPOSE 8080
CMD [&quot;python&quot;, &quot;app.py&quot; ]"><pre class="notranslate"><code class="notranslate">FROM okteto/python:3
ENV PYTHONUNBUFFERED=1
WORKDIR /usr/src/app
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
COPY app.py app.py
EXPOSE 8080
CMD ["python", "app.py" ]
</code></pre></div>
<p dir="auto">requirements.txt:</p>
<p dir="auto">kafka-python<br>
Flask==1.1.1</p>
<p dir="auto">After upgrading from 0.55.1 to 0.60.1 all of my endpoints containing an optional trailing slash in the path regex (like "/?") now result in 404 Not Found.</p>
<p dir="auto">Example to reproduce in 0.60.1:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="from fastapi import FastAPI

app = FastAPI()

@app.get(&quot;/items/{item_id}/?&quot;)
async def read_item(item_id):
    return {&quot;item_id&quot;: item_id}"><pre class="notranslate"><span class="pl-k">from</span> <span class="pl-s1">fastapi</span> <span class="pl-k">import</span> <span class="pl-v">FastAPI</span>

<span class="pl-s1">app</span> <span class="pl-c1">=</span> <span class="pl-v">FastAPI</span>()

<span class="pl-en">@<span class="pl-s1">app</span>.<span class="pl-en">get</span>(<span class="pl-s">"/items/{item_id}/?"</span>)</span>
<span class="pl-k">async</span> <span class="pl-k">def</span> <span class="pl-en">read_item</span>(<span class="pl-s1">item_id</span>):
    <span class="pl-k">return</span> {<span class="pl-s">"item_id"</span>: <span class="pl-s1">item_id</span>}</pre></div>
<h3 dir="auto">Description</h3>
<p dir="auto">In 0.55.1 the above code works fine, allowing the optional trailing slash as expected. In 0.60.1 this behavior is now resulting in 404 Not Found with or without the trailing slash.</p>
<p dir="auto">Reproduce By:</p>
<ul dir="auto">
<li>Open the browser and call the endpoint <code class="notranslate">/items/1/</code> or <code class="notranslate">/items/1</code>.</li>
<li>It returns 404 Not Found but expected is {"item_id": 1}</li>
</ul>
<h3 dir="auto">Environment</h3>
<ul dir="auto">
<li>OS: Reproduced on Windows 10 and Ubuntu</li>
<li>FastAPI Version: 0.60.1  (upgraded from 0.55.1)</li>
<li>Python version: 3.6</li>
</ul>