1
00:00:00,990 --> 00:00:04,470
So let's play with Sessions and make sure that they work the way that we want them to.

2
00:00:04,500 --> 00:00:10,810
And I'm going to come up with a completely arbitrary and artificial test to make sure that things work.

3
00:00:10,830 --> 00:00:11,730
So here's what I'm going to do.

4
00:00:11,740 --> 00:00:13,920
I'm looking in the handler's folder right now.

5
00:00:13,920 --> 00:00:14,880
I have two handlers.

6
00:00:14,970 --> 00:00:18,960
I have one for the home page and I have one for the about page.

7
00:00:19,470 --> 00:00:21,270
So what I'm going to do is this.

8
00:00:21,270 --> 00:00:28,080
I am going to grab the remote IP address of the person visiting my site and store it in the home page.

9
00:00:28,080 --> 00:00:30,390
And I'm going to do that on the home a stored in the session.

10
00:00:30,390 --> 00:00:33,380
I'm going to do that in the home handler and that's really quite easy to do.

11
00:00:33,900 --> 00:00:36,010
So I first of all, I need the remote IP address.

12
00:00:36,030 --> 00:00:36,630
Well, that's easy.

13
00:00:36,630 --> 00:00:43,290
I can pull that right out of the request, so I'll call a variable remote IP is assigned the value of

14
00:00:43,560 --> 00:00:45,150
our remote address.

15
00:00:45,570 --> 00:00:53,880
OK, so that's built right into the standard library is part of the http http package and I can just

16
00:00:53,880 --> 00:00:54,770
pull the remote address.

17
00:00:54,780 --> 00:00:59,120
So that'll be an IP version four or an IP version six address and that's returned as a string.

18
00:00:59,130 --> 00:01:00,440
So I have a string here right now.

19
00:01:00,980 --> 00:01:05,850
I want to put that in the session and I know I have access to the session from the site where I config

20
00:01:05,850 --> 00:01:12,660
and I can get to the site where I config from this M variable that's part of the receiver M app session.

21
00:01:13,140 --> 00:01:16,290
Put and put, as you can see, takes three arguments.

22
00:01:16,590 --> 00:01:22,740
The context a key to look up the value if I want to get it out of the session and the actual value that

23
00:01:22,740 --> 00:01:24,620
I'm storing in that session, larible.

24
00:01:24,630 --> 00:01:27,000
So I'll put our context.

25
00:01:28,590 --> 00:01:32,160
The name will be remote IP and this can be whatever I want it to be.

26
00:01:32,160 --> 00:01:33,810
But that may that may make sense.

27
00:01:34,200 --> 00:01:40,440
And the value is remote IP, which I just thought, OK, so if you think about this, all I'm doing

28
00:01:40,680 --> 00:01:46,020
is the first time somebody hits that home page or actually every time somebody hits that home page for

29
00:01:46,020 --> 00:01:53,280
that user session, I'm storing the remote IP as a string in the session with the key to look it up

30
00:01:53,280 --> 00:01:54,640
being remote IP.

31
00:01:54,840 --> 00:01:55,290
All right.

32
00:01:55,380 --> 00:01:56,930
Well, that doesn't seem terribly helpful.

33
00:01:57,180 --> 00:01:58,150
What am I going to do with this?

34
00:01:58,800 --> 00:02:03,060
Well, in the other handler, I'm going to pull that value out of the session.

35
00:02:03,330 --> 00:02:12,720
So I will say remote IP is assigned and app session get and I'm going to use get string so I don't have

36
00:02:12,720 --> 00:02:13,560
to typecast it.

37
00:02:14,100 --> 00:02:21,660
Get the string from the context arg context and the key to look it up is remote IP.

38
00:02:22,170 --> 00:02:28,800
And the thing to remember here is that this value will be an empty string if there is nothing in the

39
00:02:28,800 --> 00:02:31,080
session named remote IP.

40
00:02:31,660 --> 00:02:33,930
So now that I have that, I need to put it in my string.

41
00:02:33,930 --> 00:02:49,200
That string map oops map and I'll call it remote IP is given the value of remote IP and now I'm going

42
00:02:49,200 --> 00:02:50,670
to go to my vote page template.

43
00:02:50,700 --> 00:02:54,810
So let's find that about Page and we'll put a little bit logic in here.

44
00:02:56,270 --> 00:03:01,940
In a new paragraph, I'm going to put an if statement in here, so this is a and if statement as part

45
00:03:01,940 --> 00:03:03,170
of the template package.

46
00:03:03,170 --> 00:03:09,350
So it doesn't follow the standard go syntax, but it's really simple, if not equal to.

47
00:03:09,380 --> 00:03:11,650
I'm going to make sure that things are not equal.

48
00:03:12,050 --> 00:03:22,070
So if not equal to and then look up the first value index dot string map, remote IP and it has to match

49
00:03:22,070 --> 00:03:25,250
case if that's not equal to nothing.

50
00:03:25,490 --> 00:03:28,640
In other words, if there's a value stored in that string, that's not an empty string.

51
00:03:28,640 --> 00:03:30,740
I want to do something else.

52
00:03:30,950 --> 00:03:32,090
I want to do something else.

53
00:03:33,620 --> 00:03:34,040
And

54
00:03:37,100 --> 00:03:37,490
all right.

55
00:03:37,760 --> 00:03:46,340
So if my value stored in the string map is not equal to an empty string, then I want to say your remote

56
00:03:46,580 --> 00:03:52,090
IP address is and then I'll just look up that value again.

57
00:03:52,100 --> 00:03:56,480
So I'll just copy and paste this copy and paste.

58
00:03:58,190 --> 00:04:00,080
Otherwise I got at the wrong spot.

59
00:04:01,610 --> 00:04:13,310
Paste, otherwise, I don't know your IP address yet, visit the atrip equals slash homepage.

60
00:04:16,990 --> 00:04:21,560
So I can set it all right now, this should all work.

61
00:04:21,580 --> 00:04:22,760
Let's give this a try.

62
00:04:22,780 --> 00:04:26,380
First of all, let me go back to my Web browser, make sure I'm not on the homepage.

63
00:04:26,380 --> 00:04:27,690
Slug's close that tab out.

64
00:04:27,700 --> 00:04:32,570
I'll open New Initech and then close this one and close this one and close this one.

65
00:04:32,590 --> 00:04:32,870
All right.

66
00:04:32,870 --> 00:04:35,980
So I'm starting from a known place.

67
00:04:37,200 --> 00:04:41,580
Go run command web, me or StarTalk, go.

68
00:04:45,030 --> 00:04:52,830
It compiles good and now I'm going to go to localhost, but I want to go to the vote page because I

69
00:04:52,830 --> 00:04:58,940
don't want to set that sectional variable yet, I hit it and it does not connect rom part number eighty

70
00:04:58,950 --> 00:04:59,300
eighty.

71
00:05:02,160 --> 00:05:03,140
This is the vote page.

72
00:05:03,150 --> 00:05:04,710
I don't know your IP address yet.

73
00:05:04,710 --> 00:05:06,580
Visit the home page so I can set it.

74
00:05:06,600 --> 00:05:07,590
So I'll go to the homepage.

75
00:05:08,130 --> 00:05:14,790
And now that I visited that home page, when I go back I should get a different message because it should

76
00:05:14,790 --> 00:05:20,910
be able to pull a non-empty string out of this session that has my remote IP address and there it is,

77
00:05:21,090 --> 00:05:24,930
one two seven zero zero one and then appart number after.

78
00:05:25,470 --> 00:05:29,280
So that is exactly what I wanted to do and that's how sessions work.

79
00:05:29,640 --> 00:05:33,930
So we have just so you know, let me go back to the record, not the roots, the handlers.

80
00:05:34,320 --> 00:05:39,630
We have a bunch of ways of getting things out of a session or putting things or one way to put it in,

81
00:05:39,630 --> 00:05:41,010
but a bunch of ways of getting it out.

82
00:05:41,550 --> 00:05:45,390
So up here, when I said Aptos session, get string.

83
00:05:45,390 --> 00:05:47,760
Let's look at some of the other things that are built into the session.

84
00:05:47,760 --> 00:05:55,710
Package Emden after session dot I can get direct access to the cookie I can find the lifetime of the

85
00:05:55,710 --> 00:05:56,250
session.

86
00:05:56,550 --> 00:05:58,980
I can look for the codec which are never going to do.

87
00:05:58,980 --> 00:06:00,000
But it's there if you want it.

88
00:06:00,330 --> 00:06:04,260
I can get a string, I can get anything I want out of the session.

89
00:06:04,260 --> 00:06:11,670
So often I'll store something that's not a string, it's not a an integer, one of the default primitive

90
00:06:11,670 --> 00:06:12,090
types.

91
00:06:12,270 --> 00:06:14,280
I can put whatever I want in the session.

92
00:06:14,280 --> 00:06:15,990
To do that I have to make one extra step.

93
00:06:15,990 --> 00:06:17,370
But we're not going to do that for a little while.

94
00:06:17,580 --> 00:06:21,330
But just so you know, the session package is pretty simple to use.

95
00:06:21,690 --> 00:06:24,480
And I like Alex Edwards excession.

96
00:06:24,480 --> 00:06:27,870
It works extremely well if you want to experiment with another one.

97
00:06:27,870 --> 00:06:29,040
Well, it's not that difficult.

98
00:06:29,040 --> 00:06:30,660
You already know how to install a package.

99
00:06:31,050 --> 00:06:33,180
Give it a try if you're so interested.

100
00:06:33,630 --> 00:06:35,280
Anyway, that's it for Sessions for right now.

101
00:06:35,280 --> 00:06:40,290
We'll be coming back to the usage of sessions on a regular basis as our application takes shape.

102
00:06:40,680 --> 00:06:43,170
But I think we've covered the basics for now.
