feat: add article page
This commit is contained in:
68
handlers/article.go
Normal file
68
handlers/article.go
Normal file
@ -0,0 +1,68 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/PuerkitoBio/goquery"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/rramiachraf/dumb/data"
|
||||
"github.com/rramiachraf/dumb/utils"
|
||||
"github.com/rramiachraf/dumb/views"
|
||||
)
|
||||
|
||||
func article(l *utils.Logger) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
articleSlug := mux.Vars(r)["article"]
|
||||
|
||||
if a, err := getCache[data.Article](articleSlug); err == nil {
|
||||
views.ArticlePage(a).Render(context.Background(), w)
|
||||
return
|
||||
}
|
||||
|
||||
url := fmt.Sprintf("https://genius.com/a/%s", articleSlug)
|
||||
|
||||
resp, err := utils.SendRequest(url)
|
||||
if err != nil {
|
||||
l.Error(err.Error())
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
views.ErrorPage(500, "cannot reach Genius servers").Render(context.Background(), w)
|
||||
return
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode == http.StatusNotFound {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
views.ErrorPage(404, "page not found").Render(context.Background(), w)
|
||||
return
|
||||
}
|
||||
|
||||
doc, err := goquery.NewDocumentFromReader(resp.Body)
|
||||
if err != nil {
|
||||
l.Error(err.Error())
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
views.ErrorPage(500, "something went wrong").Render(context.Background(), w)
|
||||
return
|
||||
}
|
||||
|
||||
cf := doc.Find(".cloudflare_content").Length()
|
||||
if cf > 0 {
|
||||
l.Error("cloudflare got in the way")
|
||||
views.ErrorPage(500, "cloudflare is detected").Render(context.Background(), w)
|
||||
return
|
||||
}
|
||||
|
||||
var a data.Article
|
||||
if err = a.Parse(doc); err != nil {
|
||||
l.Error(err.Error())
|
||||
}
|
||||
|
||||
views.ArticlePage(a).Render(context.Background(), w)
|
||||
|
||||
if err = setCache(articleSlug, a); err != nil {
|
||||
l.Error(err.Error())
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user