Modern APIs with FastAPI and Python Transcripts
Chapter: Deploying FastAPI on Linux with gunicorn and nginx
Lecture: Getting the source code from GitHub
0:00 Next up, we need to get our source code on the server. Guess where the source code is? It's right on GitHub.
0:06 In fact, it's right here on this public GitHub repository. So what we're gonna do is just clone it.
0:11 And this is a really good way to get our code over there. You make a change, go into the server, do we git pull, restart the server.
0:18 In fact, over at Talk Python training, we have a certain branch in our GitHub repository called production. And if we push that branch,
0:25 we've got some infrastructure that will automatically look at that, get the new version, install the new requirements, restart,
0:31 you know, take the one of the servers out of a load balancing situation, reconfigure it, start it up, then put it back in. Really,
0:39 really sweet. So all we gotta do is push to GitHub, and that deploys to our Linux server.
0:42 We're not going to go that far because that's fairly involved. So what we're gonna do is just manually do Git pulls if we make any changes.
0:48 But that's a really nice way for us to get the source code on the server and keep it up to date. So we're going to do these two things. Now,
0:55 I'm gonna clone into app repo so we don't have some huge long name. There we go. And if we see over at app repo,
1:03 here's our source code and chapter eight, This is where we're working, okay? So we've got our source code over here,
1:09 and you can see we've got all of our files. Now, one thing I do want to do, we're gonna need to run "pip install requirements"
1:16 to set up our requirements. However, I realize I did skip a step. What we want to do is we actually want to create a virtual environment for our
1:24 web application, okay? Why do we want to do that? Well, when we pip install requirements here, we're changing the server set up.
1:32 And just like locally, we probably don't really want to change the server set up.
1:35 Also, having a virtual environment means if something goes dramatically wrong with your Python
1:40 set up, you could just erase that virtual environment and re-create it. It's not like something that you've got to reconfigure the server for.
1:46 So that's a really big advantage. So what we're gonna do is we're gonna create this virtual environment and go back to our chapter eight. Here we go.
2:00 Now we have our virtual environment. We can pip install -r requirements. And with that done, we should almost, almost be able to run our app.
2:11 So we should be able to say "Python3 main.py", and that will run it, right? But there's a problem. It's a secret,
2:18 do you remember? Our settings.json is not there. Remember we have our settings template like that,
2:25 and it says you're going to need to create a settings.json file with your actual keys. so "nano settings.json", actually,
2:35 let's start from our template. And then all we gotta do is we don't need this. It just says, "put your key here".
2:45 So I'm going to go and put my key right there and save it. Of course, I don't wanna share my key with you, this is where your key goes.
2:53 So I'm gonna pause the recording and then save this, should be able to run. You do that for your key.
3:00 So our virtual environment activated, the requirements installed, and the settings.json there, we should now
3:06 be able to come here and actually run our main. Yes. Look at that. How awesome. So uvicorn is running on our server as we hoped.
3:14 Now, how do I test it? If I test, can't test it here because this window is, you know, it's busy. It's busy running the server,
3:20 so let's go open up another window here, another terminal, and we can curl that URL. Now curl is fine, but there's a really cool library,
3:30 http, httpie, "h t t p i e", that is really much nicer for doing this kind of stuff, but look at that. What have we got? These are copy Talk Python
3:39 training. Here's our weather service, a RESTful weather service, right there. So it looks like our service, our server's running, you can even see it
3:48 process the get over there. So that is fantastic. We're very, very close. Now we need to find a way to make the system run this because you can see
3:56 when I hit CTRL+C or if I would have logged out of my terminal, obviously the server stops. That's not good.
4:01 We just want this to start when that server turns on when you reboot it just comes back. It just lives as part of the server.
4:07 So we need to create a SystemD service or daemon over there so that it will run our Python Web app. But it's basically working, isn't it?