- Go Web Development Cookbook
- Arpit Aggarwal
- 303字
- 2021-08-27 19:01:21
How it works…
Once we run the program, the HTTP server will start locally listening on port 8080.
Next, we will execute a couple of commands to see how the session works.
First, we will access /home by executing the following command:
$ curl -X GET http://localhost:8080/home
This will result in an unauthorized access message from the server as shown in the following screenshot:

This is because we first have to log in to an application, which will create a session ID that the server will validate before providing access to any web page. So, let's log in to the application:
$ curl -X GET -i http://localhost:8080/login
Executing the previous command will give us the Cookie, which has to be set as a request header to access any web page:

Once the previous command is executed, a Cookie will be created and saved in Redis, which you can see by executing the command from redis-cli or in the Redis Browser, as shown in the following screenshot:

Next, we will use the Cookie provided to access /home, as follows:
$ curl --cookie "session-name=MTUyMzEwNDUyM3xOd3dBTkV4T1JrdzNURFkyUkVWWlQxWklUekpKVUVOWE1saFRUMHBHVTB4T1RGVXlSRU5RVkZWWk5VeFNWVmRPVVZSQk4wTk1RMUU9fAlGgLGU-OHxoP78xzEHMoiuY0Q4rrbsXfajSS6HiJAm;" http://localhost:8080/home
This results in the Home Page as a response from the server:

Let's understand the changes we introduced in this recipe:
- Using var store *redisStore.RediStore, we declared a private RediStore to store sessions in Redis.
- Next, we updated the init() function to create NewRediStore with a size and maximum number of idle connections as 10, and assigned it to the store. If there is an error while creating a store, then we log the error and exit with a status code of 1.
- Finally, we updated main() to introduce the defer store.Close() statement, which closes the Redis store once we return from the function.
- 物聯(lián)網(wǎng)與北斗應(yīng)用
- 網(wǎng)絡(luò)云百問(wèn)百答
- 自動(dòng)駕駛網(wǎng)絡(luò):自智時(shí)代的網(wǎng)絡(luò)架構(gòu)
- 物聯(lián)網(wǎng)工程規(guī)劃技術(shù)
- Twilio Cookbook
- 物聯(lián)網(wǎng)檢驗(yàn)檢測(cè)技術(shù)
- TCP/IP入門經(jīng)典(第5版)
- 面向云平臺(tái)的物聯(lián)網(wǎng)多源異構(gòu)信息融合方法
- 世界互聯(lián)網(wǎng)發(fā)展報(bào)告·2019
- 城域網(wǎng)與廣域網(wǎng)(第2版)
- WordPress Web Application Development
- Microsoft Dynamics CRM 2011 Applications(MB2-868) Certification Guide
- 移動(dòng)物聯(lián)網(wǎng):商業(yè)模式+案例分析+應(yīng)用實(shí)戰(zhàn)
- 組網(wǎng)技術(shù)與網(wǎng)絡(luò)管理
- 云計(jì)算技術(shù)與標(biāo)準(zhǔn)化