Full Web Apps with FastAPI Transcripts
Chapter: Deploying FastAPI on Linux with gunicorn and nginx
Lecture: Getting the source code from GitHub

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Next up, we need to get our source code on the server. Well, 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 a "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 to 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've gotta do is push to GitHub and that deploys to our Linux server.
0:42 We're not gonna 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.
0:53 So we're gonna do these two things. Now, I'm gonna clone into app_repo so we don't have some huge long name. There we go. And if we see over 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 wanna do, we're gonna need to run pip install requirements
1:16 to set up our requirements. However, I realized I did skip a step. What we want to do is we actually wanna 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 wanna 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 can just erase that virtual environment and recreate it. It's not like something that you've got to reconfigure the server before,
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.txt 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'll 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 gonna need to create a settings.json file with your actual keys. So I'll just 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. Off 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 with 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 it, I can't test it here because this window is, you know, it's busy, busy running the server.
3:20 But 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. HTTPie,
3:33 that is really much nicer for doing this kind of stuff, but look at that. What do we got? Here's our Copyright Talk Python
3:39 Training. Here's our weather service, our RESTful weather service, right there. So it looks like our service,
3:45 our server's running. You could even see it process the GET over there. So that is fantastic. We're very, very close. Now
3:53 we need to find a way to make the system run this, cause you can see when I hit Ctrl + C or if I would have logged out of my terminal,
3:59 obviously the server stops. That's not good. We just want this to start when that server turns on. When you reboot, it just
4:05 comes back. It just lives as part of the server. So we need to create a systemd service or daemon over there so that it will run our Python web app.
4:13 But it's basically working, isn't it?

Talk Python's Mastodon Michael Kennedy's Mastodon