1
00:00:01,080 --> 00:00:06,810
So in this section, I want to talk about routing and I want to use our first external package, so

2
00:00:06,810 --> 00:00:15,060
if you look at our main DOT file, you'll notice that our routes are all set up here and that's OK.

3
00:00:15,240 --> 00:00:21,000
But it's really kind of awkward when you start getting a more complex Web application to have all of

4
00:00:21,000 --> 00:00:24,840
your routes right in your mean folder or your main routine.

5
00:00:25,380 --> 00:00:27,690
So what I want to do is create a separate file.

6
00:00:27,690 --> 00:00:36,450
So I'm going to go back to command web and create a new go file, which I will call routes that go all

7
00:00:36,450 --> 00:00:36,680
right.

8
00:00:36,720 --> 00:00:38,190
That's still part of our main package.

9
00:00:38,550 --> 00:00:43,620
And in here, I'm going to have a function and I'm going to call it think routes.

10
00:00:43,920 --> 00:00:46,650
And it's going to take one argument, which I will call map.

11
00:00:47,220 --> 00:00:57,840
And it is, of course, our pointer to config up config and it's going to return and HTP Tandler and

12
00:00:57,840 --> 00:01:00,540
you'll see why before too long, handler.

13
00:01:01,410 --> 00:01:01,840
All right.

14
00:01:02,430 --> 00:01:09,390
And inside of that, I'm actually going to create a new HTP handler, which we often call a mux or a

15
00:01:09,390 --> 00:01:10,200
multiplexed.

16
00:01:10,710 --> 00:01:12,960
And I'm going to do that using a package.

17
00:01:12,990 --> 00:01:16,900
I'm not going to use the built in routing functions that are part of the gold standard library.

18
00:01:17,160 --> 00:01:21,660
I want to use a third party package and we're going to use two, but we're going to start with one and

19
00:01:21,660 --> 00:01:22,680
then we'll switch to the other.

20
00:01:23,790 --> 00:01:26,190
We're going to use one that is actually really good at routing.

21
00:01:26,400 --> 00:01:27,390
It's a very simple one.

22
00:01:27,450 --> 00:01:30,660
And I'm going to go to my Web browser and show you what it is.

23
00:01:30,660 --> 00:01:38,310
It's here at GitHub, dotcom BME area and why I have no idea how to pronounce that Pat.

24
00:01:38,790 --> 00:01:39,910
And it's the router.

25
00:01:39,960 --> 00:01:45,210
So this is a package that we can import and use in our own code.

26
00:01:45,540 --> 00:01:48,100
So let's just scroll down and have a look at how do we install it.

27
00:01:48,120 --> 00:01:50,230
Go get GitHub dot com slash.

28
00:01:50,340 --> 00:01:51,530
All right, I'll copy that.

29
00:01:54,300 --> 00:02:01,800
I will go back to my I.D., open the terminal window and I'll clear the screen and I'll just paste that

30
00:02:01,800 --> 00:02:02,570
command right in there.

31
00:02:03,000 --> 00:02:09,150
Go get GitHub, dot com, slash that B word, slash pat and it gets it.

32
00:02:09,330 --> 00:02:09,800
All right.

33
00:02:09,930 --> 00:02:11,310
Now, what happened when I did that?

34
00:02:11,400 --> 00:02:17,850
Well, if I go and look at my go mod, you'll see that it now includes this package that was automatically

35
00:02:17,850 --> 00:02:18,660
put in there for us.

36
00:02:18,690 --> 00:02:19,060
All right.

37
00:02:19,500 --> 00:02:20,700
So I've got it installed.

38
00:02:21,090 --> 00:02:22,420
How do I use the darn thing?

39
00:02:23,310 --> 00:02:24,440
Well, it's pretty straightforward.

40
00:02:25,080 --> 00:02:30,960
First thing I'm going to do is create a multiplexed which which is actually at HTP Handler.

41
00:02:31,380 --> 00:02:37,380
So I will say Mux is assigned the value of Pat Daudt knew.

42
00:02:38,130 --> 00:02:38,520
All right.

43
00:02:38,530 --> 00:02:40,920
So I now have a mux and I can just return that.

44
00:02:42,840 --> 00:02:48,120
And in between where I create it and where I return it is where I actually set up my roots.

45
00:02:49,320 --> 00:02:52,470
And we only have two routes right now, so let's put them in.

46
00:02:53,430 --> 00:02:57,990
The first thing you should know is that Mux has some methods built in and you can see them listed here

47
00:02:57,990 --> 00:03:00,750
in my Idy and you probably have a similar thing in yours.

48
00:03:00,750 --> 00:03:02,640
And we want a get request.

49
00:03:03,060 --> 00:03:08,460
And the request takes two arguments, a pattern, which is the string, which is simply the route we're

50
00:03:08,460 --> 00:03:08,940
matching.

51
00:03:08,950 --> 00:03:09,270
All right.

52
00:03:09,270 --> 00:03:09,960
We can do that.

53
00:03:10,410 --> 00:03:14,610
And then it takes an HTTP handler func.

54
00:03:15,210 --> 00:03:18,420
Now we need to cast what we get to an active handler function.

55
00:03:18,420 --> 00:03:24,540
And the function we want is the one that's built into our handlers and that's the home function.

56
00:03:24,540 --> 00:03:29,740
So we can simply go handlers loops and

57
00:03:33,540 --> 00:03:38,030
dot repo home and that.

58
00:03:38,220 --> 00:03:38,870
There you go.

59
00:03:38,880 --> 00:03:47,940
So that will actually wrote to our home function and we can do the same thing here about should go to

60
00:03:47,940 --> 00:03:49,440
the about function.

61
00:03:50,100 --> 00:03:50,510
All right.

62
00:03:50,520 --> 00:03:55,470
So I've got this this function route's which takes app, which I'm not using, but I might need it at

63
00:03:55,470 --> 00:03:56,900
some point, so I'm going to pass that.

64
00:03:57,240 --> 00:03:59,170
How do we actually use this?

65
00:03:59,490 --> 00:04:01,770
Well, again, that's not too complex.

66
00:04:02,280 --> 00:04:05,090
All we need to do is make some changes in our main goal.

67
00:04:05,510 --> 00:04:06,930
We're not going to use these anymore.

68
00:04:06,930 --> 00:04:08,730
So Akhmatova's OK for right now.

69
00:04:10,230 --> 00:04:16,050
And instead of this, which is listen and serve, I'm going to comment at that, too, as well.

70
00:04:16,050 --> 00:04:16,850
So we're going to change it.

71
00:04:17,820 --> 00:04:22,620
What we need to do is actually create a new variable called serve or whatever you want to call it.

72
00:04:22,620 --> 00:04:30,210
I'm going to call it Serve S.V. because I am serving and I am going to make that equal to a pointer

73
00:04:30,210 --> 00:04:35,490
to HDB server and that has some fields.

74
00:04:35,790 --> 00:04:36,300
All right.

75
00:04:36,900 --> 00:04:40,030
Those fields are what's our address apart?

76
00:04:40,050 --> 00:04:40,850
Are we listening to.

77
00:04:40,860 --> 00:04:42,000
Well, we know what that is.

78
00:04:42,000 --> 00:04:43,020
That's our part number.

79
00:04:44,730 --> 00:04:47,880
And we also want to pass it, our handlers.

80
00:04:48,690 --> 00:04:54,030
And in this case, rather than writing them, as we did up here as individual functions, I'm actually

81
00:04:54,030 --> 00:04:57,180
going to call our routes and pass at the app.

82
00:04:59,470 --> 00:05:09,770
Got to Commissaire and Handler is singular, not plural, and this can't be that it needs to be that

83
00:05:10,070 --> 00:05:13,070
a reference to the app and what do we want to do this?

84
00:05:13,190 --> 00:05:16,070
That's all we really need to do at this point, is pass those in there.

85
00:05:16,080 --> 00:05:17,990
We're going to improve this as time goes on.

86
00:05:18,260 --> 00:05:23,690
But now I need to actually start that bit to start the actual server and we do it as follows.

87
00:05:24,200 --> 00:05:27,620
Error equals serve, listen and serve.

88
00:05:28,610 --> 00:05:33,450
And if there's an error, well, log that fatal error.

89
00:05:35,150 --> 00:05:44,720
So when I run this now, if this compiles, let's see if it does, it is go run, command web, start,

90
00:05:44,900 --> 00:05:45,380
go.

91
00:05:45,380 --> 00:05:49,200
And I do need the sternau because now I have two files in my web application.

92
00:05:49,430 --> 00:05:52,680
Let's hope this runs and it does.

93
00:05:52,730 --> 00:06:03,500
So let's go back to our web browser and go to localhost eighty eighty and there it is and about and

94
00:06:03,500 --> 00:06:04,070
there it is.

95
00:06:04,550 --> 00:06:05,050
Perfect.

96
00:06:05,210 --> 00:06:08,270
So now we've isolated our application routes so we can get rid of these.

97
00:06:08,270 --> 00:06:09,320
Commented out outbids.

98
00:06:11,920 --> 00:06:19,460
And have a look at our full main function and see what it looks like now, that's much cleaner.

99
00:06:20,380 --> 00:06:24,270
So we're now getting down to the point where we can actually put our roots in one folder.

100
00:06:25,090 --> 00:06:28,510
So the Pat Schroeder is fine for our purposes today.

101
00:06:28,510 --> 00:06:34,450
But in the next lecture, I want to swap out Pat for another one called CZI, or possibly she I've never

102
00:06:34,450 --> 00:06:38,230
actually heard it said out loud, but it's a different river and we're just going to do the swap to

103
00:06:38,230 --> 00:06:42,090
show you how easy it can be with gold modules to switch it out.

104
00:06:42,100 --> 00:06:46,930
Plus the key router has some built in middleware that will be using as well.

105
00:06:46,960 --> 00:06:48,640
So we'll talk about that in the next lecture.
