1
00:00:01,140 --> 00:00:06,480
So our task right now is to figure out how to get this app variable, which is an app config and app

2
00:00:06,480 --> 00:00:11,130
config, is nothing more than a struct that holds certain kinds of things we want to share with our

3
00:00:11,130 --> 00:00:11,840
application.

4
00:00:12,270 --> 00:00:15,800
We need to get that around to the parts of our application that have to have it.

5
00:00:16,320 --> 00:00:20,510
And right away, I can see there are two places that are going to need this app config.

6
00:00:20,970 --> 00:00:22,320
The obvious one is render.

7
00:00:22,340 --> 00:00:27,930
Render is going to have to have this because we don't want to recreate the template cache every time

8
00:00:27,930 --> 00:00:29,340
we build a page.

9
00:00:29,730 --> 00:00:31,460
So that's one place that needs to have it.

10
00:00:31,800 --> 00:00:36,060
And the other place that's going to have to have it, obviously, are the handlers and the handlers

11
00:00:36,060 --> 00:00:40,640
are going to have to have access to all kinds of configuration settings for the application.

12
00:00:41,400 --> 00:00:42,960
So let's do the render.

13
00:00:42,960 --> 00:00:49,170
First one I'm going to do here is I'm going to create a new function inside this render template, OK?

14
00:00:49,770 --> 00:00:53,070
And the render template is going to serve.

15
00:00:53,070 --> 00:01:01,380
The function is going to be called func new templates and it's going to take an argument.

16
00:01:01,380 --> 00:01:03,330
And the argument is going to take is a pointer.

17
00:01:03,330 --> 00:01:05,190
We'll call it a for the variable.

18
00:01:05,460 --> 00:01:08,260
It's going to be a pointer to config app config.

19
00:01:10,050 --> 00:01:12,780
Now I need to do something with that, a variable.

20
00:01:12,780 --> 00:01:18,910
So I'm going to create a new variable here called App and app is going to be a pointer to config app

21
00:01:18,990 --> 00:01:19,550
config.

22
00:01:21,420 --> 00:01:27,450
And when this function is called, it's just going to set that variable up equals, hey, that's it.

23
00:01:27,900 --> 00:01:28,630
Pretty straightforward.

24
00:01:29,160 --> 00:01:34,820
Let's give this thing a comment as we should create a new template.

25
00:01:35,670 --> 00:01:37,050
Sorry, let's just do this.

26
00:01:38,780 --> 00:01:43,930
Sets the config for the template package, not only the trick.

27
00:01:44,250 --> 00:01:46,970
OK, so I've got this function.

28
00:01:47,000 --> 00:01:48,000
I need to call it somewhere.

29
00:01:48,050 --> 00:01:51,590
Let's go back to Main Dutko and think about when we should call it.

30
00:01:51,620 --> 00:01:53,530
Well, we've created the app variable here.

31
00:01:53,690 --> 00:01:55,190
Then we get our template cache.

32
00:01:55,460 --> 00:01:57,110
Then we assign that to this.

33
00:01:57,410 --> 00:02:02,480
And now we want to call that new template cache so I can call two templates.

34
00:02:04,250 --> 00:02:12,260
Sorry, render create new templates, and that's going to take a pointer to app config, so I can't

35
00:02:12,260 --> 00:02:14,660
just put put app in there because that's not going to work.

36
00:02:14,660 --> 00:02:15,770
It wants a pointer instead.

37
00:02:15,770 --> 00:02:17,910
I want a reference to that there.

38
00:02:17,930 --> 00:02:24,440
So that will give our application the render component or the render package of our application access

39
00:02:24,440 --> 00:02:25,900
to this app config variable.

40
00:02:26,390 --> 00:02:33,770
So let's go back to our render routines and do something with this app variable right here.

41
00:02:34,100 --> 00:02:37,940
Instead of creating the template cache, let's just use it.

42
00:02:38,150 --> 00:02:42,260
So Tsay equals app dot template cache.

43
00:02:43,550 --> 00:02:47,680
You have to of course assign that not equal and that we can get rid of this.

44
00:02:48,920 --> 00:02:54,860
So if we did this properly, what should happen now is I should be able to run this application except

45
00:02:54,860 --> 00:02:57,530
I still have an error log.

46
00:02:57,530 --> 00:02:58,550
Fatal error.

47
00:02:58,550 --> 00:03:00,520
So if not, OK, log Fadal.

48
00:03:00,540 --> 00:03:01,870
Well we don't actually have an error here.

49
00:03:01,880 --> 00:03:07,220
I'll just say instead, log fatele could not create a template.

50
00:03:07,430 --> 00:03:14,360
You could not get a template from template cache.

51
00:03:14,900 --> 00:03:15,800
We'll just do that for now.

52
00:03:16,700 --> 00:03:17,420
Fixes that.

53
00:03:17,420 --> 00:03:20,960
And now down here we have to assign assign the error variable.

54
00:03:21,470 --> 00:03:21,790
Good.

55
00:03:21,950 --> 00:03:23,150
So that should take care of it all.

56
00:03:23,150 --> 00:03:29,150
Now we should be able to actually run this application and we'll create our template cache when the

57
00:03:29,150 --> 00:03:30,170
application starts.

58
00:03:30,530 --> 00:03:36,710
And then when we render a page, we're pulling the value from this application structure, our config.

59
00:03:36,920 --> 00:03:37,910
Let's see if that works.

60
00:03:38,930 --> 00:03:45,300
So we'll stop our application, go run command web Stargirl.

61
00:03:46,750 --> 00:03:47,810
It started.

62
00:03:47,840 --> 00:03:48,340
That's good.

63
00:03:48,380 --> 00:03:51,440
Let's go over to our Web browser and reload the homepage.

64
00:03:52,130 --> 00:03:53,000
And there it is.

65
00:03:53,000 --> 00:03:55,960
And let's go to the about page and there it is.

66
00:03:56,420 --> 00:03:58,010
Now, that is way more efficient.

67
00:03:58,010 --> 00:04:01,520
We're not accessing the disk to get the template every time we load the page.

68
00:04:01,520 --> 00:04:08,090
Instead, we build a map that holds all of our templates, put that in an application wide site config

69
00:04:08,090 --> 00:04:09,950
and we can render our templates.

70
00:04:09,950 --> 00:04:11,540
Well, that is a real improvement.

71
00:04:11,570 --> 00:04:16,220
Now, there's a lot more we can do with that configuration variable and we may as well do it now because

72
00:04:16,220 --> 00:04:19,490
we know that the handlers are also going to have to have access to it.

73
00:04:19,490 --> 00:04:25,310
They may not use the template cache, but we are going to put things in there that will make the application

74
00:04:25,490 --> 00:04:28,060
run much better because we can share configuration files.

75
00:04:28,220 --> 00:04:29,270
So how are we going to do that?

76
00:04:29,780 --> 00:04:35,900
Well, to start with, we're going to go to our handlers page and again create some new things.

77
00:04:35,930 --> 00:04:38,860
Now, this will seem a little odd to you to begin with.

78
00:04:38,870 --> 00:04:42,140
We're going to use something called we're going to use something called the repository pattern.

79
00:04:42,650 --> 00:04:48,020
It's a very common pattern that allows it allows us to swap components out of our application with a

80
00:04:48,020 --> 00:04:51,290
minimal changes required to the code base.

81
00:04:51,560 --> 00:04:55,250
It seems a little confusing at first, but hopefully by the time I go through it a couple of times,

82
00:04:55,250 --> 00:04:57,410
it will make sense to you to start with.

83
00:04:57,560 --> 00:05:03,240
I'm going to create a variable or a creative type first, and this is going to be a type it's going

84
00:05:03,240 --> 00:05:04,460
to we're going to have to give it a name.

85
00:05:04,460 --> 00:05:09,500
I shall call it repository, because it's the repository pattern and it is a structure.

86
00:05:10,400 --> 00:05:18,100
And all it's going to hold right now is an app, which is a type pointer to config app config.

87
00:05:18,530 --> 00:05:18,980
All right.

88
00:05:19,490 --> 00:05:20,780
So I've now imported that.

89
00:05:20,780 --> 00:05:21,910
I've created my type.

90
00:05:22,280 --> 00:05:27,020
So let's create a variable that uses that type VAR and I'm going to call this repo and I'm going to

91
00:05:27,020 --> 00:05:28,190
give it a capital letter.

92
00:05:28,190 --> 00:05:30,980
So it's visible outside of this package, which is critical.

93
00:05:31,610 --> 00:05:33,800
It's a type repository.

94
00:05:34,740 --> 00:05:40,530
So I've now created a variable that I'm not using with the type that I'm only using to initialize this

95
00:05:40,530 --> 00:05:42,340
variable or declare this variable.

96
00:05:42,690 --> 00:05:45,910
So now I need a couple of new things to start with.

97
00:05:46,230 --> 00:05:54,300
This is a function and I'm going to call it new repo, and it's going to take an argument.

98
00:05:54,300 --> 00:05:59,300
And the argument is I'll just give it the name of a because it is an application config.

99
00:05:59,610 --> 00:06:08,620
It's a type start, a pointer to config config, and it's going to return a pointer to a repository.

100
00:06:10,110 --> 00:06:10,540
All right.

101
00:06:11,100 --> 00:06:19,710
So to do that, all I have to do is return a reference to repo, the variable that I haven't done anything

102
00:06:19,710 --> 00:06:20,160
with yet.

103
00:06:20,820 --> 00:06:26,430
And I'm going to populate that with the only thing I have to put in that that type an app.

104
00:06:30,710 --> 00:06:31,940
Type of repository.

105
00:06:34,400 --> 00:06:39,980
An app and a sign of the value of, hey, that I passed right here in this perimeter, so now I have

106
00:06:39,980 --> 00:06:43,090
a function, new repo, OK, I'm not using it yet.

107
00:06:43,190 --> 00:06:48,260
I need one more function to create new handlers, funk, new handlers.

108
00:06:49,660 --> 00:06:56,800
And it's going to tape take an argument, ah, which is a type pointer to a repository and it doesn't

109
00:06:56,800 --> 00:06:58,480
return anything, it just sets a variable.

110
00:06:58,720 --> 00:07:02,770
And what it's going to set is the variable that I declared up here, repoll

111
00:07:05,260 --> 00:07:06,730
equals Perth.

112
00:07:08,020 --> 00:07:11,440
And let's give these things comments and we'll go through what they're supposed to do.

113
00:07:12,670 --> 00:07:13,800
This is a new handlers.

114
00:07:14,260 --> 00:07:27,280
It sets the repository for the handlers and this is new repo creates a new repository.

115
00:07:30,000 --> 00:07:31,110
And this is the type.

116
00:07:34,530 --> 00:07:40,140
Is the repository type and are variable.

117
00:07:42,770 --> 00:07:43,820
The repository.

118
00:07:46,940 --> 00:07:54,230
Used by the handlers, now, the only thing in a repository right now is actually the app config where

119
00:07:54,230 --> 00:07:55,340
we're going to put more things in here.

120
00:07:55,350 --> 00:07:59,060
For example, when we connect to databases, we need somewhere to share the database.

121
00:07:59,060 --> 00:08:05,540
Connection pool will share that using the repository structure, and it will make our life ever so much

122
00:08:05,540 --> 00:08:05,900
easier.

123
00:08:05,930 --> 00:08:09,740
So what's going to happen is, first of all, in my main function.

124
00:08:14,730 --> 00:08:22,680
I need to after I have declared my app variable, I need to set things up with our handlers and the

125
00:08:22,680 --> 00:08:27,780
very first thing I'm going to do is create a variable called repo that's going to call that function

126
00:08:27,900 --> 00:08:29,730
handler's new repo.

127
00:08:30,660 --> 00:08:33,180
And I'm going to pass it a reference to my app.

128
00:08:34,650 --> 00:08:37,260
OK, so that creates the repository variable.

129
00:08:37,290 --> 00:08:42,540
I'm not doing anything with that yet, but I'm out to after I create that repository variable, I pass

130
00:08:42,540 --> 00:08:45,750
it back to the handlers and create new handlers.

131
00:08:45,750 --> 00:08:50,390
So the handlers, new handlers and I'm going to pass it repels.

132
00:08:51,930 --> 00:08:56,790
And that should do the trick now, why on earth would I do this?

133
00:08:57,570 --> 00:09:00,000
If you think about it logically, I create a repository.

134
00:09:00,000 --> 00:09:01,860
Fine, so that calls new repoll.

135
00:09:02,070 --> 00:09:07,530
New report says take this app config that you just passed, appointed to the app config and populate

136
00:09:07,530 --> 00:09:13,460
the struct repository, return a new instance of this type that holds the application.

137
00:09:13,920 --> 00:09:14,300
Great.

138
00:09:14,310 --> 00:09:19,920
So I've done that in Maine and then I say I just take that thing I just created in handlers and I pass

139
00:09:19,920 --> 00:09:21,270
it back to handlers.

140
00:09:21,750 --> 00:09:28,860
So I call new handlers and new handlers actually just sets that variable repo, but I'm not using that

141
00:09:28,860 --> 00:09:29,250
anywhere.

142
00:09:29,310 --> 00:09:33,690
Well, actually, I'm going to and I'm going to use it in a very simple spot, one that will make sense

143
00:09:33,690 --> 00:09:34,630
to you momentarily.

144
00:09:36,240 --> 00:09:39,350
It does require a change to the functions themselves.

145
00:09:39,360 --> 00:09:42,390
And what I'm going to do is give these functions a receiver.

146
00:09:42,630 --> 00:09:48,120
And what it effectively does is links all of these functions, all of these handlers together with the

147
00:09:48,120 --> 00:09:48,970
repository.

148
00:09:49,140 --> 00:09:52,800
So all of the handlers have access to that repository.

149
00:09:53,310 --> 00:09:56,580
So I can do that simply by always seem to function.

150
00:09:56,580 --> 00:10:02,250
So far as func the keyword func that says I'm declaring a function, then the name just before the name

151
00:10:02,250 --> 00:10:05,550
in parentheses, I'm going to put a new variable, which I'm going to call em.

152
00:10:05,550 --> 00:10:06,810
I can call it whatever I want.

153
00:10:06,810 --> 00:10:12,630
But Amasa simple one letter thing that I don't have to do a lot of typing for and it's of type pointer

154
00:10:12,630 --> 00:10:18,210
to repository and then I'm going to copy that and paste it down here.

155
00:10:18,960 --> 00:10:25,080
Now these these both of these handlers and any other handlers I create that have this receiver have

156
00:10:25,080 --> 00:10:29,920
access to everything inside repository, which happens to be my application config.

157
00:10:30,690 --> 00:10:35,370
Now there's one more change I need to make before I can compile this and make it work, and we'll demonstrate

158
00:10:35,370 --> 00:10:36,210
that it does work.

159
00:10:36,810 --> 00:10:45,540
Over here in Maine, my handlers, my handler funk's don't work anymore and there's no reason why they

160
00:10:45,540 --> 00:10:46,050
don't work.

161
00:10:46,240 --> 00:10:47,670
Seems to be my handlers.

162
00:10:47,670 --> 00:10:48,960
I have a function called home.

163
00:10:48,960 --> 00:10:53,680
If I go over to handlers, sure enough, I have a function called home, but now I have a function with

164
00:10:53,680 --> 00:10:55,680
the receiver of type repo.

165
00:10:55,980 --> 00:11:01,020
So all that means is that I need to put dot repo in front of that.

166
00:11:02,230 --> 00:11:09,550
And repo in front of that, and now it should work and to demonstrate that it's going to work, let's

167
00:11:09,550 --> 00:11:13,870
pull something out of that configuration and have a look at it.

168
00:11:13,890 --> 00:11:15,970
Well, what do we have an app config right now?

169
00:11:16,000 --> 00:11:17,260
Well, we just have a template cache.

170
00:11:17,270 --> 00:11:18,550
I don't want to pull anything out of that.

171
00:11:18,760 --> 00:11:22,210
Let's just add a new structure to this and a new member to this.

172
00:11:22,210 --> 00:11:27,410
And I'm going to call it use cash and it's going to be of type, Wolf.

173
00:11:28,990 --> 00:11:30,730
So true or false?

174
00:11:30,760 --> 00:11:33,040
So now I've added this to the app config.

175
00:11:33,040 --> 00:11:36,370
So everything that refers to app config now has access to it.

176
00:11:36,670 --> 00:11:38,140
Let's go back to Mean Dudko.

177
00:11:38,410 --> 00:11:44,950
And just before we declare a repository, let's say app use cache equals false.

178
00:11:46,300 --> 00:11:49,810
I've now assigned the value of false to use cache and there's a reason for that.

179
00:11:50,080 --> 00:11:51,160
And let me show you what it is.

180
00:11:51,190 --> 00:11:52,620
We still have the application running, right?

181
00:11:52,630 --> 00:11:55,750
Let's make sure we do well on the about page.

182
00:11:56,260 --> 00:12:01,630
If I go look at the template for the about page and I'll bring up my templates here, find the templates

183
00:12:02,050 --> 00:12:12,520
there's about if I make a change to this template, let's say we add P, this is a paragraph of text

184
00:12:12,670 --> 00:12:16,170
and save it and then go back to the about page and reload it.

185
00:12:16,180 --> 00:12:20,110
You would think that I would see that paragraph of text I just typed.

186
00:12:20,530 --> 00:12:24,090
But I don't and I don't because I'm not reading it from the disk anymore.

187
00:12:24,100 --> 00:12:26,960
Remember, I'm pulling it from that template cache that we created.

188
00:12:27,580 --> 00:12:33,820
So what I'd like to be able to do is to say if you check the application preferences, so let's go to

189
00:12:35,200 --> 00:12:35,980
the render here.

190
00:12:36,550 --> 00:12:40,720
When I get the application preferences, I have access to the app here.

191
00:12:40,730 --> 00:12:42,640
Now I have access to the app variable.

192
00:12:42,640 --> 00:12:49,900
So let's say if I'm in development mode, not production, don't eat, don't use the template cache,

193
00:12:49,900 --> 00:12:51,990
instead rebuild it on every request.

194
00:12:52,000 --> 00:13:00,790
What I can do that really easily and I can do it by saying if app don't use cache, then pull the information

195
00:13:00,790 --> 00:13:01,270
out of that.

196
00:13:02,260 --> 00:13:03,740
Otherwise I want to read it.

197
00:13:03,910 --> 00:13:08,680
So what it's going to mean of course, is that I have to see what a template cache returns.

198
00:13:08,680 --> 00:13:10,570
It returns a map, string the string.

199
00:13:11,720 --> 00:13:16,330
So let's declare a variable just so I can get it outside of the scope of this.

200
00:13:16,480 --> 00:13:23,410
If statement var T.C. is of type that and then I'm not assigning, I'm really pulling it out.

201
00:13:24,070 --> 00:13:29,770
Otherwise, if it's false, T.C. equals create template cache.

202
00:13:32,680 --> 00:13:37,210
And that returns a template and a cache and an error error.

203
00:13:41,120 --> 00:13:43,460
Let's just ignore the air, you know, it works right now.

204
00:13:43,820 --> 00:13:46,270
OK, so what have I done now?

205
00:13:46,280 --> 00:13:50,950
I'm actually going to look in the configuration file and ask myself a simple question.

206
00:13:51,680 --> 00:13:54,080
First of all, I declare the variable this is going to hold my template.

207
00:13:54,080 --> 00:13:59,780
Cash, if used cash is true, then read the information from the template cache.

208
00:13:59,960 --> 00:14:02,120
Otherwise rebuild the template cache.

209
00:14:02,330 --> 00:14:05,490
And back in Maine, I said use cash defaults.

210
00:14:05,510 --> 00:14:10,520
So now if I start this up again, stop the application, start it up.

211
00:14:11,750 --> 00:14:14,600
Everything compiles and we look at the about page.

212
00:14:14,600 --> 00:14:17,750
It has this paragraph of text and I refresh the about page.

213
00:14:17,750 --> 00:14:19,190
OK, that's what I expect.

214
00:14:19,370 --> 00:14:24,440
And I would expect that whether you use cash was true or false because it's reading it right from the

215
00:14:24,440 --> 00:14:25,610
template cash that it built.

216
00:14:26,120 --> 00:14:32,600
But now because I'm in development mode, because I have this variable use, a used cash set defaults

217
00:14:32,810 --> 00:14:37,080
and because I have access to that in render, I can test against it.

218
00:14:37,250 --> 00:14:44,690
So now if I go back to my about page and add another paragraph of text and save it now, it should read

219
00:14:44,690 --> 00:14:47,930
it from the disk because I'm in development mode and there it is.

220
00:14:48,590 --> 00:14:49,110
All right.

221
00:14:49,490 --> 00:14:53,350
So this gives us a lot of control and a lot of power.

222
00:14:53,360 --> 00:14:58,970
It means that we can share information that parts of our application need really, really easily.

223
00:14:59,000 --> 00:15:04,370
If I need to add something to the application config, I just go to app config struct, create a new

224
00:15:04,370 --> 00:15:10,460
type, our new member for this this type, and it's immediately available to every other part of the

225
00:15:10,460 --> 00:15:12,890
application that might need to have access to it.

226
00:15:14,180 --> 00:15:15,050
That was a lot of work.

227
00:15:15,140 --> 00:15:18,990
Finally, in the next lecture, we're going to pass some data to templates.

228
00:15:19,020 --> 00:15:23,390
It's taken us a while to get here, but things are starting to shape up and they're starting to look

229
00:15:23,390 --> 00:15:23,750
good.
