From the course: Build REST APIs with FastAPI

Unlock this course with a free trial

Join today to access over 24,200 courses taught by industry experts.

Solution: Make a server production ready

Solution: Make a server production ready - Python Tutorial

From the course: Build REST APIs with FastAPI

Solution: Make a server production ready

Let's take a look at my solution. So I'm doing a bunch of imports. We'll see a little bit later what's going on. But I start with configuring the logging to the format that I want. And then I didn't change a lot in the utility function, but in the handler, I'm going to do some things. So first, I'm defining what is the maximum size of the file that I want to do. And this is 5MB. And I'm creating a decorator. This decorator is going to wrap a function. And then we'll call performance counter before it starts, return what the function does, and finally, in the finally clause, it is going to calculate the duration and log it as a metric with the function name. And now we have our handler. So I'm wrapping it also in this timed decorator that I'm using. And notice that this one should come first and only then the app.post. The other way goes, your time decorator is not going to get called. Right. So I'm checking that the mime type is text/csv. So it's very important to see that you're…

Contents