fix: resolve jikan api eof and context cancellation errors
- add http transport with connection pooling to prevent stale connections - check ctx.Done() before each retry attempt to abort early - pass context to renderer to skip writing on canceled requests
This commit is contained in:
@@ -29,7 +29,7 @@ type quickSearchResult struct {
|
||||
|
||||
func renderNotFoundPage(r *http.Request, w http.ResponseWriter) {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
if err := templates.GetRenderer().ExecuteTemplate(w, "not_found.gohtml", map[string]any{
|
||||
if err := templates.GetRenderer().ExecuteTemplate(r.Context(), w, "not_found.gohtml", map[string]any{
|
||||
"CurrentPath": r.URL.Path,
|
||||
}); err != nil {
|
||||
log.Printf("render error: %v", err)
|
||||
@@ -98,7 +98,7 @@ func (h *Handler) HandleCatalog(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
if err := templates.GetRenderer().ExecuteTemplate(w, "index.gohtml", map[string]any{
|
||||
if err := templates.GetRenderer().ExecuteTemplate(r.Context(), w, "index.gohtml", map[string]any{
|
||||
"MostPopular": animes.Animes,
|
||||
"CurrentlyAiring": currentlyAiring.Animes,
|
||||
"ContinueWatching": cw,
|
||||
@@ -150,7 +150,7 @@ func (h *Handler) HandleBrowse(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "text/html")
|
||||
err := templates.GetRenderer().ExecuteFragment(w, "browse.gohtml", "anime_card_scroll", map[string]any{
|
||||
err := templates.GetRenderer().ExecuteFragment(r.Context(), w, "browse.gohtml", "anime_card_scroll", map[string]any{
|
||||
"Animes": res.Animes,
|
||||
"NextPage": page + 1,
|
||||
"HasNextPage": res.HasNextPage,
|
||||
@@ -184,7 +184,7 @@ func (h *Handler) HandleBrowse(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
if err := templates.GetRenderer().ExecuteTemplate(w, "browse.gohtml", map[string]any{
|
||||
if err := templates.GetRenderer().ExecuteTemplate(r.Context(), w, "browse.gohtml", map[string]any{
|
||||
"User": user,
|
||||
"CurrentPath": r.URL.Path,
|
||||
"Query": q,
|
||||
@@ -251,7 +251,7 @@ func (h *Handler) HandleAnimeDetails(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
if err := templates.GetRenderer().ExecuteTemplate(w, "anime.gohtml", map[string]any{
|
||||
if err := templates.GetRenderer().ExecuteTemplate(r.Context(), w, "anime.gohtml", map[string]any{
|
||||
"Anime": anime,
|
||||
"User": user,
|
||||
"Status": status,
|
||||
@@ -287,7 +287,7 @@ func (h *Handler) HandleHTMLWatchOrder(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
if err := templates.GetRenderer().ExecuteFragment(w, "anime.gohtml", "watch_order", map[string]any{
|
||||
if err := templates.GetRenderer().ExecuteFragment(r.Context(), w, "anime.gohtml", "watch_order", map[string]any{
|
||||
"Relations": relations,
|
||||
"AnimeID": id,
|
||||
"WatchlistMap": watchlistMap,
|
||||
@@ -390,7 +390,7 @@ func (h *Handler) HandleDiscover(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
if err := templates.GetRenderer().ExecuteTemplate(w, "discover.gohtml", map[string]any{
|
||||
if err := templates.GetRenderer().ExecuteTemplate(r.Context(), w, "discover.gohtml", map[string]any{
|
||||
"User": user,
|
||||
"CurrentPath": r.URL.Path,
|
||||
"Trending": uniqueTrending,
|
||||
|
||||
@@ -25,7 +25,7 @@ func rateLimitErrorFromQuery(r *http.Request) string {
|
||||
}
|
||||
|
||||
func (h *Handler) HandleLoginPage(w http.ResponseWriter, r *http.Request) {
|
||||
if err := templates.GetRenderer().ExecuteTemplate(w, "login.gohtml", map[string]any{
|
||||
if err := templates.GetRenderer().ExecuteTemplate(r.Context(), w, "login.gohtml", map[string]any{
|
||||
"CurrentPath": r.URL.Path,
|
||||
}); err != nil {
|
||||
log.Printf("render error: %v", err)
|
||||
@@ -34,7 +34,7 @@ func (h *Handler) HandleLoginPage(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
func (h *Handler) HandleLogin(w http.ResponseWriter, r *http.Request) {
|
||||
if err := r.ParseForm(); err != nil {
|
||||
templates.GetRenderer().ExecuteTemplate(w, "login.gohtml", map[string]any{
|
||||
templates.GetRenderer().ExecuteTemplate(r.Context(), w, "login.gohtml", map[string]any{
|
||||
"Error": "Something went wrong. Please try again.",
|
||||
"Username": "",
|
||||
"CurrentPath": r.URL.Path,
|
||||
@@ -46,7 +46,7 @@ func (h *Handler) HandleLogin(w http.ResponseWriter, r *http.Request) {
|
||||
password := r.FormValue("password")
|
||||
|
||||
if username == "" || password == "" {
|
||||
templates.GetRenderer().ExecuteTemplate(w, "login.gohtml", map[string]any{
|
||||
templates.GetRenderer().ExecuteTemplate(r.Context(), w, "login.gohtml", map[string]any{
|
||||
"Error": "The email or password is wrong.",
|
||||
"Username": username,
|
||||
"CurrentPath": r.URL.Path,
|
||||
@@ -56,7 +56,7 @@ func (h *Handler) HandleLogin(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
session, err := h.authService.Login(r.Context(), username, password)
|
||||
if err != nil {
|
||||
templates.GetRenderer().ExecuteTemplate(w, "login.gohtml", map[string]any{
|
||||
templates.GetRenderer().ExecuteTemplate(r.Context(), w, "login.gohtml", map[string]any{
|
||||
"Error": "The email or password is wrong.",
|
||||
"Username": username,
|
||||
"CurrentPath": r.URL.Path,
|
||||
|
||||
@@ -30,7 +30,7 @@ func NewHandler(svc *Service, jikanClient *jikan.Client) *Handler {
|
||||
|
||||
func renderNotFoundPage(r *http.Request, w http.ResponseWriter) {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
if err := templates.GetRenderer().ExecuteTemplate(w, "not_found.gohtml", map[string]any{
|
||||
if err := templates.GetRenderer().ExecuteTemplate(r.Context(), w, "not_found.gohtml", map[string]any{
|
||||
"CurrentPath": r.URL.Path,
|
||||
}); err != nil {
|
||||
log.Printf("render error: %v", err)
|
||||
@@ -174,7 +174,7 @@ func (h *Handler) HandleWatchPage(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
if err := templates.GetRenderer().ExecuteTemplate(w, "watch.gohtml", map[string]any{
|
||||
if err := templates.GetRenderer().ExecuteTemplate(r.Context(), w, "watch.gohtml", map[string]any{
|
||||
"Anime": anime,
|
||||
"Episodes": episodes.Data,
|
||||
"WatchData": watchData,
|
||||
|
||||
@@ -115,7 +115,7 @@ func (h *Handler) HandleGetWatchlist(w http.ResponseWriter, r *http.Request) {
|
||||
entries, err := h.service.GetUserWatchlist(r.Context(), user.ID)
|
||||
if err != nil {
|
||||
log.Printf("failed to fetch watchlist: %v", err)
|
||||
if err := templates.GetRenderer().ExecuteTemplate(w, "not_found.gohtml", map[string]any{
|
||||
if err := templates.GetRenderer().ExecuteTemplate(r.Context(), w, "not_found.gohtml", map[string]any{
|
||||
"CurrentPath": r.URL.Path,
|
||||
}); err != nil {
|
||||
log.Printf("render error: %v", err)
|
||||
@@ -158,7 +158,7 @@ func (h *Handler) HandleGetWatchlist(w http.ResponseWriter, r *http.Request) {
|
||||
templateName = "watchlist_partial.gohtml"
|
||||
}
|
||||
|
||||
if err := templates.GetRenderer().ExecuteTemplate(w, templateName, data); err != nil {
|
||||
if err := templates.GetRenderer().ExecuteTemplate(r.Context(), w, templateName, data); err != nil {
|
||||
log.Printf("render error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user