Experimenting with sessions
This commit is contained in:
		
							
								
								
									
										2
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								go.mod
									
									
									
									
									
								
							@ -6,4 +6,4 @@ require github.com/go-chi/chi/v5 v5.0.14
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
require github.com/justinas/nosurf v1.1.1
 | 
					require github.com/justinas/nosurf v1.1.1
 | 
				
			||||||
 | 
					
 | 
				
			||||||
require github.com/alexedwards/scs/v2 v2.8.0 // indirect
 | 
					require github.com/alexedwards/scs/v2 v2.8.0
 | 
				
			||||||
 | 
				
			|||||||
@ -29,6 +29,9 @@ func NewHandlers(r *Repository) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// Home is the about page handler
 | 
					// Home is the about page handler
 | 
				
			||||||
func (m *Repository) Home(w http.ResponseWriter, r *http.Request) {
 | 
					func (m *Repository) Home(w http.ResponseWriter, r *http.Request) {
 | 
				
			||||||
 | 
						remoteIP := r.RemoteAddr
 | 
				
			||||||
 | 
						m.App.Session.Put(r.Context(), "remote_ip", remoteIP)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	render.RenderTemplate(w, "home.page.tmpl", &models.TemplateData{})
 | 
						render.RenderTemplate(w, "home.page.tmpl", &models.TemplateData{})
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -37,6 +40,10 @@ func (m *Repository) About(w http.ResponseWriter, r *http.Request) {
 | 
				
			|||||||
	// perform some logic
 | 
						// perform some logic
 | 
				
			||||||
	stringMap := make(map[string]string)
 | 
						stringMap := make(map[string]string)
 | 
				
			||||||
	stringMap["test"] = "Hello world!"
 | 
						stringMap["test"] = "Hello world!"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						remoteIP := m.App.Session.GetString(r.Context(), "remote_ip")
 | 
				
			||||||
 | 
						stringMap["remote_ip"] = remoteIP
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// send the data to the template
 | 
						// send the data to the template
 | 
				
			||||||
	render.RenderTemplate(w, "about.page.tmpl", &models.TemplateData{StringMap: stringMap})
 | 
						render.RenderTemplate(w, "about.page.tmpl", &models.TemplateData{StringMap: stringMap})
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -7,6 +7,14 @@
 | 
				
			|||||||
            <p>This is the content</p>
 | 
					            <p>This is the content</p>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            <p>This came from the template: {{index .StringMap "test"}}</p>
 | 
					            <p>This came from the template: {{index .StringMap "test"}}</p>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            <p>
 | 
				
			||||||
 | 
					                {{if ne (index .StringMap "remote_ip") ""}}
 | 
				
			||||||
 | 
					                Your remote IP address is {{index .StringMap "remote_ip"}}
 | 
				
			||||||
 | 
					                {{else}}
 | 
				
			||||||
 | 
					                I don't know your ip address yet. Visit the <a href="/"></a> so I can set it.
 | 
				
			||||||
 | 
					                {{end}}
 | 
				
			||||||
 | 
					            </p>
 | 
				
			||||||
        </div>
 | 
					        </div>
 | 
				
			||||||
    </div>
 | 
					    </div>
 | 
				
			||||||
</div>
 | 
					</div>
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user