So last time we got the cargo file in the card package, we got at least initially set up, there's
more work to do, but we have a good start here.
And as you can see on the screen in front of you, I've posted to the source code for this lecture an
updated version of cargo that has all of the necessary or at least most of the necessary cases for a
stripe area code.
And you can just copy and paste that.
It would be pointless for you to watch me type those all in.
So I thought I'd do it between lectures.
And this time around, we want to start and we want to get started on getting our backend API up and
running.
So if you go to the API folder, you'll see that there's nothing in there.
So what we need to do, first of all, is create a new file and I'm going to call it API Go.
And this will be the main entry point for API package.
Now, this is going to be packaged package and it's also going to have a function.
This will be unusual to you if you've only ever had a code base with one main function.
But what we're going to do is build two binaries from the same kopeks.
And when we compile what's in CMD Web, that's our front end.
But what we compile what's in CMD API, that will be our backend.
So just as we had to have a configuration and application types for our main Dongo, let's open that
up and have a look at it.
We're going to have the same sort of thing in our API Dutko.
So I'm going to copy all of this and we'll just modify it as necessary just to save some typing so that
an API dutko.
I just got that out of the main Dongo file right here.
Want to paste that right in here and we'll modify it as necessary.
OK, so there's a missing template, import and log import, so let's see if it'll do it for me.
Log or did that one and template which I better spell right template dot now.
It's not going to do that with template that we are html template.
OK, so we have those two reports in there, so we have our type application and our type config.
And what do we have for a warning here.
It's on you.
I know that will be used shortly.
And we're also going to have in our main function reading from the flags to copy that stuff as well.
It'll just save some time.
So I'll copy this and copy these and paste them right into my main function in API Dongo right here,
OK.
And now it wants to flag package.
Let's get that important PFLAG goods imported that and we want the OS package Ostara and that should
do it for us OS and now it's important.
Good.
Now obviously we can't have the front end in the back end, both running on port for thousands or changes
to four thousand one.
I need that for sure.
Environment is fine development and production.
So we also are going to put another one here because we'll get to that eventually maintenance.
We can put our backend into maintenance mode and we don't need the API so we can delete that line entirely
because this is the API.
So now we have our necessary flags.
We have our stripe secret and stripe key and we may as well set up our logger's.
But again, I can go back to not go copy the logger's because there's no difference in this at all.
Back to API.
Don't go, OK?
And we'll stick those right in the.
So let's create the application for.
NAP will be of type A reference to application.
And in that, I want my confect, which is capture, and I also want to get forlong, which is the info
log and I want the airlock, which is the airlock.
Good, and that should be sufficient for that now, just as I did in the last case, I'll go copy that
as well and modify it as necessary.
We'll copy our search function.
Now, this looks like code duplication, and you might be asking yourself, why didn't you just declare
the types in another package or something?
Well, that's because over time, the configuration for the API and the configuration for the front
end might be markedly different.
And I just like to keep things as clean as I possibly can.
So back here, let's just paste in that search function and we'll need to import HTP servers.
So HTP like that.
That's that one format package will import that.
And time will import that.
So that fixes all of those will have to take care of our RUTINA minute.
And we're not starting the server.
We're starting the back end server and everything else should stay the same.
And of course, we have to call it.
So error equals Abdulnasser, if error is not equal to nil, we'll just say log fatele with error and
this needs to be an assignment.
Very operator.
OK, so there, with the exception of routes file is our initial setup for this application.
So let's create a file in the API folder or create a new file and I will call it routes, routes, dash
API dot com and it has the package of Main and it has a function with the receiver, which is a pointer
to application and it's called routes and it returns and HTP Handler.
And again, we'll use our mux router, our key router and declare variable marks.
And that's a sign the value of Cheetah New River.
And we can return.
So we'll need to set up some actual routes to handle things, but one thing we're absolutely going to
have to have is a way of dealing with calls so that we can allow requests only from those sites that
we want.
And when development, it doesn't matter what it is, but we can actually take advantage of a package,
an extension to the key router that makes that much easier.
And the package should remain here.
There was an air.
That looks better.
So if you go back and look at the CHY homepage that we got our actual router package from, you'll notice
that there's something called Go Dash Cheese course.
And I'm going to go get that package right now.
So go get that.
I'll paste in what?
I just copied it from another screen and that will give me a really easy middleware package that I can
use to determine who has the rights to access my API.
OK, so it's been added now I can use it and you can go look at the documentation yourself.
I'll post a link to that, to the course resources for this lecture.
But it's as simple as mux don't use and we're going to use the course package.
ACARS dog handler.
And that's going to take a course options and we'll just specify the things that we want to be in there.
So the first thing I want is what are my allowed origins?
And I want to allow and has to be a slice of strings.
I want to allow HTP, Collins, Sturr, anybody which is really wide open, but it's sufficient for
development purposes and HGP, Collins, anybody.
So that's my allowed origins.
Now, what options are we going to allow allowed method's?
What kind of requests are we going to take?
Well, again, it's a slice of string and we're going to allow get.
Post put delete and options, OK, we're allowing everything basically this is wide open and allowed
headers, not allowed headers.
Are we going to have well, slice of string, as you probably guessed.
And we'll allow except you have to have authorization because at some point we might have to allow people
to authenticate using, say, JSON Web tokens, content, dash type, which you're going to want to
have, and eventually we're going to add extra protection.
So X X RF dash token.
I'm not going to get to that for a while, but we will get to it.
So those are my allowed headers and will allow credentials.
We're going to set false for that and if necessary, will change later.
And Max Age will set that to say three hundred five minutes.
That should be sufficient.
So now we have middleware, one piece of middleware that will set the appropriate course information
and restrict who can and cannot access our backend.
So we have our middleware and we have our roots, but we actually don't have a root or a handler yet.
So we'll take care of setting up our handler to get a payment intent in the next election.