mirror of
https://github.com/aljazceru/njump.git
synced 2025-12-17 06:14:22 +01:00
Set Cache-Control to 60s on failed fetch
This way we can permit a fast retry while keeping some control on DoS attacks
This commit is contained in:
@@ -57,6 +57,7 @@ func render(w http.ResponseWriter, r *http.Request) {
|
|||||||
if strings.HasPrefix(code, "note1") {
|
if strings.HasPrefix(code, "note1") {
|
||||||
_, redirectHex, err := nip19.Decode(code)
|
_, redirectHex, err := nip19.Decode(code)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
w.Header().Set("Cache-Control", "max-age=60")
|
||||||
http.Error(w, "error fetching event: "+err.Error(), 404)
|
http.Error(w, "error fetching event: "+err.Error(), 404)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -75,6 +76,7 @@ func render(w http.ResponseWriter, r *http.Request) {
|
|||||||
// code can be a nevent, nprofile, npub or nip05 identifier, in which case we try to fetch the associated event
|
// code can be a nevent, nprofile, npub or nip05 identifier, in which case we try to fetch the associated event
|
||||||
event, err := getEvent(r.Context(), code)
|
event, err := getEvent(r.Context(), code)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
w.Header().Set("Cache-Control", "max-age=60")
|
||||||
http.Error(w, "error fetching event: "+err.Error(), 404)
|
http.Error(w, "error fetching event: "+err.Error(), 404)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -132,6 +134,7 @@ func render(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
w.Header().Set("Cache-Control", "max-age=60")
|
||||||
http.Error(w, "error fetching event: "+err.Error(), 404)
|
http.Error(w, "error fetching event: "+err.Error(), 404)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -319,6 +322,8 @@ func render(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
if (strings.Contains(typ, "profile") && len(renderableLastNotes) != 0) || (!strings.Contains(typ, "profile") && len(content) != 0) {
|
if (strings.Contains(typ, "profile") && len(renderableLastNotes) != 0) || (!strings.Contains(typ, "profile") && len(content) != 0) {
|
||||||
w.Header().Set("Cache-Control", "max-age=604800")
|
w.Header().Set("Cache-Control", "max-age=604800")
|
||||||
|
} else {
|
||||||
|
w.Header().Set("Cache-Control", "max-age=60")
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := tmpl.ExecuteTemplate(w, templateMapping[typ], params); err != nil {
|
if err := tmpl.ExecuteTemplate(w, templateMapping[typ], params); err != nil {
|
||||||
|
|||||||
@@ -66,6 +66,8 @@ func renderArchive(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
if len(data) != 0 {
|
if len(data) != 0 {
|
||||||
w.Header().Set("Cache-Control", "max-age=86400")
|
w.Header().Set("Cache-Control", "max-age=86400")
|
||||||
|
} else {
|
||||||
|
w.Header().Set("Cache-Control", "max-age=60")
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := tmpl.ExecuteTemplate(w, "archive.html", params); err != nil {
|
if err := tmpl.ExecuteTemplate(w, "archive.html", params); err != nil {
|
||||||
|
|||||||
@@ -74,6 +74,8 @@ func renderRelayPage(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
if len(renderableLastNotes) != 0 {
|
if len(renderableLastNotes) != 0 {
|
||||||
w.Header().Set("Cache-Control", "max-age=604800")
|
w.Header().Set("Cache-Control", "max-age=604800")
|
||||||
|
} else {
|
||||||
|
w.Header().Set("Cache-Control", "max-age=60")
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := tmpl.ExecuteTemplate(w, templateMapping[typ], params); err != nil {
|
if err := tmpl.ExecuteTemplate(w, templateMapping[typ], params); err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user