1
00:00:01,560 --> 00:00:06,630
So, as promised, this time around, I want to not use the pat router, instead, I want to use the

2
00:00:06,630 --> 00:00:08,890
key router and let's have a look at that.

3
00:00:08,910 --> 00:00:16,050
So over on my Web browser, I go here to GitHub, Dotcom, Shūgo, Dash, Kiguchi.

4
00:00:16,860 --> 00:00:21,580
And when I look at the actual description of this, it seems pretty good.

5
00:00:21,690 --> 00:00:22,950
It's a good writer.

6
00:00:22,950 --> 00:00:24,570
It's it's it's lightweight.

7
00:00:24,570 --> 00:00:25,260
It's fast.

8
00:00:25,260 --> 00:00:29,220
It's absolutely compatible with the standard library net HTP.

9
00:00:29,580 --> 00:00:30,990
It seems to be exactly.

10
00:00:31,260 --> 00:00:32,120
Are very useful.

11
00:00:32,400 --> 00:00:36,810
And one of the other nice things about this is if you scroll down you'll see it has some built in middleware,

12
00:00:36,810 --> 00:00:41,310
which we haven't talked about yet, but we're going to in the upcoming lectures, middleware allows

13
00:00:41,310 --> 00:00:46,420
us to process requests as they come in and make decisions about what to do with them.

14
00:00:46,440 --> 00:00:51,600
So, for example, I might have some pages on my site that I only want logged in users to see.

15
00:00:51,870 --> 00:00:57,690
And middleware is a great place to determine whether or not a user has the necessary rights to see that

16
00:00:57,690 --> 00:00:58,110
page.

17
00:00:58,500 --> 00:01:00,740
So it has a whole bunch of built in middleware.

18
00:01:00,780 --> 00:01:04,960
And you can see there there's all kinds of them listed right here and you can read those at your leisure

19
00:01:04,990 --> 00:01:08,220
will be using some of them, some of them in the next few minutes.

20
00:01:08,940 --> 00:01:09,350
All right.

21
00:01:09,360 --> 00:01:10,440
So how do I install this?

22
00:01:10,470 --> 00:01:15,530
Well, this is the same process we used to install, Pat.

23
00:01:15,810 --> 00:01:17,790
So let's find the install part.

24
00:01:18,300 --> 00:01:20,130
Should be go get somewhere in this page.

25
00:01:20,130 --> 00:01:22,440
Go get there it is install.

26
00:01:22,440 --> 00:01:23,100
Go get.

27
00:01:23,110 --> 00:01:30,570
So I'm going to copy that, go back to my ID, open a terminal window, stop my application because

28
00:01:30,570 --> 00:01:35,670
I had it running and I'll clear the screen and simply paste that command in and it goes and gets it.

29
00:01:36,240 --> 00:01:42,840
And again, if we look at our Gadot mod file, which is right here, it's now an important GOCE.

30
00:01:43,140 --> 00:01:47,560
It still has Pat, and we're still using Pat, but we're not going to be in just a moment.

31
00:01:48,390 --> 00:01:49,470
So how do I use this?

32
00:01:49,470 --> 00:01:53,460
Let me go back to my roots folder and let me just delete everything in the middle here.

33
00:01:53,460 --> 00:01:54,330
I'll just comment to that.

34
00:01:58,320 --> 00:02:03,510
All right, so now it says, we're not using Pat, you're not using handlers and we will be in a minute,

35
00:02:03,520 --> 00:02:09,960
so don't ignore those errors if you see them, instead of creating a new mux that is a type of pat,

36
00:02:10,380 --> 00:02:16,860
let me create a mux that is of type Chey dot newsreader.

37
00:02:19,020 --> 00:02:19,440
All right.

38
00:02:19,770 --> 00:02:23,180
So it got rid of Pat up here because we're not using it anymore.

39
00:02:23,190 --> 00:02:26,720
It's still in our go mode file, which I can show you right here.

40
00:02:27,390 --> 00:02:28,140
We don't want that.

41
00:02:28,260 --> 00:02:33,500
Now, you could manually delete this, but we're not going to will simply do a much cleaner method,

42
00:02:33,510 --> 00:02:40,500
will go down to our terminal window and say go mode tidy, tidy up my mod file, and when I do, it

43
00:02:40,500 --> 00:02:42,420
actually gets rid of unused packages.

44
00:02:42,480 --> 00:02:47,670
OK, so you're going to be doing that periodically and later on I'll show you how to update your dependencies

45
00:02:47,670 --> 00:02:52,350
so you may draw in a third party package that's updated and has some new features and you want to use

46
00:02:52,350 --> 00:02:54,450
the new version, but we'll get to that later on.

47
00:02:54,510 --> 00:02:56,090
Right now, we're just putting a restaurant.

48
00:02:56,100 --> 00:03:02,160
So I've created my new key router and I have a Mac and I'm returning the mux.

49
00:03:02,160 --> 00:03:07,530
Now, I want to have these pads matched in our router the same as we did before.

50
00:03:07,540 --> 00:03:09,870
So it's a very similar approach.

51
00:03:09,870 --> 00:03:21,180
Mux dot get the pattern is slash for home and I'm just going to go handler's repo, not home.

52
00:03:23,340 --> 00:03:24,960
Now, that seems like it works pretty well.

53
00:03:25,110 --> 00:03:31,640
OK, let's copy that one and do the one for boat route home.

54
00:03:32,730 --> 00:03:37,520
A report a boat seems to have worked.

55
00:03:37,680 --> 00:03:39,960
Let's compile it and see what happens.

56
00:03:43,360 --> 00:03:52,520
Go run command web main first sorry, StarTalk, go it compiled seems to be running.

57
00:03:52,540 --> 00:03:57,220
Let's go to our Web browser and open a new window and go to localhost.

58
00:03:58,700 --> 00:03:59,530
Eighty, eighty.

59
00:04:01,790 --> 00:04:09,890
There's the homepage perfect, and let's have a look at our about page to make sure that works about

60
00:04:10,250 --> 00:04:12,200
very good, that works extremely well.

61
00:04:12,500 --> 00:04:14,940
Now, why did I go to all the trouble of changing Rotor's?

62
00:04:14,960 --> 00:04:21,140
Well, the first to start with it was actually just to show you that it's not that difficult if you

63
00:04:21,140 --> 00:04:25,120
keep things separated from one another and keep things really clean.

64
00:04:25,160 --> 00:04:30,560
You can swap out an entire component of your package in just a couple of minutes, which is exactly

65
00:04:30,560 --> 00:04:31,360
what we just did.

66
00:04:31,940 --> 00:04:35,090
But there are other reasons I want to use this particular router as well.

67
00:04:35,090 --> 00:04:39,040
So I'm going to get rid of this commented code and I'm going to install some middleware.

68
00:04:39,290 --> 00:04:45,020
Now, as I said, middleware actually allows you to process a request as it comes into your Web application

69
00:04:45,290 --> 00:04:47,240
and perform some action on it.

70
00:04:47,810 --> 00:04:54,520
Well, let's go back to our Web browser and look at the list of middleware, which is way down here

71
00:04:54,530 --> 00:04:54,830
somewhere.

72
00:04:54,860 --> 00:04:55,360
There it is.

73
00:04:56,300 --> 00:04:57,440
Let's look at some of these.

74
00:04:57,440 --> 00:04:59,330
Some of these actually seem pretty good.

75
00:04:59,360 --> 00:04:59,930
Here's one.

76
00:04:59,930 --> 00:05:03,980
Recover gracefully, absorbs panic's and prints the stack trace.

77
00:05:04,280 --> 00:05:09,770
So sometimes hopefully not very often if you've if you've coated carefully, but sometimes your application

78
00:05:09,770 --> 00:05:16,220
will panic and a panic says, oh, no, the world is coming to an end, I am going to die, and your

79
00:05:16,220 --> 00:05:18,050
application just dies.

80
00:05:18,470 --> 00:05:22,780
What you would want to do instead is say, oh, no, something really bad happened.

81
00:05:23,030 --> 00:05:27,710
Here's some useful information about what went wrong and now I'm going to recover from that panic.

82
00:05:27,710 --> 00:05:30,020
So maybe I want to use this Rickover Middleware.

83
00:05:30,050 --> 00:05:30,990
Well, how do I do that?

84
00:05:31,730 --> 00:05:33,200
Well, it's actually pretty easy.

85
00:05:33,470 --> 00:05:43,190
I can just say mux don't use middleware, which is one of the things built into CZI and I want to find

86
00:05:43,190 --> 00:05:43,730
the Rickover.

87
00:05:43,730 --> 00:05:46,670
So Rickover, that's it.

88
00:05:46,670 --> 00:05:48,080
And I don't want those parentheses.

89
00:05:48,710 --> 00:05:49,090
That's it.

90
00:05:49,100 --> 00:05:54,710
So when I put that middleware in place because I've used it before these roots, it's actually available

91
00:05:54,710 --> 00:05:55,490
to us now.

92
00:05:55,490 --> 00:05:57,740
Are there any other ones that I want to put in there?

93
00:05:57,770 --> 00:06:00,590
Well, at the moment, let's have a look and just make sure.

94
00:06:01,350 --> 00:06:04,070
Is there anything else I want to use in those middleware?

95
00:06:04,670 --> 00:06:09,080
So let's find them again, allow content type basic auth.

96
00:06:09,090 --> 00:06:14,420
Not here, but basically if I want to use a more more secure version of authentication compress that'll

97
00:06:14,420 --> 00:06:15,670
be handled by my Web server.

98
00:06:15,680 --> 00:06:19,400
Don't care about that monitoring endpoint logger.

99
00:06:19,430 --> 00:06:23,510
What's Logger do logs the start and end of each request with the elapsed processing time.

100
00:06:23,510 --> 00:06:25,820
Now that'll be available to me in other ways.

101
00:06:26,540 --> 00:06:29,230
Well, for right now I'm not too worried about the rest of this.

102
00:06:29,240 --> 00:06:31,820
This seems to me like it might be OK.

103
00:06:31,820 --> 00:06:35,150
So we're going to make some more middleware and we'll do that in the next lecture.

104
00:06:35,150 --> 00:06:37,550
But for right now, this is pretty good.

105
00:06:37,550 --> 00:06:38,570
We have this running.

106
00:06:38,960 --> 00:06:41,210
So let's stop right now.

107
00:06:41,210 --> 00:06:47,600
In the next lecture, we'll look at actually writing our own middleware and using that in here, because

108
00:06:47,600 --> 00:06:50,960
sometimes what you need just doesn't come out of the box with your router.

109
00:06:51,260 --> 00:06:53,990
Actually, quite often it doesn't come out of the box with your router.

110
00:06:54,140 --> 00:06:57,710
So we'll write some basic middleware and add those in the next lecture.
