WEBVTT 00:01.230 --> 00:06.840 So last time we got the cargo file in the card package, we got at least initially set up, there's 00:06.840 --> 00:08.610 more work to do, but we have a good start here. 00:09.090 --> 00:14.700 And as you can see on the screen in front of you, I've posted to the source code for this lecture an 00:14.700 --> 00:21.900 updated version of cargo that has all of the necessary or at least most of the necessary cases for a 00:21.900 --> 00:22.890 stripe area code. 00:22.900 --> 00:24.750 And you can just copy and paste that. 00:24.750 --> 00:27.450 It would be pointless for you to watch me type those all in. 00:27.450 --> 00:28.950 So I thought I'd do it between lectures. 00:29.610 --> 00:35.580 And this time around, we want to start and we want to get started on getting our backend API up and 00:35.580 --> 00:35.790 running. 00:36.450 --> 00:40.380 So if you go to the API folder, you'll see that there's nothing in there. 00:40.390 --> 00:45.090 So what we need to do, first of all, is create a new file and I'm going to call it API Go. 00:45.450 --> 00:49.440 And this will be the main entry point for API package. 00:49.650 --> 00:53.370 Now, this is going to be packaged package and it's also going to have a function. 00:54.810 --> 01:00.000 This will be unusual to you if you've only ever had a code base with one main function. 01:00.360 --> 01:03.900 But what we're going to do is build two binaries from the same kopeks. 01:04.290 --> 01:08.810 And when we compile what's in CMD Web, that's our front end. 01:08.820 --> 01:12.960 But what we compile what's in CMD API, that will be our backend. 01:13.590 --> 01:20.220 So just as we had to have a configuration and application types for our main Dongo, let's open that 01:20.220 --> 01:21.050 up and have a look at it. 01:22.500 --> 01:26.290 We're going to have the same sort of thing in our API Dutko. 01:26.340 --> 01:31.260 So I'm going to copy all of this and we'll just modify it as necessary just to save some typing so that 01:31.320 --> 01:32.330 an API dutko. 01:32.340 --> 01:36.120 I just got that out of the main Dongo file right here. 01:36.540 --> 01:40.460 Want to paste that right in here and we'll modify it as necessary. 01:40.620 --> 01:45.870 OK, so there's a missing template, import and log import, so let's see if it'll do it for me. 01:45.930 --> 01:55.390 Log or did that one and template which I better spell right template dot now. 01:55.560 --> 01:59.550 It's not going to do that with template that we are html template. 01:59.850 --> 02:05.280 OK, so we have those two reports in there, so we have our type application and our type config. 02:05.490 --> 02:07.560 And what do we have for a warning here. 02:07.570 --> 02:08.000 It's on you. 02:08.140 --> 02:09.630 I know that will be used shortly. 02:10.230 --> 02:14.940 And we're also going to have in our main function reading from the flags to copy that stuff as well. 02:15.180 --> 02:16.410 It'll just save some time. 02:17.280 --> 02:27.870 So I'll copy this and copy these and paste them right into my main function in API Dongo right here, 02:29.430 --> 02:29.830 OK. 02:29.910 --> 02:31.400 And now it wants to flag package. 02:31.410 --> 02:41.640 Let's get that important PFLAG goods imported that and we want the OS package Ostara and that should 02:41.640 --> 02:44.360 do it for us OS and now it's important. 02:44.400 --> 02:44.740 Good. 02:45.780 --> 02:50.520 Now obviously we can't have the front end in the back end, both running on port for thousands or changes 02:50.520 --> 02:51.420 to four thousand one. 02:52.230 --> 02:53.270 I need that for sure. 02:53.700 --> 02:55.930 Environment is fine development and production. 02:55.950 --> 03:00.420 So we also are going to put another one here because we'll get to that eventually maintenance. 03:01.680 --> 03:07.590 We can put our backend into maintenance mode and we don't need the API so we can delete that line entirely 03:09.150 --> 03:10.640 because this is the API. 03:12.480 --> 03:14.310 So now we have our necessary flags. 03:14.430 --> 03:20.340 We have our stripe secret and stripe key and we may as well set up our logger's. 03:20.340 --> 03:27.930 But again, I can go back to not go copy the logger's because there's no difference in this at all. 03:30.030 --> 03:30.870 Back to API. 03:30.870 --> 03:33.180 Don't go, OK? 03:33.870 --> 03:36.320 And we'll stick those right in the. 03:37.470 --> 03:38.860 So let's create the application for. 03:41.600 --> 03:45.530 NAP will be of type A reference to application. 03:47.760 --> 03:55.770 And in that, I want my confect, which is capture, and I also want to get forlong, which is the info 03:55.770 --> 04:01.740 log and I want the airlock, which is the airlock. 04:04.400 --> 04:11.960 Good, and that should be sufficient for that now, just as I did in the last case, I'll go copy that 04:11.960 --> 04:14.330 as well and modify it as necessary. 04:15.140 --> 04:16.700 We'll copy our search function. 04:19.270 --> 04:23.290 Now, this looks like code duplication, and you might be asking yourself, why didn't you just declare 04:23.290 --> 04:25.390 the types in another package or something? 04:25.820 --> 04:30.280 Well, that's because over time, the configuration for the API and the configuration for the front 04:30.280 --> 04:32.130 end might be markedly different. 04:32.530 --> 04:35.570 And I just like to keep things as clean as I possibly can. 04:36.190 --> 04:42.250 So back here, let's just paste in that search function and we'll need to import HTP servers. 04:42.250 --> 04:45.280 So HTP like that. 04:46.420 --> 04:50.170 That's that one format package will import that. 04:51.520 --> 04:53.590 And time will import that. 04:55.570 --> 04:58.240 So that fixes all of those will have to take care of our RUTINA minute. 04:58.780 --> 05:00.960 And we're not starting the server. 05:01.000 --> 05:05.840 We're starting the back end server and everything else should stay the same. 05:06.170 --> 05:07.720 And of course, we have to call it. 05:10.780 --> 05:24.850 So error equals Abdulnasser, if error is not equal to nil, we'll just say log fatele with error and 05:24.850 --> 05:26.080 this needs to be an assignment. 05:26.080 --> 05:27.700 Very operator. 05:28.760 --> 05:36.090 OK, so there, with the exception of routes file is our initial setup for this application. 05:36.100 --> 05:44.800 So let's create a file in the API folder or create a new file and I will call it routes, routes, dash 05:44.800 --> 05:54.370 API dot com and it has the package of Main and it has a function with the receiver, which is a pointer 05:54.370 --> 06:01.270 to application and it's called routes and it returns and HTP Handler. 06:03.520 --> 06:07.930 And again, we'll use our mux router, our key router and declare variable marks. 06:07.930 --> 06:10.870 And that's a sign the value of Cheetah New River. 06:14.760 --> 06:15.520 And we can return. 06:18.030 --> 06:23.310 So we'll need to set up some actual routes to handle things, but one thing we're absolutely going to 06:23.310 --> 06:31.260 have to have is a way of dealing with calls so that we can allow requests only from those sites that 06:31.260 --> 06:31.680 we want. 06:31.770 --> 06:37.430 And when development, it doesn't matter what it is, but we can actually take advantage of a package, 06:37.440 --> 06:40.320 an extension to the key router that makes that much easier. 06:41.070 --> 06:42.630 And the package should remain here. 06:43.050 --> 06:43.740 There was an air. 06:45.640 --> 06:46.190 That looks better. 06:46.600 --> 06:52.510 So if you go back and look at the CHY homepage that we got our actual router package from, you'll notice 06:52.510 --> 06:56.080 that there's something called Go Dash Cheese course. 06:56.080 --> 06:58.170 And I'm going to go get that package right now. 06:58.180 --> 07:00.030 So go get that. 07:00.040 --> 07:00.760 I'll paste in what? 07:00.760 --> 07:07.540 I just copied it from another screen and that will give me a really easy middleware package that I can 07:07.540 --> 07:12.910 use to determine who has the rights to access my API. 07:13.150 --> 07:17.110 OK, so it's been added now I can use it and you can go look at the documentation yourself. 07:17.110 --> 07:19.720 I'll post a link to that, to the course resources for this lecture. 07:20.050 --> 07:24.470 But it's as simple as mux don't use and we're going to use the course package. 07:24.490 --> 07:26.980 ACARS dog handler. 07:29.370 --> 07:34.840 And that's going to take a course options and we'll just specify the things that we want to be in there. 07:35.310 --> 07:37.890 So the first thing I want is what are my allowed origins? 07:38.280 --> 07:41.460 And I want to allow and has to be a slice of strings. 07:42.750 --> 07:49.860 I want to allow HTP, Collins, Sturr, anybody which is really wide open, but it's sufficient for 07:49.860 --> 07:54.020 development purposes and HGP, Collins, anybody. 07:56.070 --> 07:57.590 So that's my allowed origins. 07:57.600 --> 08:00.690 Now, what options are we going to allow allowed method's? 08:02.730 --> 08:04.440 What kind of requests are we going to take? 08:04.710 --> 08:11.250 Well, again, it's a slice of string and we're going to allow get. 08:14.010 --> 08:25.920 Post put delete and options, OK, we're allowing everything basically this is wide open and allowed 08:25.920 --> 08:27.240 headers, not allowed headers. 08:27.240 --> 08:31.410 Are we going to have well, slice of string, as you probably guessed. 08:32.910 --> 08:40.440 And we'll allow except you have to have authorization because at some point we might have to allow people 08:40.440 --> 08:47.130 to authenticate using, say, JSON Web tokens, content, dash type, which you're going to want to 08:47.130 --> 08:50.160 have, and eventually we're going to add extra protection. 08:50.160 --> 08:53.260 So X X RF dash token. 08:53.580 --> 08:55.770 I'm not going to get to that for a while, but we will get to it. 08:56.820 --> 09:01.350 So those are my allowed headers and will allow credentials. 09:01.410 --> 09:06.210 We're going to set false for that and if necessary, will change later. 09:06.210 --> 09:10.140 And Max Age will set that to say three hundred five minutes. 09:10.440 --> 09:11.270 That should be sufficient. 09:12.030 --> 09:17.340 So now we have middleware, one piece of middleware that will set the appropriate course information 09:17.730 --> 09:21.510 and restrict who can and cannot access our backend. 09:21.690 --> 09:26.850 So we have our middleware and we have our roots, but we actually don't have a root or a handler yet. 09:26.850 --> 09:31.700 So we'll take care of setting up our handler to get a payment intent in the next election.