The video owner has disabled playback on external websites.
This video is no longer available on YouTube.
This video cannot be played right now.
Watch on YouTube
How to Tell If Your Code Is Actually Production-Ready
자막 (571 세그먼트)
Here's an example of a simple fast API
app that has a single endpoint convert
and that converts from one currency to
another. This is just some hard-coded
rate here. Uh it gets raised from that
dictionary and then computes the
conversion. And this actually works. I
can send a call request to this API and
I get the results that I want. But is
this production ready? Well, not really.
So, I'm going to take this service that
just works and upgrade it step by step
into something you can actually deploy
and rely on in production. And along the
way, I'm going to talk about things like
validation, error handling, monitoring,
deployment, and other things that are
important when you want production ready
code. So, let's dive in. Before I start
working on the code, what does
production ready actually mean? I mean,
there's no fixed definition of what that
is. It can mean different things for
different types of applications. It
depends on what is more important.
Perhaps security is a key issue for you
because it's a public API or something.
Or perhaps you are thinking more about
scalability and you expect this to be
used by millions of people. Or perhaps
you really want this to be robust and
make sure that it absolutely never
crashes. In any case, production ready
doesn't mean it works on my machine. And
to me typically production ready code
means that it works reliably in
realworld conditions whatever those
conditions are. So it means that at the
core it needs to be observable. It needs
to be reasonably secure. It needs to be
maintainable right you need to be able
to work on it and reasonably resilient.
Now I start with this very naive uh fast
API route but uh it only supports
hard-coded uh conversion. There's no
validation whatsoever. So, let's improve
this step by step. Now, before I start,
if you want to learn more about how to
design a piece of software from scratch,
I have a free guide for you. You can get
this at iron.code/design
guide. This contains the seven steps I
take when I design new software and
hopefully it helps you avoid some of the
mistakes that I made in the past.
Iron/design guide. The link is also in
the description of this video. Now, the
first step that we're going to take is
looking at the data that we actually
expect. So even before you think about
validation, you have to make sure that
the data that your app works with is
actually appropriate. And in this case,
we're passing a from and two currencies
as a string, which well makes sense. But
as you can see, the amount is in float.
And floats are actually kind of
dangerous for uh currency handling
because they're not very precise. So
it's better to use something like the
decimal type for amounts and rates. So
what we're going to do as a first step
is to fix that.
So, I'm going to import the decimal
type. And that means we're going to need
to make some changes here and here as
well. So, first I'm going to turn these
conversion rates into decimal values
like so.
Simply going to copy paste this. Now,
normally I would use AI to edit this
stuff for me, but I'm not doing that on
purpose so it's a bit easier for you to
follow in the video. So now my rates are
in decimal which is great. I can also
turn my amount in decimal as well. And
now let's see what happens when we do
this conversion. So this still works in
exactly the same way except now our
types are much safer. So like I said
that's something you need to do before
you even think about uh validation. Make
sure that your types are appropriate. So
next step is validating the input. So we
don't want to be able to send like weird
stuff to our API. So you need to add
checks for that. And actually fast API
which is the library that I'm using here
has support for doing this via paidantic
which makes this really easy. In order
to do that I'm going to import the query
type from fast API and then we can use
that to put some guard lines on these
types. For example, from currency is a
string. But we can make sure
that this string has let's say a minimum
length of three.
And we can also give it a maximum length
of three. So we are always sure that
currencies have an exact length of
three. And we can do the same thing for
the two currency like so. And for the
decimal amounts we can also supply a
query
where let's say we want to have
전체 자막은 비디오 플레이어에서 이용 가능
댓글 (0)
로그인하여 댓글 달기인터랙티브 모드
퀴즈
정답:
퀴즈
정답:
영상을 보면서 퀴즈가 나타납니다
암기 팁
이 영상에서
무료로 언어 학습