From de008faa3253afe07214ba41a41bb34af848a7f8 Mon Sep 17 00:00:00 2001 From: monirzadeh <25131576+Monirzadeh@users.noreply.github.com> Date: Fri, 19 Jan 2024 23:33:10 +0330 Subject: [PATCH 1/9] add workflow and dependabot --- .github/dependabot.yml | 23 +++++++++++++++++++++++ .github/workflows/ci.yml | 27 +++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 .github/dependabot.yml create mode 100644 .github/workflows/ci.yml diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..7151303 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,23 @@ +# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates + +version: 2 +updates: + # Maintain dependencies for GitHub Actions + - package-ecosystem: "github-actions" + directory: "/" + schedule: + interval: "weekly" + groups: + all: + patterns: + - "*" + + # Maintain dependencies for Golang + - package-ecosystem: "gomod" + directory: "/" + schedule: + interval: "monthly" + groups: + all: + patterns: + - "*" diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..2ea48da --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,27 @@ +# Source: https://github.com/actions/starter-workflows/blob/main/ci/go.yml +name: CI + +on: + push: + branches: [ main ] + pull_request: + branches: [ main ] + +permissions: + contents: read + +jobs: + # Test golangci-lint for go-version define in go.mod + golangci: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac #v4.0.0 + - uses: actions/setup-go@93397bea11091df50f3d7e59dc26a7711a8bcfbe #v4.1.0 + with: + go-version-file: go.mod + cache: false + - name: golangci-lint + uses: golangci/golangci-lint-action@3a919529898de77ec3da873e3063ca4b10e7f5cc #v3.7.0 + with: + version: latest + only-new-issues: true From 2b078849344b44d513512065fa319eb205b72a99 Mon Sep 17 00:00:00 2001 From: monirzadeh <25131576+Monirzadeh@users.noreply.github.com> Date: Fri, 19 Jan 2024 23:33:43 +0330 Subject: [PATCH 2/9] update golang to v1.21 --- go.mod | 20 ++++++++--------- go.sum | 68 ++++++++++++++++++++++++++++++++++++++++++---------------- 2 files changed, 60 insertions(+), 28 deletions(-) diff --git a/go.mod b/go.mod index 6b46600..63c93d0 100644 --- a/go.mod +++ b/go.mod @@ -1,20 +1,20 @@ module github.com/rramiachraf/dumb -go 1.18 +go 1.21 require ( - github.com/PuerkitoBio/goquery v1.8.0 - github.com/allegro/bigcache/v3 v3.0.2 - github.com/gorilla/mux v1.8.0 + github.com/PuerkitoBio/goquery v1.8.1 + github.com/allegro/bigcache/v3 v3.1.0 + github.com/caffix/cloudflare-roundtripper v0.0.0-20181218223503-4c29d231c9cb + github.com/gorilla/mux v1.8.1 github.com/sirupsen/logrus v1.9.3 ) require ( - github.com/andybalholm/cascadia v1.3.1 // indirect - github.com/caffix/cloudflare-roundtripper v0.0.0-20181218223503-4c29d231c9cb // indirect - github.com/robertkrimen/otto v0.2.1 // indirect - golang.org/x/net v0.0.0-20210916014120-12bc252f5db8 // indirect - golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect - golang.org/x/text v0.4.0 // indirect + github.com/andybalholm/cascadia v1.3.2 // indirect + github.com/robertkrimen/otto v0.3.0 // indirect + golang.org/x/net v0.20.0 // indirect + golang.org/x/sys v0.16.0 // indirect + golang.org/x/text v0.14.0 // indirect gopkg.in/sourcemap.v1 v1.0.5 // indirect ) diff --git a/go.sum b/go.sum index 3eb7ee1..e5e2388 100644 --- a/go.sum +++ b/go.sum @@ -1,43 +1,75 @@ -github.com/PuerkitoBio/goquery v1.8.0 h1:PJTF7AmFCFKk1N6V6jmKfrNH9tV5pNE6lZMkG0gta/U= -github.com/PuerkitoBio/goquery v1.8.0/go.mod h1:ypIiRMtY7COPGk+I/YbZLbxsxn9g5ejnI2HSMtkjZvI= -github.com/allegro/bigcache/v3 v3.0.2 h1:AKZCw+5eAaVyNTBmI2fgyPVJhHkdWder3O9IrprcQfI= -github.com/allegro/bigcache/v3 v3.0.2/go.mod h1:aPyh7jEvrog9zAwx5N7+JUQX5dZTSGpxF1LAR4dr35I= -github.com/andybalholm/cascadia v1.3.1 h1:nhxRkql1kdYCc8Snf7D5/D3spOX+dBgjA6u8x004T2c= +github.com/PuerkitoBio/goquery v1.8.1 h1:uQxhNlArOIdbrH1tr0UXwdVFgDcZDrZVdcpygAcwmWM= +github.com/PuerkitoBio/goquery v1.8.1/go.mod h1:Q8ICL1kNUJ2sXGoAhPGUdYDJvgQgHzJsnnd3H7Ho5jQ= +github.com/allegro/bigcache/v3 v3.1.0 h1:H2Vp8VOvxcrB91o86fUSVJFqeuz8kpyyB02eH3bSzwk= +github.com/allegro/bigcache/v3 v3.1.0/go.mod h1:aPyh7jEvrog9zAwx5N7+JUQX5dZTSGpxF1LAR4dr35I= github.com/andybalholm/cascadia v1.3.1/go.mod h1:R4bJ1UQfqADjvDa4P6HZHLh/3OxWWEqc0Sk8XGwHqvA= +github.com/andybalholm/cascadia v1.3.2 h1:3Xi6Dw5lHF15JtdcmAHD3i1+T8plmv7BQ/nsViSLyss= +github.com/andybalholm/cascadia v1.3.2/go.mod h1:7gtRlve5FxPPgIgX36uWBX58OdBsSS6lUvCFb+h7KvU= github.com/caffix/cloudflare-roundtripper v0.0.0-20181218223503-4c29d231c9cb h1:RKySaWkjoE/ECY1FFk4JbcfG9dTrJmoqp08rQ2oA51Y= github.com/caffix/cloudflare-roundtripper v0.0.0-20181218223503-4c29d231c9cb/go.mod h1:LkIRP8n1KY5Ew4Y7S+V7ooavIXNrraFZ1IKmI4SNMuE= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= -github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= +github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY= +github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/robertkrimen/otto v0.2.1 h1:FVP0PJ0AHIjC+N4pKCG9yCDz6LHNPCwi/GKID5pGGF0= -github.com/robertkrimen/otto v0.2.1/go.mod h1:UPwtJ1Xu7JrLcZjNWN8orJaM5n5YEtqL//farB5FlRY= -github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0= -github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= +github.com/robertkrimen/otto v0.3.0 h1:5RI+8860NSxvXywDY9ddF5HcPw0puRsd8EgbXV0oqRE= +github.com/robertkrimen/otto v0.3.0/go.mod h1:uW9yN1CYflmUQYvAMS0m+ZiNo3dMzRUDQJX0jWbzgxw= github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= -golang.org/x/net v0.0.0-20210916014120-12bc252f5db8 h1:/6y1LfuqNuQdHAm0jjtPtgRcxIxjVZgm5OTu8/QhZvk= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20210916014120-12bc252f5db8/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= +golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= +golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns= +golang.org/x/net v0.20.0 h1:aCL9BSgETF1k+blQaYUBx9hJ9LOGP3gAVemcZlf1Kpo= +golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f h1:v4INt8xihDGvnrfjMDVXGxw9wrfxYyCjk0KbXjhR55s= golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU= +golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= +golang.org/x/term v0.7.0/go.mod h1:P32HKFT3hSsZrRxla30E9HqToFYAQPCMs/zFMBUFqPY= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.4.0 h1:BrVqGRd7+k1DiOgtnFvAkoQEWQvBc25ouMJM6429SFg= -golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= +golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/sourcemap.v1 v1.0.5 h1:inv58fC9f9J3TK2Y2R1NPntXEn3/wjWHkonhIUODNTI= gopkg.in/sourcemap.v1 v1.0.5/go.mod h1:2RlvNNSMglmRrcvhfuzp4hQHwOtjxlbjX7UPY/GXb78= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= From 6c83f7d8fb02cf49982810f8df8689a9e143e50a Mon Sep 17 00:00:00 2001 From: monirzadeh <25131576+Monirzadeh@users.noreply.github.com> Date: Fri, 19 Jan 2024 23:52:10 +0330 Subject: [PATCH 3/9] NewBigCache deprecated so replace with New --- main.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 69799f9..1dde722 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "context" "fmt" "net" "net/http" @@ -16,7 +17,8 @@ import ( var logger = logrus.New() func main() { - c, err := bigcache.NewBigCache(bigcache.DefaultConfig(time.Hour * 24)) + ctx := context.Background() + c, err := bigcache.New(ctx, bigcache.DefaultConfig(time.Hour*24)) if err != nil { logger.Fatalln("can't initialize caching") } From 69988e11565102c03d02cdf720f89f9f2ba18eac Mon Sep 17 00:00:00 2001 From: monirzadeh <25131576+Monirzadeh@users.noreply.github.com> Date: Sat, 20 Jan 2024 00:00:53 +0330 Subject: [PATCH 4/9] add err handling for missing function --- album.go | 11 +++++++++-- annotations.go | 6 +++++- lyrics.go | 6 +++++- proxy.go | 6 +++++- search.go | 5 ++++- utils.go | 7 +++++-- 6 files changed, 33 insertions(+), 8 deletions(-) diff --git a/album.go b/album.go index e21d8a2..7f48982 100644 --- a/album.go +++ b/album.go @@ -55,7 +55,10 @@ func (a *album) parseAlbumData(doc *goquery.Document) { } var albumMetadataFromPage albumMetadata - json.Unmarshal([]byte(pageMetadata), &albumMetadataFromPage) + err := json.Unmarshal([]byte(pageMetadata), &albumMetadataFromPage) + if err != nil { + logger.Errorln(err) + } albumData := albumMetadataFromPage.Album a.Artist = albumData.Artist.Name @@ -135,5 +138,9 @@ func albumHandler(w http.ResponseWriter, r *http.Request) { render("album", w, a) - setCache(id, a) + err = setCache(id, a) + if err != nil { + logger.Errorln(err) + } + } diff --git a/annotations.go b/annotations.go index f77d35c..715c9ec 100644 --- a/annotations.go +++ b/annotations.go @@ -110,7 +110,11 @@ func annotationsHandler(w http.ResponseWriter, r *http.Request) { return } - setCache(id, body) + err = setCache(id, body) + if err != nil { + logger.Errorln(err) + } + _, err = w.Write(response) if err != nil { logger.Errorln("Error sending response: ", err) diff --git a/lyrics.go b/lyrics.go index 0fdcb8c..913844c 100644 --- a/lyrics.go +++ b/lyrics.go @@ -168,5 +168,9 @@ func lyricsHandler(w http.ResponseWriter, r *http.Request) { s.parse(doc) render("lyrics", w, s) - setCache(id, s) + err = setCache(id, s) + if err != nil { + logger.Errorln(err) + } + } diff --git a/proxy.go b/proxy.go index 006c932..bb7005a 100644 --- a/proxy.go +++ b/proxy.go @@ -69,5 +69,9 @@ func proxyHandler(w http.ResponseWriter, r *http.Request) { } w.Header().Add("Content-type", fmt.Sprintf("image/%s", ext)) - io.Copy(w, res.Body) + _, err = io.Copy(w, res.Body) + if err != nil { + logger.Errorln(err) + } + } diff --git a/search.go b/search.go index 1392e8d..cfd28ac 100644 --- a/search.go +++ b/search.go @@ -53,7 +53,10 @@ func searchHandler(w http.ResponseWriter, r *http.Request) { var data response d := json.NewDecoder(res.Body) - d.Decode(&data) + err = d.Decode(&data) + if err != nil { + logger.Errorln(err) + } vars := renderVars{query, data.Response.Sections} diff --git a/utils.go b/utils.go index 94a686a..a7ccc8a 100644 --- a/utils.go +++ b/utils.go @@ -42,7 +42,11 @@ func getCache(key string) (interface{}, error) { func write(w http.ResponseWriter, status int, data []byte) { w.WriteHeader(status) - w.Write(data) + _, err := w.Write(data) + if err != nil { + logger.Errorln(err) + } + } func securityHeaders(next http.Handler) http.Handler { @@ -105,7 +109,6 @@ func sendRequest(u string) (*http.Response, error) { return nil, err } - req := &http.Request{ Method: http.MethodGet, URL: url, From aea4fc4bab76d8723964fe62c8aa230efcde7387 Mon Sep 17 00:00:00 2001 From: monirzadeh <25131576+Monirzadeh@users.noreply.github.com> Date: Sat, 20 Jan 2024 00:03:31 +0330 Subject: [PATCH 5/9] update Readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 6323e61..b55e2eb 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ With the massive daily increase of useless scripts on Genius's web frontend and ![Screenshot](https://raw.githubusercontent.com/rramiachraf/dumb/main/screenshot.png) ## Installation & Usage -[Go 1.18+](https://go.dev/dl) is required. +[Go 1.21+](https://go.dev/dl) is required. ```bash git clone https://github.com/rramiachraf/dumb cd dumb From 3e50e52d5e24a3a20739228c38190a624c407430 Mon Sep 17 00:00:00 2001 From: rramiachraf <51409801+rramiachraf@users.noreply.github.com> Date: Sat, 13 Jul 2024 22:06:26 +0100 Subject: [PATCH 6/9] chore: second attempt to sync branch with latest changes --- album.go | 146 ---------------------------------------- annotations.go | 131 ------------------------------------ go.sum | 1 - lyrics.go | 176 ------------------------------------------------- main.go | 33 ---------- proxy.go | 77 ---------------------- search.go | 64 ------------------ 7 files changed, 628 deletions(-) delete mode 100644 album.go delete mode 100644 annotations.go delete mode 100644 lyrics.go delete mode 100644 proxy.go delete mode 100644 search.go diff --git a/album.go b/album.go deleted file mode 100644 index 7f48982..0000000 --- a/album.go +++ /dev/null @@ -1,146 +0,0 @@ -package main - -import ( - "encoding/json" - "fmt" - "net/http" - "strings" - - "github.com/PuerkitoBio/goquery" - "github.com/gorilla/mux" -) - -type album struct { - Artist string - Name string - Image string - About [2]string - - Tracks []Track -} - -type Track struct { - Title string - Url string -} - -type albumMetadata struct { - Album struct { - Id int `json:"id"` - Image string `json:"cover_art_thumbnail_url"` - Name string `json:"name"` - Description string `json:"description_preview"` - Artist `json:"artist"` - } - AlbumAppearances []AlbumAppearances `json:"album_appearances"` -} - -type AlbumAppearances struct { - Id int `json:"id"` - TrackNumber int `json:"track_number"` - Song struct { - Title string `json:"title"` - Url string `json:"url"` - } -} - -type Artist struct { - Name string `json:"name"` -} - -func (a *album) parseAlbumData(doc *goquery.Document) { - pageMetadata, exists := doc.Find("meta[itemprop='page_data']").Attr("content") - if !exists { - return - } - - var albumMetadataFromPage albumMetadata - err := json.Unmarshal([]byte(pageMetadata), &albumMetadataFromPage) - if err != nil { - logger.Errorln(err) - } - - albumData := albumMetadataFromPage.Album - a.Artist = albumData.Artist.Name - a.Name = albumData.Name - a.Image = albumData.Image - a.About[0] = albumData.Description - a.About[1] = truncateText(albumData.Description) - - for _, track := range albumMetadataFromPage.AlbumAppearances { - url := strings.Replace(track.Song.Url, "https://genius.com", "", -1) - a.Tracks = append(a.Tracks, Track{Title: track.Song.Title, Url: url}) - } -} - -func (a *album) parse(doc *goquery.Document) { - a.parseAlbumData(doc) -} - -func albumHandler(w http.ResponseWriter, r *http.Request) { - artist := mux.Vars(r)["artist"] - albumName := mux.Vars(r)["albumName"] - - id := fmt.Sprintf("%s/%s", artist, albumName) - - if data, err := getCache(id); err == nil { - render("album", w, data) - return - } - - url := fmt.Sprintf("https://genius.com/albums/%s/%s", artist, albumName) - - resp, err := sendRequest(url) - if err != nil { - logger.Errorln(err) - w.WriteHeader(http.StatusInternalServerError) - render("error", w, map[string]string{ - "Status": "500", - "Error": "cannot reach genius servers", - }) - return - } - - defer resp.Body.Close() - - if resp.StatusCode == http.StatusNotFound { - w.WriteHeader(http.StatusNotFound) - render("error", w, map[string]string{ - "Status": "404", - "Error": "page not found", - }) - return - } - - doc, err := goquery.NewDocumentFromReader(resp.Body) - if err != nil { - logger.Errorln(err) - w.WriteHeader(http.StatusInternalServerError) - render("error", w, map[string]string{ - "Status": "500", - "Error": "something went wrong", - }) - return - } - - cf := doc.Find(".cloudflare_content").Length() - if cf > 0 { - logger.Errorln("cloudflare got in the way") - render("error", w, map[string]string{ - "Status": "500", - "Error": "damn cloudflare, issue #21 on GitHub", - }) - return - } - - var a album - a.parse(doc) - - render("album", w, a) - - err = setCache(id, a) - if err != nil { - logger.Errorln(err) - } - -} diff --git a/annotations.go b/annotations.go deleted file mode 100644 index 715c9ec..0000000 --- a/annotations.go +++ /dev/null @@ -1,131 +0,0 @@ -package main - -import ( - "bytes" - "encoding/json" - "fmt" - "net/http" - "regexp" - "strings" - - "github.com/gorilla/mux" -) - -type annotationsResponse struct { - Response struct { - Referent struct { - Annotations []struct { - Body struct { - Html string `json:"html"` - } `json:"body"` - } `json:"annotations"` - } `json:"referent"` - } `json:"response"` -} - -func annotationsHandler(w http.ResponseWriter, r *http.Request) { - id := mux.Vars(r)["id"] - - if data, err := getCache(id); err == nil { - - response, err := json.Marshal(data) - - if err != nil { - logger.Errorf("could not marshal json: %s\n", err) - w.WriteHeader(http.StatusInternalServerError) - render("error", w, map[string]string{ - "Status": "500", - "Error": "Could not parse genius api response", - }) - return - } - w.Header().Set("content-type", "application/json") - _, err = w.Write(response) - if err != nil { - logger.Errorln("Error sending response: ", err) - } - return - } - - url := fmt.Sprintf("https://genius.com/api/referents/%s?text_format=html", id) - resp, err := sendRequest(url) - - if err != nil { - logger.Errorln(err) - w.WriteHeader(http.StatusInternalServerError) - render("error", w, map[string]string{ - "Status": "500", - "Error": "cannot reach genius servers", - }) - return - } - - defer resp.Body.Close() - - if resp.StatusCode == http.StatusNotFound { - w.WriteHeader(http.StatusNotFound) - render("error", w, map[string]string{ - "Status": "404", - "Error": "page not found", - }) - return - } - - buf := new(bytes.Buffer) - _, err = buf.ReadFrom(resp.Body) - if err != nil { - logger.Errorln("Error paring genius api response", err) - w.WriteHeader(http.StatusInternalServerError) - render("error", w, map[string]string{ - "Status": "500", - "Error": "Parsing error", - }) - return - } - - var data annotationsResponse - err = json.Unmarshal(buf.Bytes(), &data) - if err != nil { - logger.Errorf("could not unmarshal json: %s\n", err) - w.WriteHeader(http.StatusInternalServerError) - render("error", w, map[string]string{ - "Status": "500", - "Error": "Could not parse genius api response", - }) - return - } - - w.Header().Set("content-type", "application/json") - body := data.Response.Referent.Annotations[0].Body - body.Html = cleanBody(body.Html) - response, err := json.Marshal(body) - - if err != nil { - logger.Errorf("could not marshal json: %s\n", err) - w.WriteHeader(http.StatusInternalServerError) - render("error", w, map[string]string{ - "Status": "500", - "Error": "Could not parse genius api response", - }) - return - } - - err = setCache(id, body) - if err != nil { - logger.Errorln(err) - } - - _, err = w.Write(response) - if err != nil { - logger.Errorln("Error sending response: ", err) - } -} - -func cleanBody(body string) string { - var withCleanedImageLinks = strings.Replace(body, "https://images.rapgenius.com/", "/images/", -1) - - var re = regexp.MustCompile(`https?:\/\/[a-z]*.?genius.com`) - var withCleanedLinks = re.ReplaceAllString(withCleanedImageLinks, "") - - return withCleanedLinks -} diff --git a/go.sum b/go.sum index 2bf8f1d..2f38299 100644 --- a/go.sum +++ b/go.sum @@ -50,4 +50,3 @@ golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtn golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= - diff --git a/lyrics.go b/lyrics.go deleted file mode 100644 index 913844c..0000000 --- a/lyrics.go +++ /dev/null @@ -1,176 +0,0 @@ -package main - -import ( - "encoding/json" - "fmt" - "net/http" - "strings" - - "github.com/PuerkitoBio/goquery" - "github.com/gorilla/mux" -) - -type song struct { - Artist string - Title string - Image string - Lyrics string - Credits map[string]string - About [2]string - Album string - LinkToAlbum string -} - -type songResponse struct { - Response struct { - Song struct { - ArtistNames string `json:"artist_names"` - Image string `json:"song_art_image_thumbnail_url"` - Title string - Description struct { - Plain string - } - Album struct { - Url string `json:"url"` - Name string `json:"name"` - } - CustomPerformances []customPerformance `json:"custom_performances"` - } - } -} - -type customPerformance struct { - Label string - Artists []struct { - Name string - } -} - -func (s *song) parseLyrics(doc *goquery.Document) { - doc.Find("[data-lyrics-container='true']").Each(func(i int, ss *goquery.Selection) { - h, err := ss.Html() - if err != nil { - logger.Errorln("unable to parse lyrics", err) - } - s.Lyrics += h - }) -} - -func (s *song) parseSongData(doc *goquery.Document) { - attr, exists := doc.Find("meta[property='twitter:app:url:iphone']").Attr("content") - if exists { - songID := strings.Replace(attr, "genius://songs/", "", 1) - - u := fmt.Sprintf("https://genius.com/api/songs/%s?text_format=plain", songID) - - res, err := sendRequest(u) - if err != nil { - logger.Errorln(err) - } - - defer res.Body.Close() - - var data songResponse - decoder := json.NewDecoder(res.Body) - err = decoder.Decode(&data) - if err != nil { - logger.Errorln(err) - } - - songData := data.Response.Song - s.Artist = songData.ArtistNames - s.Image = songData.Image - s.Title = songData.Title - s.About[0] = songData.Description.Plain - s.About[1] = truncateText(songData.Description.Plain) - s.Credits = make(map[string]string) - s.Album = songData.Album.Name - s.LinkToAlbum = strings.Replace(songData.Album.Url, "https://genius.com", "", -1) - - for _, perf := range songData.CustomPerformances { - var artists []string - for _, artist := range perf.Artists { - artists = append(artists, artist.Name) - } - s.Credits[perf.Label] = strings.Join(artists, ", ") - } - } -} - -func truncateText(text string) string { - textArr := strings.Split(text, "") - - if len(textArr) > 250 { - return strings.Join(textArr[0:250], "") + "..." - } - - return text -} - -func (s *song) parse(doc *goquery.Document) { - s.parseLyrics(doc) - s.parseSongData(doc) -} - -func lyricsHandler(w http.ResponseWriter, r *http.Request) { - id := mux.Vars(r)["id"] - - if data, err := getCache(id); err == nil { - render("lyrics", w, data) - return - } - - url := fmt.Sprintf("https://genius.com/%s-lyrics", id) - resp, err := sendRequest(url) - if err != nil { - logger.Errorln(err) - w.WriteHeader(http.StatusInternalServerError) - render("error", w, map[string]string{ - "Status": "500", - "Error": "cannot reach genius servers", - }) - return - } - - defer resp.Body.Close() - - if resp.StatusCode == http.StatusNotFound { - w.WriteHeader(http.StatusNotFound) - render("error", w, map[string]string{ - "Status": "404", - "Error": "page not found", - }) - return - } - - doc, err := goquery.NewDocumentFromReader(resp.Body) - if err != nil { - logger.Errorln(err) - w.WriteHeader(http.StatusInternalServerError) - render("error", w, map[string]string{ - "Status": "500", - "Error": "something went wrong", - }) - return - } - - cf := doc.Find(".cloudflare_content").Length() - if cf > 0 { - logger.Errorln("cloudflare got in the way") - render("error", w, map[string]string{ - "Status": "500", - "Error": "damn cloudflare, issue #21 on GitHub", - }) - return - } - - var s song - s.parse(doc) - - render("lyrics", w, s) - err = setCache(id, s) - if err != nil { - logger.Errorln(err) - } - -} diff --git a/main.go b/main.go index 760d907..872745f 100644 --- a/main.go +++ b/main.go @@ -1,11 +1,7 @@ package main import ( -<<<<<<< HEAD - "context" -======= "embed" ->>>>>>> upstream/main "fmt" "net" "net/http" @@ -22,36 +18,7 @@ import ( var staticFiles embed.FS func main() { -<<<<<<< HEAD - ctx := context.Background() - c, err := bigcache.New(ctx, bigcache.DefaultConfig(time.Hour*24)) - if err != nil { - logger.Fatalln("can't initialize caching") - } - cache = c - - r := mux.NewRouter() - - r.Use(securityHeaders) - - r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { render("home", w, nil) }) - r.HandleFunc("/search", searchHandler).Methods("GET") - r.HandleFunc("/{id}-lyrics", lyricsHandler) - r.HandleFunc("/{id}/{artist-song}/{verse}/annotations", annotationsHandler) - r.HandleFunc("/images/{filename}.{ext}", proxyHandler) - r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("static")))) - r.HandleFunc("/albums/{artist}/{albumName}", albumHandler) - r.NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(http.StatusNotFound) - render("error", w, map[string]string{ - "Status": "404", - "Error": "page not found", - }) - - }) -======= logger := utils.NewLogger(os.Stdout) ->>>>>>> upstream/main server := &http.Server{ Handler: handlers.New(logger, staticFiles), diff --git a/proxy.go b/proxy.go deleted file mode 100644 index bb7005a..0000000 --- a/proxy.go +++ /dev/null @@ -1,77 +0,0 @@ -package main - -import ( - "fmt" - "io" - "net/http" - "net/url" - "strings" - - "github.com/gorilla/mux" -) - -func isValidExt(ext string) bool { - valid := []string{"jpg", "jpeg", "png", "gif"} - for _, c := range valid { - if strings.ToLower(ext) == c { - return true - } - } - - return false -} - -func extractURL(image string) string { - u, err := url.Parse(image) - if err != nil { - return "" - } - - return fmt.Sprintf("/images%s", u.Path) -} - -func proxyHandler(w http.ResponseWriter, r *http.Request) { - v := mux.Vars(r) - f := v["filename"] - ext := v["ext"] - - if !isValidExt(ext) { - w.WriteHeader(http.StatusBadRequest) - render("error", w, map[string]string{ - "Status": "400", - "Error": "Something went wrong", - }) - return - } - - // first segment of URL resize the image to reduce bandwith usage. - url := fmt.Sprintf("https://t2.genius.com/unsafe/300x300/https://images.genius.com/%s.%s", f, ext) - - res, err := sendRequest(url) - if err != nil { - logger.Errorln(err) - w.WriteHeader(http.StatusInternalServerError) - render("error", w, map[string]string{ - "Status": "500", - "Error": "cannot reach genius servers", - }) - return - } - - if res.StatusCode != http.StatusOK { - w.WriteHeader(http.StatusInternalServerError) - render("error", w, map[string]string{ - "Status": "500", - "Error": "something went wrong", - }) - - return - } - - w.Header().Add("Content-type", fmt.Sprintf("image/%s", ext)) - _, err = io.Copy(w, res.Body) - if err != nil { - logger.Errorln(err) - } - -} diff --git a/search.go b/search.go deleted file mode 100644 index cfd28ac..0000000 --- a/search.go +++ /dev/null @@ -1,64 +0,0 @@ -package main - -import ( - "encoding/json" - "fmt" - "net/http" - "net/url" -) - -type response struct { - Response struct { - Sections sections - } -} - -type result struct { - ArtistNames string `json:"artist_names"` - Title string - Path string - Thumbnail string `json:"song_art_image_thumbnail_url"` -} - -type hits []struct { - Result result -} - -type sections []struct { - Type string - Hits hits -} - -type renderVars struct { - Query string - Sections sections -} - -func searchHandler(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 { - logger.Errorln(err) - w.WriteHeader(http.StatusInternalServerError) - render("error", w, map[string]string{ - "Status": "500", - "Error": "cannot reach genius servers", - }) - } - - defer res.Body.Close() - - var data response - - d := json.NewDecoder(res.Body) - err = d.Decode(&data) - if err != nil { - logger.Errorln(err) - } - - vars := renderVars{query, data.Response.Sections} - - render("search", w, vars) -} From ea14549d6fba1d50696adf7b5da8cbb18e0a8dd0 Mon Sep 17 00:00:00 2001 From: rramiachraf <51409801+rramiachraf@users.noreply.github.com> Date: Sat, 13 Jul 2024 22:23:04 +0100 Subject: [PATCH 7/9] ci: refactor linter workflow --- .github/workflows/ci.yml | 27 --------------------------- .github/workflows/linter.yml | 26 ++++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 27 deletions(-) delete mode 100644 .github/workflows/ci.yml create mode 100644 .github/workflows/linter.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml deleted file mode 100644 index 2ea48da..0000000 --- a/.github/workflows/ci.yml +++ /dev/null @@ -1,27 +0,0 @@ -# Source: https://github.com/actions/starter-workflows/blob/main/ci/go.yml -name: CI - -on: - push: - branches: [ main ] - pull_request: - branches: [ main ] - -permissions: - contents: read - -jobs: - # Test golangci-lint for go-version define in go.mod - golangci: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac #v4.0.0 - - uses: actions/setup-go@93397bea11091df50f3d7e59dc26a7711a8bcfbe #v4.1.0 - with: - go-version-file: go.mod - cache: false - - name: golangci-lint - uses: golangci/golangci-lint-action@3a919529898de77ec3da873e3063ca4b10e7f5cc #v3.7.0 - with: - version: latest - only-new-issues: true diff --git a/.github/workflows/linter.yml b/.github/workflows/linter.yml new file mode 100644 index 0000000..df5c4a2 --- /dev/null +++ b/.github/workflows/linter.yml @@ -0,0 +1,26 @@ +name: linter + +on: + push: + branches: [ main ] + pull_request: + branches: [ main ] + +permissions: + contents: read + +jobs: + golangci: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-go@v5 + with: + go-version-file: go.mod + - run: go install github.com/a-h/templ/cmd/templ@latest + - run: make build + - name: golangci-lint + uses: golangci/golangci-lint-action@v6.0.1 + with: + version: latest + only-new-issues: true From b6cae441b36d1957f3e308484b928a564db55978 Mon Sep 17 00:00:00 2001 From: rramiachraf <51409801+rramiachraf@users.noreply.github.com> Date: Sun, 14 Jul 2024 00:43:06 +0100 Subject: [PATCH 8/9] refactor: fix linter errors --- handlers/album.go | 13 ++++++------- handlers/annotations.go | 9 ++++----- handlers/article.go | 13 ++++++------- handlers/artist.go | 13 ++++++------- handlers/handler.go | 3 +-- handlers/lyrics.go | 14 +++++++------- handlers/proxy.go | 7 +++---- handlers/search.go | 7 +++---- utils/template.go | 19 +++++++++++++++++++ 9 files changed, 55 insertions(+), 43 deletions(-) create mode 100644 utils/template.go diff --git a/handlers/album.go b/handlers/album.go index 8aa05d2..a48a7f7 100644 --- a/handlers/album.go +++ b/handlers/album.go @@ -1,7 +1,6 @@ package handlers import ( - "context" "fmt" "net/http" @@ -20,7 +19,7 @@ func album(l *utils.Logger) http.HandlerFunc { id := fmt.Sprintf("%s/%s", artist, albumName) if a, err := getCache[data.Album](id); err == nil { - views.AlbumPage(a).Render(context.Background(), w) + utils.RenderTemplate(w, views.AlbumPage(a), l) return } @@ -30,7 +29,7 @@ func album(l *utils.Logger) http.HandlerFunc { if err != nil { l.Error(err.Error()) w.WriteHeader(http.StatusInternalServerError) - views.ErrorPage(500, "cannot reach Genius servers").Render(context.Background(), w) + utils.RenderTemplate(w, views.ErrorPage(500, "cannot reach Genius servers"), l) return } @@ -38,7 +37,7 @@ func album(l *utils.Logger) http.HandlerFunc { if resp.StatusCode == http.StatusNotFound { w.WriteHeader(http.StatusNotFound) - views.ErrorPage(404, "page not found").Render(context.Background(), w) + utils.RenderTemplate(w, views.ErrorPage(404, "page not found"), l) return } @@ -46,14 +45,14 @@ func album(l *utils.Logger) http.HandlerFunc { if err != nil { l.Error(err.Error()) w.WriteHeader(http.StatusInternalServerError) - views.ErrorPage(500, "something went wrong").Render(context.Background(), w) + utils.RenderTemplate(w, views.ErrorPage(500, "something went wrong"), l) 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) + utils.RenderTemplate(w, views.ErrorPage(500, "cloudflare is detected"), l) return } @@ -62,7 +61,7 @@ func album(l *utils.Logger) http.HandlerFunc { l.Error(err.Error()) } - views.AlbumPage(a).Render(context.Background(), w) + utils.RenderTemplate(w, views.AlbumPage(a), l) if err = setCache(id, a); err != nil { l.Error(err.Error()) diff --git a/handlers/annotations.go b/handlers/annotations.go index 137bab3..47ddbae 100644 --- a/handlers/annotations.go +++ b/handlers/annotations.go @@ -2,7 +2,6 @@ package handlers import ( "bytes" - "context" "encoding/json" "fmt" "net/http" @@ -33,7 +32,7 @@ func annotations(l *utils.Logger) http.HandlerFunc { if err != nil { l.Error(err.Error()) w.WriteHeader(http.StatusInternalServerError) - views.ErrorPage(500, "cannot reach genius servers").Render(context.Background(), w) + utils.RenderTemplate(w, views.ErrorPage(500, "cannot reach genius servers"), l) return } @@ -41,7 +40,7 @@ func annotations(l *utils.Logger) http.HandlerFunc { if resp.StatusCode == http.StatusNotFound { w.WriteHeader(http.StatusNotFound) - views.ErrorPage(404, "page not found").Render(context.Background(), w) + utils.RenderTemplate(w, views.ErrorPage(404, "page not found"), l) return } @@ -50,7 +49,7 @@ func annotations(l *utils.Logger) http.HandlerFunc { if err != nil { l.Error("Error paring genius api response: %s", err.Error()) w.WriteHeader(http.StatusInternalServerError) - views.ErrorPage(500, "something went wrong").Render(context.Background(), w) + utils.RenderTemplate(w, views.ErrorPage(500, "something went wrong"), l) return } @@ -59,7 +58,7 @@ func annotations(l *utils.Logger) http.HandlerFunc { if err != nil { l.Error("could not unmarshal json: %s\n", err) w.WriteHeader(http.StatusInternalServerError) - views.ErrorPage(500, "something went wrong").Render(context.Background(), w) + utils.RenderTemplate(w, views.ErrorPage(500, "something went wrong"), l) return } diff --git a/handlers/article.go b/handlers/article.go index 08b5e22..11b8dda 100644 --- a/handlers/article.go +++ b/handlers/article.go @@ -1,7 +1,6 @@ package handlers import ( - "context" "fmt" "net/http" @@ -17,7 +16,7 @@ func article(l *utils.Logger) http.HandlerFunc { articleSlug := mux.Vars(r)["article"] if a, err := getCache[data.Article](articleSlug); err == nil { - views.ArticlePage(a).Render(context.Background(), w) + utils.RenderTemplate(w, views.ArticlePage(a), l) return } @@ -27,7 +26,7 @@ func article(l *utils.Logger) http.HandlerFunc { if err != nil { l.Error(err.Error()) w.WriteHeader(http.StatusInternalServerError) - views.ErrorPage(500, "cannot reach Genius servers").Render(context.Background(), w) + utils.RenderTemplate(w, views.ErrorPage(500, "cannot reach Genius servers"), l) return } @@ -35,7 +34,7 @@ func article(l *utils.Logger) http.HandlerFunc { if resp.StatusCode == http.StatusNotFound { w.WriteHeader(http.StatusNotFound) - views.ErrorPage(404, "page not found").Render(context.Background(), w) + utils.RenderTemplate(w, views.ErrorPage(404, "page not found"), l) return } @@ -43,14 +42,14 @@ func article(l *utils.Logger) http.HandlerFunc { if err != nil { l.Error(err.Error()) w.WriteHeader(http.StatusInternalServerError) - views.ErrorPage(500, "something went wrong").Render(context.Background(), w) + utils.RenderTemplate(w, views.ErrorPage(500, "something went wrong"), l) 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) + utils.RenderTemplate(w, views.ErrorPage(500, "cloudflare is detected"), l) return } @@ -59,7 +58,7 @@ func article(l *utils.Logger) http.HandlerFunc { l.Error(err.Error()) } - views.ArticlePage(a).Render(context.Background(), w) + utils.RenderTemplate(w, views.ArticlePage(a), l) if err = setCache(articleSlug, a); err != nil { l.Error(err.Error()) diff --git a/handlers/artist.go b/handlers/artist.go index 4406133..113a65e 100644 --- a/handlers/artist.go +++ b/handlers/artist.go @@ -1,7 +1,6 @@ package handlers import ( - "context" "fmt" "net/http" @@ -19,7 +18,7 @@ func artist(l *utils.Logger) http.HandlerFunc { id := fmt.Sprintf("artist:%s", artistName) if a, err := getCache[data.Artist](id); err == nil { - views.ArtistPage(a).Render(context.Background(), w) + utils.RenderTemplate(w, views.ArtistPage(a), l) return } @@ -29,7 +28,7 @@ func artist(l *utils.Logger) http.HandlerFunc { if err != nil { l.Error(err.Error()) w.WriteHeader(http.StatusInternalServerError) - views.ErrorPage(500, "cannot reach Genius servers").Render(context.Background(), w) + utils.RenderTemplate(w, views.ErrorPage(500, "cannot reach Genius servers"), l) return } @@ -37,7 +36,7 @@ func artist(l *utils.Logger) http.HandlerFunc { if resp.StatusCode == http.StatusNotFound { w.WriteHeader(http.StatusNotFound) - views.ErrorPage(404, "page not found").Render(context.Background(), w) + utils.RenderTemplate(w, views.ErrorPage(404, "page not found"), l) return } @@ -45,14 +44,14 @@ func artist(l *utils.Logger) http.HandlerFunc { if err != nil { l.Error(err.Error()) w.WriteHeader(http.StatusInternalServerError) - views.ErrorPage(500, "something went wrong").Render(context.Background(), w) + utils.RenderTemplate(w, views.ErrorPage(500, "something went wrong"), l) 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) + utils.RenderTemplate(w, views.ErrorPage(500, "cloudflare is detected"), l) return } @@ -61,7 +60,7 @@ func artist(l *utils.Logger) http.HandlerFunc { l.Error(err.Error()) } - views.ArtistPage(a).Render(context.Background(), w) + utils.RenderTemplate(w, views.ArtistPage(a), l) if err = setCache(id, a); err != nil { l.Error(err.Error()) diff --git a/handlers/handler.go b/handlers/handler.go index 62e0f57..367741e 100644 --- a/handlers/handler.go +++ b/handlers/handler.go @@ -1,7 +1,6 @@ package handlers import ( - "context" "net/http" "github.com/a-h/templ" @@ -45,7 +44,7 @@ func New(logger *utils.Logger, staticFiles static) *mux.Router { r.NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNotFound) - views.ErrorPage(404, "page not found").Render(context.Background(), w) + utils.RenderTemplate(w, views.ErrorPage(404, "page not found"), logger) }) return r diff --git a/handlers/lyrics.go b/handlers/lyrics.go index 8322d58..65906b3 100644 --- a/handlers/lyrics.go +++ b/handlers/lyrics.go @@ -1,7 +1,6 @@ package handlers import ( - "context" "fmt" "net/http" @@ -23,7 +22,7 @@ func lyrics(l *utils.Logger) http.HandlerFunc { } if s, err := getCache[data.Song](id); err == nil { - views.LyricsPage(s).Render(context.Background(), w) + utils.RenderTemplate(w, views.LyricsPage(s), l) return } @@ -32,7 +31,7 @@ func lyrics(l *utils.Logger) http.HandlerFunc { if err != nil { l.Error(err.Error()) w.WriteHeader(http.StatusInternalServerError) - views.ErrorPage(500, "cannot reach Genius servers").Render(context.Background(), w) + utils.RenderTemplate(w, views.ErrorPage(500, "cannot reach Genius servers"), l) return } @@ -40,7 +39,7 @@ func lyrics(l *utils.Logger) http.HandlerFunc { if resp.StatusCode == http.StatusNotFound { w.WriteHeader(http.StatusNotFound) - views.ErrorPage(404, "page not found").Render(context.Background(), w) + utils.RenderTemplate(w, views.ErrorPage(404, "page not found"), l) return } @@ -48,14 +47,14 @@ func lyrics(l *utils.Logger) http.HandlerFunc { if err != nil { l.Error(err.Error()) w.WriteHeader(http.StatusInternalServerError) - views.ErrorPage(500, "something went wrong").Render(context.Background(), w) + utils.RenderTemplate(w, views.ErrorPage(500, "something went wrong"), l) 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) + utils.RenderTemplate(w, views.ErrorPage(500, "cloudflare is detected"), l) return } @@ -64,7 +63,8 @@ func lyrics(l *utils.Logger) http.HandlerFunc { l.Error(err.Error()) } - views.LyricsPage(s).Render(context.Background(), w) + utils.RenderTemplate(w, views.LyricsPage(s), l) + if err = setCache(id, s); err != nil { l.Error(err.Error()) } diff --git a/handlers/proxy.go b/handlers/proxy.go index b85ff82..6eae13c 100644 --- a/handlers/proxy.go +++ b/handlers/proxy.go @@ -1,7 +1,6 @@ package handlers import ( - "context" "fmt" "io" "mime" @@ -35,7 +34,7 @@ func imageProxy(l *utils.Logger) http.HandlerFunc { if !isValidExt(ext) { w.WriteHeader(http.StatusBadRequest) - views.ErrorPage(400, "something went wrong").Render(context.Background(), w) + utils.RenderTemplate(w, views.ErrorPage(400, "something went wrong"), l) return } @@ -46,13 +45,13 @@ func imageProxy(l *utils.Logger) http.HandlerFunc { if err != nil { l.Error(err.Error()) w.WriteHeader(http.StatusInternalServerError) - views.ErrorPage(500, "cannot reach Genius servers").Render(context.Background(), w) + utils.RenderTemplate(w, views.ErrorPage(500, "cannot reach Genius servers"), l) return } if res.StatusCode != http.StatusOK { w.WriteHeader(http.StatusInternalServerError) - views.ErrorPage(500, "something went wrong").Render(context.Background(), w) + utils.RenderTemplate(w, views.ErrorPage(500, "something went wrong"), l) return } diff --git a/handlers/search.go b/handlers/search.go index c662b04..84dc494 100644 --- a/handlers/search.go +++ b/handlers/search.go @@ -1,7 +1,6 @@ package handlers import ( - "context" "encoding/json" "fmt" "net/http" @@ -21,7 +20,7 @@ func search(l *utils.Logger) http.HandlerFunc { if err != nil { l.Error(err.Error()) w.WriteHeader(http.StatusInternalServerError) - views.ErrorPage(500, "cannot reach Genius servers").Render(context.Background(), w) + utils.RenderTemplate(w, views.ErrorPage(500, "cannot reach Genius servers"), l) return } @@ -33,12 +32,12 @@ func search(l *utils.Logger) http.HandlerFunc { if err = d.Decode(&sRes); err != nil { l.Error(err.Error()) w.WriteHeader(http.StatusInternalServerError) - views.ErrorPage(500, "something went wrong").Render(context.Background(), w) + utils.RenderTemplate(w, views.ErrorPage(500, "something went wrong"), l) } results := data.SearchResults{Query: query, Sections: sRes.Response.Sections} - views.SearchPage(results).Render(context.Background(), w) + utils.RenderTemplate(w, views.SearchPage(results), l) } } diff --git a/utils/template.go b/utils/template.go new file mode 100644 index 0000000..91c8bb7 --- /dev/null +++ b/utils/template.go @@ -0,0 +1,19 @@ +package utils + +import ( + "context" + "net/http" + + "github.com/a-h/templ" +) + +func RenderTemplate(w http.ResponseWriter, t templ.Component, l *Logger) { + if err := t.Render(context.Background(), w); err != nil { + l.Error("unable to render template %s", err) + w.WriteHeader(http.StatusInternalServerError) + _, err := w.Write([]byte{}) + if err != nil { + l.Error(err.Error()) + } + } +} From eccbf0957e6e6088d96811a3a8599e141dbfd6bb Mon Sep 17 00:00:00 2001 From: rramiachraf <51409801+rramiachraf@users.noreply.github.com> Date: Sun, 14 Jul 2024 01:00:30 +0100 Subject: [PATCH 9/9] ci: refactor dependabot.yml --- .github/dependabot.yml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.github/dependabot.yml b/.github/dependabot.yml index 7151303..fba8a54 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -1,12 +1,10 @@ -# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates - version: 2 updates: # Maintain dependencies for GitHub Actions - package-ecosystem: "github-actions" directory: "/" schedule: - interval: "weekly" + interval: "monthly" groups: all: patterns: