| 
									
										
										
										
											2023-09-03 04:11:56 +02:00
										 |  |  | package app | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"net/http" | 
					
						
							|  |  |  | 	"time" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/flosch/pongo2/v6" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-04 17:32:45 +02:00
										 |  |  | func (app *App) ErrorTemplate(w http.ResponseWriter, r *http.Request, header, desc string) error { | 
					
						
							|  |  |  | 	return app.Template(w, r, "error.tpl", pongo2.Context{ | 
					
						
							|  |  |  | 		"header":      header, | 
					
						
							|  |  |  | 		"description": desc, | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-03 04:11:56 +02:00
										 |  |  | func (app *App) Template(w http.ResponseWriter, r *http.Request, tmplName string, ctx pongo2.Context) error { | 
					
						
							|  |  |  | 	tmpl, err := app.tmpl.FromCache(tmplName) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	tctx := pongo2.Context{ | 
					
						
							| 
									
										
										
										
											2023-09-04 03:33:13 +02:00
										 |  |  | 		"config":        app.DBConfig.Get(), | 
					
						
							| 
									
										
										
										
											2023-09-03 04:11:56 +02:00
										 |  |  | 		"flash_message": app.getFlash(w, r), | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	tctx.Update(ctx) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	w.Header().Set("Content-Type", "text/html") | 
					
						
							|  |  |  | 	return tmpl.ExecuteWriter(tctx, w) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const flashCookieName = "mercury-flash-message" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (app *App) Flash(w http.ResponseWriter, msg string) { | 
					
						
							|  |  |  | 	http.SetCookie(w, &http.Cookie{ | 
					
						
							|  |  |  | 		Name:     flashCookieName, | 
					
						
							|  |  |  | 		Value:    msg, | 
					
						
							|  |  |  | 		Path:     "/", | 
					
						
							|  |  |  | 		HttpOnly: true, | 
					
						
							|  |  |  | 		Expires:  time.Now().Add(time.Minute), | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (app *App) getFlash(w http.ResponseWriter, r *http.Request) string { | 
					
						
							|  |  |  | 	cookie, err := r.Cookie(flashCookieName) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return "" | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	defer http.SetCookie(w, &http.Cookie{ | 
					
						
							|  |  |  | 		Name:     flashCookieName, | 
					
						
							|  |  |  | 		Value:    "", | 
					
						
							|  |  |  | 		Path:     "/", | 
					
						
							|  |  |  | 		HttpOnly: true, | 
					
						
							|  |  |  | 		Expires:  time.Now(), | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return cookie.Value | 
					
						
							|  |  |  | } |