So this time around, what we want to do is to modify our roots, go file to allow for protected roots
and as I suggested, we'll be doing that using middleware.
So before we write the middleware, let's just go to the bottom of this file just before return monks,
and we'll put some more code in here.
And what I'm going to use is Muxtape Root, which is available to us from the cheap package.
And this allows us to create a new mux and apply middleware to it and actually to group certain kinds
of roots logically into one location.
So I'm going to decide that any route that begins with API admin is going to be protected by our middleware.
We're going to write momentarily.
And the way we do that is simply by putting a phone call in here func and handing it the router, which
I'll call Maxygen.
And it's a type dot router right there.
And everything inside this closure, this anonymous function will be grouped together.
So I can actually put a call to use middleware right in here, and it will only apply to the roots that
are included in this muxtape root.
So let's go right to middleware.
So over to my API folder.
I'll create a new file inside of there, which I'll call Middleware Dutko.
And of course, that's in package making.
So we'll write one bit of middleware in here, which I'll call off, and it's very, very simple and
actually it uses almost exactly the same logic as we did the last time we tried to authenticate a token
so of function.
And I'm going to give it the receiver of up to application because I'll be using some things that are
available from that receiver, not to call it off, because this is middleware that has one parameter,
which is almost always called next, and it's a type HTTP dog handler and it returns and HTP dog handler.
Now, inside of that I'll actually return and htp dog handler Folke and make sure you get handler func
and not handle func.
So handler func.
And that takes a function with two parameters, our response writer and a pointer to a request, our
star HDB request.
And all I'm going to do in here is call app dot authenticate.
So I don't care about the user in this case.
I just want to make sure I'm not getting an error.
And that's a sign the value of after authenticate token and all that requires is a request and we check
for an error.
If error is not equal to nil, then I'll call after invalid credentials because I don't like giving
too much information back to the user.
All I want to tell them is you can't do this and I'll return.
Otherwise we just keep going.
We pass our next door, serve HTP with our response operator and a request.
Now that we have this written, I can go back to Roots, a dash API, go and use that middleware don't
use and I'm going to use app dot off.
Now I can add as many routes as I want inside this, this anonymous function that's called from Muxtape
route and they'll all be protected.
So we'll get started on using this in the next lecture.