refactor: replace logrus with log/slog
This commit is contained in:
@ -8,11 +8,11 @@ import (
|
||||
"github.com/PuerkitoBio/goquery"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/rramiachraf/dumb/data"
|
||||
"github.com/rramiachraf/dumb/utils"
|
||||
"github.com/rramiachraf/dumb/views"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func album(l *logrus.Logger) http.HandlerFunc {
|
||||
func album(l *utils.Logger) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
artist := mux.Vars(r)["artist"]
|
||||
albumName := mux.Vars(r)["albumName"]
|
||||
@ -28,7 +28,7 @@ func album(l *logrus.Logger) http.HandlerFunc {
|
||||
|
||||
resp, err := sendRequest(url)
|
||||
if err != nil {
|
||||
l.Errorln(err)
|
||||
l.Error(err.Error())
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
views.ErrorPage(500, "cannot reach Genius servers").Render(context.Background(), w)
|
||||
return
|
||||
@ -44,7 +44,7 @@ func album(l *logrus.Logger) http.HandlerFunc {
|
||||
|
||||
doc, err := goquery.NewDocumentFromReader(resp.Body)
|
||||
if err != nil {
|
||||
l.Errorln(err)
|
||||
l.Error(err.Error())
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
views.ErrorPage(500, "something went wrong").Render(context.Background(), w)
|
||||
return
|
||||
@ -52,20 +52,20 @@ func album(l *logrus.Logger) http.HandlerFunc {
|
||||
|
||||
cf := doc.Find(".cloudflare_content").Length()
|
||||
if cf > 0 {
|
||||
l.Errorln("cloudflare got in the way")
|
||||
l.Error("cloudflare got in the way")
|
||||
views.ErrorPage(500, "i'll fix this later #21").Render(context.Background(), w)
|
||||
return
|
||||
}
|
||||
|
||||
var a data.Album
|
||||
if err = a.Parse(doc); err != nil {
|
||||
l.Error(err)
|
||||
l.Error(err.Error())
|
||||
}
|
||||
|
||||
views.AlbumPage(a).Render(context.Background(), w)
|
||||
|
||||
if err = setCache(id, a); err != nil {
|
||||
l.Errorln(err)
|
||||
l.Error(err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3,10 +3,12 @@ package handlers
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/PuerkitoBio/goquery"
|
||||
"github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/rramiachraf/dumb/utils"
|
||||
)
|
||||
|
||||
func TestAlbum(t *testing.T) {
|
||||
@ -19,7 +21,7 @@ func TestAlbum(t *testing.T) {
|
||||
}
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
l := logrus.New()
|
||||
l := utils.NewLogger(os.Stdout)
|
||||
m := New(l)
|
||||
|
||||
m.ServeHTTP(rr, r)
|
||||
|
@ -12,11 +12,11 @@ import (
|
||||
"github.com/PuerkitoBio/goquery"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/rramiachraf/dumb/data"
|
||||
"github.com/rramiachraf/dumb/utils"
|
||||
"github.com/rramiachraf/dumb/views"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func annotations(l *logrus.Logger) http.HandlerFunc {
|
||||
func annotations(l *utils.Logger) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
id := mux.Vars(r)["annotation-id"]
|
||||
if a, err := getCache[data.Annotation]("annotation:" + id); err == nil {
|
||||
@ -24,7 +24,7 @@ func annotations(l *logrus.Logger) http.HandlerFunc {
|
||||
|
||||
w.Header().Set("content-type", "application/json")
|
||||
if err = encoder.Encode(&a); err != nil {
|
||||
l.Errorln(err)
|
||||
l.Error(err.Error())
|
||||
}
|
||||
|
||||
return
|
||||
@ -34,7 +34,7 @@ func annotations(l *logrus.Logger) http.HandlerFunc {
|
||||
resp, err := sendRequest(url)
|
||||
|
||||
if err != nil {
|
||||
l.Errorln(err)
|
||||
l.Error(err.Error())
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
views.ErrorPage(500, "cannot reach genius servers").Render(context.Background(), w)
|
||||
return
|
||||
@ -51,7 +51,7 @@ func annotations(l *logrus.Logger) http.HandlerFunc {
|
||||
buf := new(bytes.Buffer)
|
||||
_, err = buf.ReadFrom(resp.Body)
|
||||
if err != nil {
|
||||
l.Errorln("Error paring genius api response", err)
|
||||
l.Error("Error paring genius api response: %s", err.Error())
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
views.ErrorPage(500, "something went wrong").Render(context.Background(), w)
|
||||
return
|
||||
@ -60,7 +60,7 @@ func annotations(l *logrus.Logger) http.HandlerFunc {
|
||||
var data data.AnnotationsResponse
|
||||
err = json.Unmarshal(buf.Bytes(), &data)
|
||||
if err != nil {
|
||||
l.Errorf("could not unmarshal json: %s\n", err)
|
||||
l.Error("could not unmarshal json: %s\n", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
views.ErrorPage(500, "something went wrong").Render(context.Background(), w)
|
||||
return
|
||||
@ -73,12 +73,12 @@ func annotations(l *logrus.Logger) http.HandlerFunc {
|
||||
encoder := json.NewEncoder(w)
|
||||
|
||||
if err = encoder.Encode(&body); err != nil {
|
||||
l.Errorln("Error sending response: ", err)
|
||||
l.Error("Error sending response: %s", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if err = setCache("annotation:"+id, body); err != nil {
|
||||
l.Errorln(err)
|
||||
l.Error(err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4,9 +4,10 @@ import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/rramiachraf/dumb/utils"
|
||||
)
|
||||
|
||||
func TestAnnotations(t *testing.T) {
|
||||
@ -18,7 +19,7 @@ func TestAnnotations(t *testing.T) {
|
||||
}
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
l := logrus.New()
|
||||
l := utils.NewLogger(os.Stdout)
|
||||
m := New(l)
|
||||
|
||||
m.ServeHTTP(rr, r)
|
||||
|
@ -7,11 +7,11 @@ import (
|
||||
"github.com/a-h/templ"
|
||||
gorillaHandlers "github.com/gorilla/handlers"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/rramiachraf/dumb/utils"
|
||||
"github.com/rramiachraf/dumb/views"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func New(logger *logrus.Logger) *mux.Router {
|
||||
func New(logger *utils.Logger) *mux.Router {
|
||||
r := mux.NewRouter()
|
||||
|
||||
r.Use(mustHeaders)
|
||||
|
@ -5,22 +5,22 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/rramiachraf/dumb/utils"
|
||||
"github.com/rramiachraf/dumb/views"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
const ContentTypeJSON = "application/json"
|
||||
|
||||
// TODO: move this to utils, so it can be used by other handlers.
|
||||
func sendError(err error, status int, msg string, l *logrus.Logger, w http.ResponseWriter) {
|
||||
l.Errorln(err)
|
||||
func sendError(err error, status int, msg string, l *utils.Logger, w http.ResponseWriter) {
|
||||
l.Error(err.Error())
|
||||
w.WriteHeader(status)
|
||||
if err := views.ErrorPage(status, msg).Render(context.Background(), w); err != nil {
|
||||
l.Errorln(err)
|
||||
l.Error(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func instances(l *logrus.Logger) http.HandlerFunc {
|
||||
func instances(l *utils.Logger) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if instances, err := getCache[[]byte]("instances"); err == nil {
|
||||
w.Header().Set("content-type", ContentTypeJSON)
|
||||
@ -46,11 +46,11 @@ func instances(l *logrus.Logger) http.HandlerFunc {
|
||||
|
||||
w.Header().Set("content-type", ContentTypeJSON)
|
||||
if _, err = w.Write(instances); err != nil {
|
||||
l.Errorln(err)
|
||||
l.Error(err.Error())
|
||||
}
|
||||
|
||||
if err = setCache("instances", instances); err != nil {
|
||||
l.Errorln(err)
|
||||
l.Error(err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4,9 +4,10 @@ import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/rramiachraf/dumb/utils"
|
||||
)
|
||||
|
||||
func TestInstancesList(t *testing.T) {
|
||||
@ -16,7 +17,7 @@ func TestInstancesList(t *testing.T) {
|
||||
}
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
l := logrus.New()
|
||||
l := utils.NewLogger(os.Stdout)
|
||||
|
||||
m := New(l)
|
||||
m.ServeHTTP(rr, r)
|
||||
|
@ -8,11 +8,11 @@ import (
|
||||
"github.com/PuerkitoBio/goquery"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/rramiachraf/dumb/data"
|
||||
"github.com/rramiachraf/dumb/utils"
|
||||
"github.com/rramiachraf/dumb/views"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func lyrics(l *logrus.Logger) http.HandlerFunc {
|
||||
func lyrics(l *utils.Logger) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
// prefer artist-song over annotation-id for cache key when available
|
||||
id := mux.Vars(r)["artist-song"]
|
||||
@ -30,7 +30,7 @@ func lyrics(l *logrus.Logger) http.HandlerFunc {
|
||||
url := fmt.Sprintf("https://genius.com/%s", id)
|
||||
resp, err := sendRequest(url)
|
||||
if err != nil {
|
||||
l.Errorln(err)
|
||||
l.Error(err.Error())
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
views.ErrorPage(500, "cannot reach Genius servers").Render(context.Background(), w)
|
||||
return
|
||||
@ -46,7 +46,7 @@ func lyrics(l *logrus.Logger) http.HandlerFunc {
|
||||
|
||||
doc, err := goquery.NewDocumentFromReader(resp.Body)
|
||||
if err != nil {
|
||||
l.Errorln(err)
|
||||
l.Error(err.Error())
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
views.ErrorPage(500, "something went wrong").Render(context.Background(), w)
|
||||
return
|
||||
@ -54,17 +54,19 @@ func lyrics(l *logrus.Logger) http.HandlerFunc {
|
||||
|
||||
cf := doc.Find(".cloudflare_content").Length()
|
||||
if cf > 0 {
|
||||
l.Errorln("cloudflare got in the way")
|
||||
l.Error("cloudflare got in the way")
|
||||
views.ErrorPage(500, "TODO: fix Cloudflare #21").Render(context.Background(), w)
|
||||
return
|
||||
}
|
||||
|
||||
var s data.Song
|
||||
s.Parse(doc)
|
||||
if err := s.Parse(doc); err != nil {
|
||||
l.Error(err.Error())
|
||||
}
|
||||
|
||||
views.LyricsPage(s).Render(context.Background(), w)
|
||||
if err = setCache(id, s); err != nil {
|
||||
l.Errorln(err)
|
||||
l.Error(err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3,10 +3,11 @@ package handlers
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/PuerkitoBio/goquery"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/rramiachraf/dumb/utils"
|
||||
)
|
||||
|
||||
func TestLyrics(t *testing.T) {
|
||||
@ -31,7 +32,7 @@ func testLyrics(t *testing.T, url string) {
|
||||
}
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
l := logrus.New()
|
||||
l := utils.NewLogger(os.Stdout)
|
||||
m := New(l)
|
||||
|
||||
m.ServeHTTP(rr, r)
|
||||
|
@ -9,8 +9,8 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/rramiachraf/dumb/utils"
|
||||
"github.com/rramiachraf/dumb/views"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func isValidExt(ext string) bool {
|
||||
@ -24,7 +24,7 @@ func isValidExt(ext string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func imageProxy(l *logrus.Logger) http.HandlerFunc {
|
||||
func imageProxy(l *utils.Logger) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
v := mux.Vars(r)
|
||||
f := v["filename"]
|
||||
@ -41,7 +41,7 @@ func imageProxy(l *logrus.Logger) http.HandlerFunc {
|
||||
|
||||
res, err := sendRequest(url)
|
||||
if err != nil {
|
||||
l.Errorln(err)
|
||||
l.Error(err.Error())
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
views.ErrorPage(500, "cannot reach Genius servers").Render(context.Background(), w)
|
||||
return
|
||||
@ -56,7 +56,7 @@ func imageProxy(l *logrus.Logger) http.HandlerFunc {
|
||||
w.Header().Add("Content-type", mime.TypeByExtension("."+ext))
|
||||
w.Header().Add("Cache-Control", "max-age=1296000")
|
||||
if _, err = io.Copy(w, res.Body); err != nil {
|
||||
l.Errorln("unable to write image", err)
|
||||
l.Error("unable to write image, %s", err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4,9 +4,10 @@ import (
|
||||
"mime"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/rramiachraf/dumb/utils"
|
||||
)
|
||||
|
||||
func TestImageProxy(t *testing.T) {
|
||||
@ -18,7 +19,7 @@ func TestImageProxy(t *testing.T) {
|
||||
}
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
l := logrus.New()
|
||||
l := utils.NewLogger(os.Stdout)
|
||||
m := New(l)
|
||||
|
||||
m.ServeHTTP(rr, r)
|
||||
|
@ -8,18 +8,18 @@ import (
|
||||
"net/url"
|
||||
|
||||
"github.com/rramiachraf/dumb/data"
|
||||
"github.com/rramiachraf/dumb/utils"
|
||||
"github.com/rramiachraf/dumb/views"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func search(l *logrus.Logger) http.HandlerFunc {
|
||||
func search(l *utils.Logger) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
query := r.URL.Query().Get("q")
|
||||
url := fmt.Sprintf(`https://genius.com/api/search/multi?q=%s`, url.QueryEscape(query))
|
||||
|
||||
res, err := sendRequest(url)
|
||||
if err != nil {
|
||||
l.Errorln(err)
|
||||
l.Error(err.Error())
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
views.ErrorPage(500, "cannot reach Genius servers").Render(context.Background(), w)
|
||||
return
|
||||
@ -31,7 +31,7 @@ func search(l *logrus.Logger) http.HandlerFunc {
|
||||
|
||||
d := json.NewDecoder(res.Body)
|
||||
if err = d.Decode(&sRes); err != nil {
|
||||
l.Errorln(err)
|
||||
l.Error(err.Error())
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
views.ErrorPage(500, "something went wrong").Render(context.Background(), w)
|
||||
}
|
||||
|
@ -3,10 +3,11 @@ package handlers
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/PuerkitoBio/goquery"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/rramiachraf/dumb/utils"
|
||||
)
|
||||
|
||||
func TestSearch(t *testing.T) {
|
||||
@ -19,7 +20,7 @@ func TestSearch(t *testing.T) {
|
||||
}
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
l := logrus.New()
|
||||
l := utils.NewLogger(os.Stdout)
|
||||
m := New(l)
|
||||
|
||||
m.ServeHTTP(rr, r)
|
||||
|
Reference in New Issue
Block a user