-
Notifications
You must be signed in to change notification settings - Fork 0
/
route404.go
76 lines (65 loc) · 2.01 KB
/
route404.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"net/http"
"github.com/go-chi/chi/middleware"
"github.com/gorilla/sessions"
"github.com/nicksnyder/go-i18n/v2/i18n"
)
type notFoundPageData struct {
Session *sessions.Session
IsAuthenticated bool
User User
Translations notFoundPageTranslations
}
type notFoundPageTranslations struct {
Navbar navbarTranslations
NotFoundText string
}
func notFound(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
sessionError := ctx.Value("session_error").(string)
if sessionError != "" {
log.Error("There was an error getting the user's session")
log.Error(sessionError)
reqID := middleware.GetReqID(ctx)
http.Error(w, "Error getting user session\nRequestID: "+reqID, 500)
return
}
session := ctx.Value("session").(*sessions.Session)
isAuthenticated := ctx.Value("isAuthenticated").(bool)
var user User
if isAuthenticated {
userError := ctx.Value("user_error").(string)
if userError != "" {
log.Error("There was an error getting the user's account info")
log.Error(userError)
reqID := middleware.GetReqID(ctx)
http.Error(w, "Error getting user account info\nRequestID: "+reqID, 500)
return
}
user = *ctx.Value("user").(*User)
}
// Localization
lang := session.Values["language"].(string)
accept := r.Header.Get("Accept-Language")
localizer := i18n.NewLocalizer(bundle, lang, accept)
translations := translateNotFoundPage(localizer, isAuthenticated, user)
pageData := notFoundPageData{
Session: session,
IsAuthenticated: isAuthenticated,
User: user,
Translations: translations,
}
w.WriteHeader(404)
templates.ExecuteTemplate(w, "404.html", pageData)
}
func translateNotFoundPage(localizer *i18n.Localizer, isAuthenticated bool, user User) notFoundPageTranslations {
navbar := translateNavbar(localizer, isAuthenticated, user)
notFoundText := localizer.MustLocalize(&i18n.LocalizeConfig{
MessageID: "NotFoundText",
})
return notFoundPageTranslations{
Navbar: navbar,
NotFoundText: notFoundText,
}
}