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.
0:03 Well, guess where the source code is?
0:04 It's right on GitHub.
0:05 In fact, it's right here on this public GitHub repository.
0:08 So what we're gonna do is just clone it.
0:10 And this is a really good way to get our code over there.
0:12 You make a change, go into the server,
0:15 do a "git pull", restart the server.
0:17 In fact, over at Talk Python Training,
0:19 we have a certain branch in our GitHub repository called production.
0:23 And if we push to that branch,
0:24 we've got some infrastructure that will automatically look at that,
0:27 get the new version, install the new requirements, restart,
0:30 you know, take the, one of the servers out of,
0:32 a load balancing situation, reconfigure it, start it up,
0:36 then put it back in. Really,
0:38 really, sweet. So all we've gotta do is push to GitHub
0:40 and that deploys to our Linux server.
0:41 We're not gonna go that far because that's fairly involved.
0:44 So what we're gonna do is just manually do git pulls if we make any changes
0:47 But that's a really nice way for us to get the source code on the
0:50 server and keep it up to date.
0:52 So we're gonna do these two things. Now, I'm gonna clone into app_repo so we
0:56 don't have some huge long name.
0:58 There we go. And if we see over app_repo,
1:02 here's our source code and chapter eight,
1:04 this is where we're working. Okay,
1:06 so we've got our source code over here,
1:08 and you can see we've got all of our files.
1:11 Now, one thing I do wanna do,
1:12 we're gonna need to run pip install requirements
1:15 to set up our requirements. However,
1:17 I realized I did skip a step.
1:19 What we want to do is we actually wanna create a virtual environment for our
1:23 web application, okay? why do we want to do that?
1:26 Well, when we pip install requirements here,
1:28 we're changing the server set up.
1:31 And just like locally, we probably don't really wanna change the server set up.
1:34 Also, having a virtual environment means if something goes dramatically wrong with your Python
1:39 set up, you can just erase that virtual environment and recreate it.
1:42 It's not like something that you've got to reconfigure the server before,
1:45 so that's a really big advantage.
1:47 So what we're gonna do is we're gonna create this virtual environment and go back to
1:56 our chapter eight, here we go.
1:59 Now we have our virtual environment
2:00 we can pip install -r requirements.txt
2:06 And with that done, we should almost, almost be able to run our app.
2:10 So we should be able to say python3 main.py
2:14 and that'll run it, right?
2:16 But there's a problem, it's a secret,
2:17 do you remember? Our settings.json is not there.
2:21 Remember we have our settings template like that,
2:24 and it says you're gonna need to create a settings.json
2:27 file with your actual keys.
2:30 So I'll just nano settings.json. Actually,
2:34 let's start from our template. And then all we gotta do is, we don't need
2:41 this. It just says, put your key here.
2:44 So I'm going to, go and put my key right there and save it. Off course
2:49 I don't wanna share my key with you,
2:51 this is where your key goes.
2:52 So I'm gonna pause the recording and then save this, should be able to run.
2:57 You do that for your key.
2:59 So with our virtual environment activated, the requirements installed and the settings.json there, we should now
3:05 be able to come here and actually run our main.
3:08 Yes, look at that. How awesome.
3:10 So uvicorn is running on our server as we hoped.
3:13 Now, how do I test it? If I test it, I can't test it here
3:16 because this window is, you know,
3:17 it's busy, busy running the server.
3:19 But let's go open up another window here,
3:23 another terminal, and we can curl that url.
3:27 Now curl is fine, but there's a really cool library.
3:29 HTTPie,
3:32 that is really much nicer for doing this kind of stuff,
3:35 but look at that. What do we got?
3:36 Here's our Copyright Talk Python
3:38 Training. Here's our weather service,
3:41 our RESTful weather service, right there.
3:43 So it looks like our service,
3:44 our server's running. You could even see it
3:47 process the GET over there. So that is fantastic.
3:51 We're very, very close. Now
3:52 we need to find a way to make the system run this, cause you can see
3:55 when I hit Ctrl + C or if I would have logged out of my terminal,
3:58 obviously the server stops. That's not good.
4:00 We just want this to start when that server turns on. When you reboot, it just
4:04 comes back. It just lives as part of the server.
4:06 So we need to create a systemd service or daemon over there so that it
4:11 will run our Python web app.
4:12 But it's basically working, isn't it?