Modern APIs with FastAPI and Python 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 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 we 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 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 gotta do is push to GitHub,
0:40 and that deploys to our Linux server.
0:41 We're not going to 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 going to do these two things. Now,
0:54 I'm gonna clone into app repo so we don't have some huge long name.
0:58 There we go. And if we see over at 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 want to do,
1:12 we're gonna need to run "pip install requirements"
1:15 to set up our requirements. However,
1:17 I realize I did skip a step.
1:19 What we want to do is we actually want to 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 want to 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 could just erase that virtual environment and re-create it. It's not like something
1:44 that you've got to reconfigure the server for.
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.
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 will 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 going to need to create a settings.json
2:27 file with your actual keys.
2:30 so "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. Of 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 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, can't test it here
3:16 because this window is, you know,
3:17 it's busy. It's busy running the server,
3:19 so let's go open up another window here,
3:23 another terminal, and we can curl that URL. Now curl is
3:27 fine, but there's a really cool library,
3:29 http, httpie, "h t t p i e",
3:32 that is really much nicer for doing this kind of stuff,
3:35 but look at that. What have we got?
3:36 These are copy Talk Python
3:38 training. Here's our weather service,
3:41 a RESTful weather service, right there.
3:43 So it looks like our service,
3:44 our server's running, you can 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 because 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?