So this time around, we want to experiment with and write our first bit of middleware and the first
middleware we write is going to be completely useless.
The entire purpose of it is just to show you how middleware works and how you might go about writing
it.
So what I want to do is this is my roots file and I want to write some middleware that says every time
somebody hits a page, just write something to the console.
So in other words, format, print, line and some kind of message.
So to make that happen, the very first thing I have to do is create a new file, create a new go file
in command web called middleware.
Go.
All right.
In the middleware I go, I'm going to create a function and I'm going to create a function that's going
to look a little bit odd to you at first, but I'll explain it to you.
So, first of all, let's give it a name func right to console.
That's my function.
And it's going to take one parameter and I'm going to call that parameter next, which is pretty common
to name the parameter in middleware next.
When you're writing middleware, it's of type HTP Tandler and it returns and htp dot oops, HDB handler
as well.
And inside of that I'm going to write a return function.
I'm going to return a type of htp dot handler func.
Inside of that I'm going to have an anonymous function func and it's going to take as most things to
deal with handlers due to arguments w htp dot response writer and our star request, which you've seen
many times by this point, and that is going to inside of that return function.
Here's what we're actually returning a better put a capital H there.
What we're going to return is something that's really simple.
We're going to say print to the console format, dot print line, hit the page, OK?
And then we need to at the end of this, we need to move onto the next and the next might be another
bit of middleware or it might be the part in our file where we actually return our mux.
So we'll just say next dot serve http w our response writer and the pointer to the request.
So now I've written this little bit of this little bit of code that's just a bit of middleware and it's
a function, it has a name, it takes an argument which is a handler, it returns a handler.
And inside of that I have the return function which consists of an anonymous function cast to a handler
func, and that that will actually allow us to serve our middleware.
OK, so how do I use this?
Well, the same way I used other middleware.
I'll go back to my roots folder and I will say, are my roots file and say Muxtape use?
And I called it right to console.
That's it.
That's all I should have to do.
So if I start my application by going to my terminal window and writing Go Run Command Web Stargirl,
I'm now compiling three files in my web folder, Main Middleware and Roots and run that.
Good, so it's running and then I go over to my Web browser and I just reload this page a few times,
so I've reloaded it three times and I come back and there wrote the middleware three times.
That's how you write basic middleware.
And most middleware, actually, all middleware will have a very similar format to what you're seeing
right here now.
There'll be other instances where you don't have to have this anonymous function and you just return
on HTP Handler and we'll be seeing that in just a moment.
Actually, right now, before too much longer, we're going to start building a useful application.
Right now, our application is nothing.
It's just showing us how things work.
We have one about page which really has no information.
We have one home page that has no information.
We're actually going to build an application and I'll be talking about that.
Not the next section.
The section after that, probably we're going to build an application that we'll actually have forms
on the page, for example.
And you may recall a while ago, I said in that default data, let's find that render function right
here actually is not there.
And we put it into where is it, template data.
Remember when we made this model for template data, I said we're going to be using C SRF tokens.
Well, now is the perfect time to actually look at how we can populate that.
We're going to do the whole thing, but we'll get started.
That CSR CSR token stands for cross site request forgery token.
And it's nothing more that when you build a Web page, you have a hidden with a form on it.
You have a hidden field in that form, which is a long string of random numbers, and they change every
single time somebody goes to a page.
Well, that seems to me like a really suitable place for middleware instead of as we've done here in
this right to console, just format, print line, hit the page.
We could generate that token.
Well, it turns out there's a really good package which already exists that does precisely that.
Let's go to Google and find Googling.
No search because it's called no surf.
I put any on there.
Let's get rid of that.
Here it is, GitHub.
So this is a vulnerability, but that gives me the name of it, that's all I really want.
So let's copy that paste into here.
And this is a nice little package that actually generates this for us.
It's an easy package for go that helps you prevent CSF.
So how do we actually use this?
It's pretty straightforward.
Let's find our install package.
We shouldn't bother giving it to me, but I can figure it out or copy that part of the URL.
Go back to my terminal or my I.D. Stop the application and clear the screen.
Go get that.
So it's gone and gotten it for us.
Let's make sure it's in our goman because I like to verify those sort of things.
Go on.
There it is, it's installed.
Well, we can actually use this without too much difficulty at all.
So back in my middleware, well, first of all, my roots, let's get rid of this one.
We're not actually going to do anything with because it's useless.
Delete that back in here.
I'll delete this because I'm not using it.
Actually, only that there is a reference.
It's not hurting anything because it's never called.
I need to create a new bit of middleware func no serve next htp dot handler and it returns and htp dot
handler as all middleware must.
And what I'm going to do here is create a new variable, CSIR, F Tandler.
I can call it whatever I want, but that's a meaningful name equals no sirf dot.
Knew, and then I put in my next variable, so I'm taking this handler, which is an argument to I think
I misspelled that.
No, sir.
There we are.
And I'm taking the argument that this an HP handler passed to this Nosseir function, I'm using that
as an argument to know Cerf knew, which will actually create a handler for us.
And then I need to set some values for this.
See SRF Handler.
We need to set the base cookie because it uses cookies to make sure that the token it generates is available
on a per page basis.
So inside of that, we have the argument htp cookie, we're creating a new cookie and that's going to
be HTP only, let's you say true for that path, cookie path.
I want this to apply to the entire site.
So Slash is the way that you refer to the entire site for a cookie secure?
I'm going to say false because we're not running on HTP right now, but in production will actually
change that to true and same site.
The fourth parameter, we'll just use the in standard for that, which is same site, lax mode.
All right.
That's just enough information to create our No.
SIRF token.
That's all that we need.
So we've installed the package.
No SIRF now we've created new middleware for no surf and we need of course to use it.
We have one error here somewhere.
What is that error.
Oh yeah.
I got to return something.
I better return CSIR handler there because if I don't return something we're never going to get past
this bit of middleware.
Let's go back to our Roots folder and now so we want to use my best use and I called it nurser.
That's it.
So now this should still compile, let's compile it, go run, command, web, go.
It does perfect.
And I should be able to reload the pages here.
No problem at all, it does exactly what I want.
So we've now put in a bit of middleware, which we're going to use a little bit later in the course
when we start process are building forms and processing form posts.
But that's really all you need to worry about for making middleware.
And as time goes on, we'll be adding to this and we might even be using some more external packages.
But this gives us a good start.
All right.
We need to move on next to sessions, which will also require us to use some middleware.
That's enough for this time around.