diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..175794b --- /dev/null +++ b/go.mod @@ -0,0 +1,30 @@ +module gitlab.com/durfy/durpify + +go 1.25 + +require ( + github.com/MicahParks/keyfunc v1.9.0 + github.com/a-h/templ v0.3.960 + github.com/charmbracelet/log v0.4.2 + github.com/golang-jwt/jwt/v4 v4.5.2 + gitlab.com/durfy/durpify/handlers v0.0.0-20250708105701-c59d09e4c9da + gopkg.in/yaml.v3 v3.0.1 +) + +require ( + github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect + github.com/charmbracelet/colorprofile v0.2.3-0.20250311203215-f60798e515dc // indirect + github.com/charmbracelet/lipgloss v1.1.0 // indirect + github.com/charmbracelet/x/ansi v0.8.0 // indirect + github.com/charmbracelet/x/cellbuf v0.0.13-0.20250311204145-2c3ea96c31dd // indirect + github.com/charmbracelet/x/term v0.2.1 // indirect + github.com/go-logfmt/logfmt v0.6.0 // indirect + github.com/lucasb-eyer/go-colorful v1.2.0 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/mattn/go-runewidth v0.0.16 // indirect + github.com/muesli/termenv v0.16.0 // indirect + github.com/rivo/uniseg v0.4.7 // indirect + github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect + golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect + golang.org/x/sys v0.34.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..a1211a4 --- /dev/null +++ b/go.sum @@ -0,0 +1,55 @@ +github.com/MicahParks/keyfunc v1.9.0 h1:lhKd5xrFHLNOWrDc4Tyb/Q1AJ4LCzQ48GVJyVIID3+o= +github.com/MicahParks/keyfunc v1.9.0/go.mod h1:IdnCilugA0O/99dW+/MkvlyrsX8+L8+x95xuVNtM5jw= +github.com/a-h/templ v0.3.960 h1:trshEpGa8clF5cdI39iY4ZrZG8Z/QixyzEyUnA7feTM= +github.com/a-h/templ v0.3.960/go.mod h1:oCZcnKRf5jjsGpf2yELzQfodLphd2mwecwG4Crk5HBo= +github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k= +github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8= +github.com/charmbracelet/colorprofile v0.2.3-0.20250311203215-f60798e515dc h1:4pZI35227imm7yK2bGPcfpFEmuY1gc2YSTShr4iJBfs= +github.com/charmbracelet/colorprofile v0.2.3-0.20250311203215-f60798e515dc/go.mod h1:X4/0JoqgTIPSFcRA/P6INZzIuyqdFY5rm8tb41s9okk= +github.com/charmbracelet/lipgloss v1.1.0 h1:vYXsiLHVkK7fp74RkV7b2kq9+zDLoEU4MZoFqR/noCY= +github.com/charmbracelet/lipgloss v1.1.0/go.mod h1:/6Q8FR2o+kj8rz4Dq0zQc3vYf7X+B0binUUBwA0aL30= +github.com/charmbracelet/log v0.4.2 h1:hYt8Qj6a8yLnvR+h7MwsJv/XvmBJXiueUcI3cIxsyig= +github.com/charmbracelet/log v0.4.2/go.mod h1:qifHGX/tc7eluv2R6pWIpyHDDrrb/AG71Pf2ysQu5nw= +github.com/charmbracelet/x/ansi v0.8.0 h1:9GTq3xq9caJW8ZrBTe0LIe2fvfLR/bYXKTx2llXn7xE= +github.com/charmbracelet/x/ansi v0.8.0/go.mod h1:wdYl/ONOLHLIVmQaxbIYEC/cRKOQyjTkowiI4blgS9Q= +github.com/charmbracelet/x/cellbuf v0.0.13-0.20250311204145-2c3ea96c31dd h1:vy0GVL4jeHEwG5YOXDmi86oYw2yuYUGqz6a8sLwg0X8= +github.com/charmbracelet/x/cellbuf v0.0.13-0.20250311204145-2c3ea96c31dd/go.mod h1:xe0nKWGd3eJgtqZRaN9RjMtK7xUYchjzPr7q6kcvCCs= +github.com/charmbracelet/x/term v0.2.1 h1:AQeHeLZ1OqSXhrAWpYUtZyX1T3zVxfpZuEQMIQaGIAQ= +github.com/charmbracelet/x/term v0.2.1/go.mod h1:oQ4enTYFV7QN4m0i9mzHrViD7TQKvNEEkHUMCmsxdUg= +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/go-logfmt/logfmt v0.6.0 h1:wGYYu3uicYdqXVgoYbvnkrPVXkuLM1p1ifugDMEdRi4= +github.com/go-logfmt/logfmt v0.6.0/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs= +github.com/golang-jwt/jwt/v4 v4.4.2/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0= +github.com/golang-jwt/jwt/v4 v4.5.2 h1:YtQM7lnr8iZ+j5q71MGKkNw9Mn7AjHM68uc9g5fXeUI= +github.com/golang-jwt/jwt/v4 v4.5.2/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= +github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc= +github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= +github.com/muesli/termenv v0.16.0 h1:S5AlUN9dENB57rsbnkPyfdGuWIlkmzJjbFf0Tf5FWUc= +github.com/muesli/termenv v0.16.0/go.mod h1:ZRfOIKPFDYQoDFF4Olj7/QJbW60Ol/kL1pU3VfY/Cnk= +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/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= +github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= +github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= +github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= +github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no= +github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM= +gitlab.com/durfy/durpify/handlers v0.0.0-20250708105701-c59d09e4c9da h1:iOtV2x/VSKDOlGPkNiOErIs4U3LjFDw9BBzyMnE9cZI= +gitlab.com/durfy/durpify/handlers v0.0.0-20250708105701-c59d09e4c9da/go.mod h1:l/gPuZkaNBq2bKYCUs9uMHOqsxpmDqpn6bMFplZxz1w= +golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI= +golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.34.0 h1:H5Y5sJ2L2JRdyv7ROF1he/lPdvFsd0mJHFw2ThKHxLA= +golang.org/x/sys v0.34.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/go.work b/go.work deleted file mode 100644 index fb9c91c..0000000 --- a/go.work +++ /dev/null @@ -1,4 +0,0 @@ -go 1.23.0 - -use ./handlers -use ./middleware \ No newline at end of file diff --git a/go.work.sum b/go.work.sum index 6762650..99f9f36 100644 --- a/go.work.sum +++ b/go.work.sum @@ -1,15 +1,29 @@ +github.com/a-h/htmlformat v0.0.0-20250209131833-673be874c677 h1:H1EoxMpNo/TnCZEgSaKsomi+dQ05dXiVSKDN86Lap9A= +github.com/a-h/htmlformat v0.0.0-20250209131833-673be874c677/go.mod h1:FMIm5afKmEfarNbIXOaPHFY8X7fo+fRQB6I9MPG2nB0= +github.com/a-h/parse v0.0.0-20250122154542-74294addb73e h1:HjVbSQHy+dnlS6C3XajZ69NYAb5jbGNfHanvm1+iYlo= +github.com/a-h/parse v0.0.0-20250122154542-74294addb73e/go.mod h1:3mnrkvGpurZ4ZrTDbYU84xhwXW2TjTKShSwjRi2ihfQ= github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d h1:licZJFw2RwpHMqeKTCYkitsPqHNxTmd4SNR5r94FGM8= github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d/go.mod h1:asat636LX7Bqt5lYEZ27JNDcqxfjdBQuJ/MM4CN/Lzo= +github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M= +github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY= github.com/aymanbagabas/go-udiff v0.2.0 h1:TK0fH4MteXUDspT88n8CKzvK0X9O2xu9yQjWpi6yML8= github.com/aymanbagabas/go-udiff v0.2.0/go.mod h1:RE4Ex0qsGkTAJoQdQQCA0uG+nAzJO/pI/QwceO5fgrA= +github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8= +github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE= github.com/charmbracelet/lipgloss v0.10.0 h1:KWeXFSexGcfahHX+54URiZGkBFazf70JNMtwg/AFW3s= github.com/charmbracelet/lipgloss v0.10.0/go.mod h1:Wig9DSfvANsxqkRsqj6x87irdy123SR4dOXlKa91ciE= github.com/charmbracelet/x/exp/golden v0.0.0-20240806155701-69247e0abc2a h1:G99klV19u0QnhiizODirwVksQB91TJKV/UaTnACcG30= github.com/charmbracelet/x/exp/golden v0.0.0-20240806155701-69247e0abc2a/go.mod h1:wDlXFlCrmJ8J+swcL/MnGUuYnqgQdW9rhSD61oNMb6U= +github.com/cli/browser v1.3.0 h1:LejqCrpWr+1pRqmEPDGnTZOjsMe7sehifLynZJuqJpo= +github.com/cli/browser v1.3.0/go.mod h1:HH8s+fOAxjhQoBUAsKuPCbqUuxZDhQ2/aD+SzsEfBTk= +github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM= +github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE= +github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= +github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg= github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= -github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= -github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= +github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98= github.com/mattn/go-isatty v0.0.18/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk= @@ -17,18 +31,32 @@ github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZ github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s= github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8= +github.com/natefinch/atomic v1.0.1 h1:ZPYKxkqQOx3KZ+RsbnP/YsgvxWQPGxjC0oBt2AhwV0A= +github.com/natefinch/atomic v1.0.1/go.mod h1:N/D/ELrljoqDyT3rZrsUmtsuzvHkeB/wWjHV22AZRbM= github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= -golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI= -golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo= +github.com/rs/cors v1.11.0 h1:0B9GE/r9Bc2UxRMMtymBkHTenPkHDv0CW4Y98GBY+po= +github.com/rs/cors v1.11.0/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= golang.org/x/mod v0.13.0 h1:I/DsJXRlw/8l/0c24sM9yb0T4z9liZTduXvdAWYiysY= golang.org/x/mod v0.13.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= golang.org/x/mod v0.20.0 h1:utOm6MM3R3dnawAiJgn0y+xvuYRsm1RKM/4giyfDgV0= golang.org/x/mod v0.20.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= +golang.org/x/mod v0.24.0 h1:ZfthKaKaT4NrhGVZHO1/WDTwGES4De8KtWO0SIbNJMU= +golang.org/x/mod v0.24.0/go.mod h1:IXM97Txy2VM4PJ3gI61r1YEk/gAj6zAHN3AdZt6S9Ww= +golang.org/x/net v0.39.0 h1:ZCu7HMWDxpXpaiKdhzIfaltL9Lp31x/3fCP11bc6/fY= +golang.org/x/net v0.39.0/go.mod h1:X7NRbYVEA+ewNkCNyJ513WmMdQ3BineSwVtN2zD/d+E= golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ= golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sync v0.13.0 h1:AauUjRAJ9OSnvULf/ARrrVywoJDy0YS2AwQ98I37610= +golang.org/x/sync v0.13.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA= golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE= golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/telemetry v0.0.0-20240521205824-bda55230c457/go.mod h1:pRgIJT+bRLFKnoM1ldnzKoxTIn14Yxz928LQRYYgIN0= +golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58= golang.org/x/tools v0.14.0 h1:jvNa2pY0M4r62jkRQ6RwEZZyPcymeL9XZMLBbV7U2nc= golang.org/x/tools v0.14.0/go.mod h1:uYBEerGOWcJyEORxN+Ek8+TT266gXkNlHdJBwexUsBg= golang.org/x/tools v0.24.0 h1:J1shsA93PJUEVaUSaay7UXAyE8aimq3GW0pjlolpa24= golang.org/x/tools v0.24.0/go.mod h1:YhNqVBIfWHdzvTLs0d8LCuMhkKUgSUKldakyV7W/WDQ= +golang.org/x/tools v0.32.0 h1:Q7N1vhpkQv7ybVzLFtTjvQya2ewbwNDZzUgfXGqtMWU= +golang.org/x/tools v0.32.0/go.mod h1:ZxrU41P/wAbZD8EDa6dDCa6XfpkhJ7HFMjHJXfBDu8s= diff --git a/handlers/go.mod b/handlers/go.mod deleted file mode 100644 index dfb288c..0000000 --- a/handlers/go.mod +++ /dev/null @@ -1,19 +0,0 @@ -module gitlab.com/developerdurp/durpify/handlers - -go 1.23.0 - -require github.com/charmbracelet/log v0.4.0 - -require ( - github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect - github.com/charmbracelet/lipgloss v0.13.0 // indirect - github.com/charmbracelet/x/ansi v0.2.3 // indirect - github.com/go-logfmt/logfmt v0.6.0 // indirect - github.com/lucasb-eyer/go-colorful v1.2.0 // indirect - github.com/mattn/go-isatty v0.0.20 // indirect - github.com/mattn/go-runewidth v0.0.16 // indirect - github.com/muesli/termenv v0.15.2 // indirect - github.com/rivo/uniseg v0.4.7 // indirect - golang.org/x/exp v0.0.0-20240823005443-9b4947da3948 // indirect - golang.org/x/sys v0.24.0 // indirect -) diff --git a/handlers/go.sum b/handlers/go.sum deleted file mode 100644 index 19b74ff..0000000 --- a/handlers/go.sum +++ /dev/null @@ -1,34 +0,0 @@ -github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k= -github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8= -github.com/charmbracelet/lipgloss v0.13.0 h1:4X3PPeoWEDCMvzDvGmTajSyYPcZM4+y8sCA/SsA3cjw= -github.com/charmbracelet/lipgloss v0.13.0/go.mod h1:nw4zy0SBX/F/eAO1cWdcvy6qnkDUxr8Lw7dvFrAIbbY= -github.com/charmbracelet/log v0.4.0 h1:G9bQAcx8rWA2T3pWvx7YtPTPwgqpk7D68BX21IRW8ZM= -github.com/charmbracelet/log v0.4.0/go.mod h1:63bXt/djrizTec0l11H20t8FDSvA4CRZJ1KH22MdptM= -github.com/charmbracelet/x/ansi v0.2.3 h1:VfFN0NUpcjBRd4DnKfRaIRo53KRgey/nhOoEqosGDEY= -github.com/charmbracelet/x/ansi v0.2.3/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw= -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/go-logfmt/logfmt v0.6.0 h1:wGYYu3uicYdqXVgoYbvnkrPVXkuLM1p1ifugDMEdRi4= -github.com/go-logfmt/logfmt v0.6.0/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs= -github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= -github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= -github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= -github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= -github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc= -github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= -github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo= -github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8= -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/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= -github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= -github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= -github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= -github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= -golang.org/x/exp v0.0.0-20240823005443-9b4947da3948 h1:kx6Ds3MlpiUHKj7syVnbp57++8WpuKPcR5yjLBjvLEA= -golang.org/x/exp v0.0.0-20240823005443-9b4947da3948/go.mod h1:akd2r19cwCdwSwWeIdzYQGa/EZZyqcOdwWiwj5L5eKQ= -golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg= -golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= -gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/handlers/handlers_test.go b/handlers/handlers_test.go deleted file mode 100644 index a56b3c4..0000000 --- a/handlers/handlers_test.go +++ /dev/null @@ -1,113 +0,0 @@ -package handlers - -import ( - "encoding/json" - "net/http" - "net/http/httptest" - "reflect" - "testing" -) - -func TestSendResponseStandardMessage(t *testing.T) { - message := &BasicMessage{ - Message: "Hello World!", - } - resp := &StandardMessage{ - Status: http.StatusAccepted, - Message: message, - } - w := httptest.NewRecorder() - resp.SendReponse(w) - - // Check the status code is set correctly - if w.Code != 202 { - t.Errorf("Expected status code to be 202, but got %d", w.Code) - } - - // Check that the content type header is set to "application/json" - contentType := w.Header().Get("Content-Type") - if contentType != "application/json" { - t.Errorf("Expected content type to be 'application/json', but got %s", contentType) - } - - // Check that the message is written to the response body correctly - response := &BasicMessage{} - err := json.NewDecoder(w.Body).Decode(response) - if err != nil { - t.Fatal(err) - } - if !reflect.DeepEqual(message, response) { - t.Errorf("Expected body to be %s but got %s", message, response) - } -} - -func TestSendResponseStandardError(t *testing.T) { - resp := &StandardError{ - Status: http.StatusInternalServerError, - Message: "An error has occured", - Description: []string{"An Error"}, - } - - w := httptest.NewRecorder() - resp.SendReponse(w) - - // Check the status code is set correctly - if w.Code != 500 { - t.Errorf("Expected status code to be 500, but got %d", w.Code) - } - - // Check that the content type header is set to "application/json" - contentType := w.Header().Get("Content-Type") - if contentType != "application/json" { - t.Errorf("Expected content type to be 'application/json', but got %s", contentType) - } - - // Check that the message is written to the response body correctly - response := &StandardError{} - err := json.NewDecoder(w.Body).Decode(response) - if err != nil { - t.Fatal(err) - } - - if response.Message != resp.Message { - t.Errorf("Expected Message of %s but got %s", resp.Message, response.Message) - } - - if !reflect.DeepEqual(resp, response) { - t.Errorf("Expected Message of %v but got %v", resp, response) - } -} - -// NewFailureResponse returns a new instance of StandardError with the given message, status code and description. -func TestNewFailureResponse(t *testing.T) { - message := "An error has occured" - status := http.StatusInternalServerError - description := []string{"An Error"} - resp := NewFailureResponse(message, status, description) - - if resp.Status != status { - t.Errorf("Expected Status to be %d but got %d", status, resp.Status) - } - if resp.Message != message { - t.Errorf("Expected Status to be %s but got %s", message, resp.Message) - } - if !reflect.DeepEqual(description, resp.Description) { - t.Errorf("Expected Status to be %v but got %v", description, resp.Description) - } -} - -func TestNewMessageResponse(t *testing.T) { - - message := &BasicMessage{ - Message: "Hello World!", - } - - resp := NewMessageResponse(message, http.StatusOK) - - if resp.Status != http.StatusOK { - t.Errorf("Expected Status to be %d but got %d", http.StatusOK, resp.Status) - } - if !reflect.DeepEqual(message, resp.Message) { - t.Errorf("Expected Message to be %s but got %s", message, resp.Message) - } -} diff --git a/middleware/auth.go b/middleware/auth.go index 83bf573..10a6735 100644 --- a/middleware/auth.go +++ b/middleware/auth.go @@ -3,12 +3,13 @@ package middleware import ( "context" "errors" - "github.com/charmbracelet/log" - "gitlab.com/developerdurp/durpify/handlers" "net/http" "strings" "time" + "github.com/charmbracelet/log" + "gitlab.com/durfy/durpify/handlers" + "github.com/MicahParks/keyfunc" "github.com/golang-jwt/jwt/v4" ) @@ -30,87 +31,90 @@ type StandardMessage struct { } func (cfg *AuthConfig) AuthMiddleware(next http.Handler) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + return http.HandlerFunc( + func(w http.ResponseWriter, r *http.Request) { + var groups []string - var groups []string - - tokenString, err := getToken(w) - if err != nil { - resp := handlers.NewFailureResponse( - err.Error(), - http.StatusUnauthorized, - []string{}, - ) - resp.SendReponse(w) - } - - token, err := cfg.validateToken(tokenString) - if err != nil { - resp := handlers.NewFailureResponse( - "Failed to Validate Token", - http.StatusUnauthorized, - []string{err.Error()}, - ) - resp.SendReponse(w) - } - - claims, ok := token.Claims.(jwt.MapClaims) - if !ok { - resp := handlers.NewFailureResponse( - "Invalid Authorization token claim", - http.StatusUnauthorized, - []string{}, - ) - resp.SendReponse(w) - return - } - - groupsClaim, ok := claims["groups"].([]interface{}) - if !ok { - resp := handlers.NewFailureResponse( - "Missing or invalid groups in the token", - http.StatusUnauthorized, - []string{}, - ) - resp.SendReponse(w) - return - } - - for _, group := range groupsClaim { - if groupName, ok := group.(string); ok { - groups = append(groups, groupName) + tokenString, err := getToken(w) + if err != nil { + resp := handlers.NewFailureResponse( + err.Error(), + http.StatusUnauthorized, + []string{}, + ) + resp.SendReponse(w, r) } - } - isAllowed := false - for _, allowedGroup := range cfg.allowedGroups { - for _, group := range groups { - if group == allowedGroup { - isAllowed = true + token, err := cfg.validateToken(tokenString) + if err != nil { + resp := handlers.NewFailureResponse( + "Failed to Validate Token", + http.StatusUnauthorized, + []string{err.Error()}, + ) + resp.SendReponse(w, r) + } + + claims, ok := token.Claims.(jwt.MapClaims) + if !ok { + resp := handlers.NewFailureResponse( + "Invalid Authorization token claim", + http.StatusUnauthorized, + []string{}, + ) + resp.SendReponse(w, r) + return + } + + groupsClaim, ok := claims["groups"].([]interface{}) + if !ok { + resp := handlers.NewFailureResponse( + "Missing or invalid groups in the token", + http.StatusUnauthorized, + []string{}, + ) + resp.SendReponse(w, r) + return + } + + for _, group := range groupsClaim { + if groupName, ok := group.(string); ok { + groups = append(groups, groupName) + } + } + + isAllowed := false + for _, allowedGroup := range cfg.allowedGroups { + for _, group := range groups { + if group == allowedGroup { + isAllowed = true + break + } + } + if isAllowed { break } } - if isAllowed { - break + + if !isAllowed { + resp := handlers.NewFailureResponse( + "Unauthorized to use this endpoint", + http.StatusUnauthorized, + []string{}, + ) + resp.SendReponse(w, r) + return } - } - if !isAllowed { - resp := handlers.NewFailureResponse( - "Unauthorized to use this endpoint", - http.StatusUnauthorized, - []string{}, - ) - resp.SendReponse(w) - return - } - - next.ServeHTTP(w, r) - }) + next.ServeHTTP(w, r) + }, + ) } -func (cfg *AuthConfig) validateToken(tokenString string) (*jwt.Token, error) { - +func (cfg *AuthConfig) validateToken(tokenString string) ( + *jwt.Token, + error, +) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() diff --git a/middleware/go.mod b/middleware/go.mod deleted file mode 100644 index 7048425..0000000 --- a/middleware/go.mod +++ /dev/null @@ -1,25 +0,0 @@ -module gitlab.com/developerdurp/durpify/middleware - -go 1.23.0 - -require ( - github.com/MicahParks/keyfunc v1.9.0 - github.com/charmbracelet/log v0.4.0 - github.com/golang-jwt/jwt/v4 v4.5.0 - gitlab.com/developerdurp/durpify/handlers v0.0.0-20240902184301-9f6d8ac6219b -) - -require ( - github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect - github.com/charmbracelet/lipgloss v0.13.0 // indirect - github.com/charmbracelet/x/ansi v0.2.3 // indirect - github.com/go-logfmt/logfmt v0.6.0 // indirect - github.com/lucasb-eyer/go-colorful v1.2.0 // indirect - github.com/mattn/go-isatty v0.0.20 // indirect - github.com/mattn/go-runewidth v0.0.16 // indirect - github.com/muesli/termenv v0.15.2 // indirect - github.com/rivo/uniseg v0.4.7 // indirect - gitlab.com/developerdurp/logger v1.0.0 // indirect - golang.org/x/exp v0.0.0-20240823005443-9b4947da3948 // indirect - golang.org/x/sys v0.24.0 // indirect -) diff --git a/middleware/go.sum b/middleware/go.sum deleted file mode 100644 index 2c13ca1..0000000 --- a/middleware/go.sum +++ /dev/null @@ -1,43 +0,0 @@ -github.com/MicahParks/keyfunc v1.9.0 h1:lhKd5xrFHLNOWrDc4Tyb/Q1AJ4LCzQ48GVJyVIID3+o= -github.com/MicahParks/keyfunc v1.9.0/go.mod h1:IdnCilugA0O/99dW+/MkvlyrsX8+L8+x95xuVNtM5jw= -github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k= -github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8= -github.com/charmbracelet/lipgloss v0.13.0 h1:4X3PPeoWEDCMvzDvGmTajSyYPcZM4+y8sCA/SsA3cjw= -github.com/charmbracelet/lipgloss v0.13.0/go.mod h1:nw4zy0SBX/F/eAO1cWdcvy6qnkDUxr8Lw7dvFrAIbbY= -github.com/charmbracelet/log v0.4.0 h1:G9bQAcx8rWA2T3pWvx7YtPTPwgqpk7D68BX21IRW8ZM= -github.com/charmbracelet/log v0.4.0/go.mod h1:63bXt/djrizTec0l11H20t8FDSvA4CRZJ1KH22MdptM= -github.com/charmbracelet/x/ansi v0.2.3 h1:VfFN0NUpcjBRd4DnKfRaIRo53KRgey/nhOoEqosGDEY= -github.com/charmbracelet/x/ansi v0.2.3/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw= -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/go-logfmt/logfmt v0.6.0 h1:wGYYu3uicYdqXVgoYbvnkrPVXkuLM1p1ifugDMEdRi4= -github.com/go-logfmt/logfmt v0.6.0/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs= -github.com/golang-jwt/jwt/v4 v4.4.2/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0= -github.com/golang-jwt/jwt/v4 v4.5.0 h1:7cYmW1XlMY7h7ii7UhUyChSgS5wUJEnm9uZVTGqOWzg= -github.com/golang-jwt/jwt/v4 v4.5.0/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0= -github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= -github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= -github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= -github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= -github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc= -github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= -github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo= -github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8= -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/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= -github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= -github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= -github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= -github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= -gitlab.com/developerdurp/durpify/handlers v0.0.0-20240902184301-9f6d8ac6219b h1:1JJ8A3suyoIiq/GB4CE4h2RIlJhudWnTCUf2Qrx7nx0= -gitlab.com/developerdurp/durpify/handlers v0.0.0-20240902184301-9f6d8ac6219b/go.mod h1:ZWQURdqvqLVAVle3SPjxd9gZ7CR/33TB2nwN1ZUmbq8= -gitlab.com/developerdurp/logger v1.0.0 h1:wozbKR26RVoFVaUgJV2x8WsZHLWOJBqaSCSTpK7crfk= -gitlab.com/developerdurp/logger v1.0.0/go.mod h1:x6gZvBeEq8oQUXeQoLY2m78mYJjvb5KE+7Vb5AcS8oo= -golang.org/x/exp v0.0.0-20240823005443-9b4947da3948 h1:kx6Ds3MlpiUHKj7syVnbp57++8WpuKPcR5yjLBjvLEA= -golang.org/x/exp v0.0.0-20240823005443-9b4947da3948/go.mod h1:akd2r19cwCdwSwWeIdzYQGa/EZZyqcOdwWiwj5L5eKQ= -golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg= -golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= -gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/middleware/logging.go b/middleware/logging.go index b303b98..53710dd 100644 --- a/middleware/logging.go +++ b/middleware/logging.go @@ -27,6 +27,12 @@ func Logging(next http.Handler) http.Handler { } next.ServeHTTP(wrapped, r) - log.Error("INFO", wrapped.statusCode, r.Method, r.URL.Path, time.Since(start)) + log.Error( + "INFO", + wrapped.statusCode, + r.Method, + r.URL.Path, + time.Since(start), + ) }) } diff --git a/middleware/recover.go b/middleware/recover.go new file mode 100644 index 0000000..1bf78b8 --- /dev/null +++ b/middleware/recover.go @@ -0,0 +1,29 @@ +package middleware + +import ( + "log" + "net/http" + "runtime/debug" + + "gitlab.com/durfy/durpify/handlers" +) + +func Recovery(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + defer func() { + if err := recover(); err != nil { + msg := "Caught panic: %v, Stack trace: %s" + log.Printf(msg, err, string(debug.Stack())) + + resp := handlers.NewFailureResponse( + "Unternal Server Error", + http.StatusInternalServerError, + []string{err.(string)}, + ) + resp.SendReponse(w, r) + return + } + }() + next.ServeHTTP(w, r) + }) +} diff --git a/renovate.json b/renovate.json new file mode 100644 index 0000000..7190a60 --- /dev/null +++ b/renovate.json @@ -0,0 +1,3 @@ +{ + "$schema": "https://docs.renovatebot.com/renovate-schema.json" +} diff --git a/handlers/handlers.go b/respond/handlers.go similarity index 51% rename from handlers/handlers.go rename to respond/handlers.go index aaf8ac6..83cca95 100644 --- a/handlers/handlers.go +++ b/respond/handlers.go @@ -1,11 +1,14 @@ -package handlers +package respond import ( "encoding/json" "errors" "fmt" - "github.com/charmbracelet/log" "net/http" + + "github.com/a-h/templ" + "github.com/charmbracelet/log" + "gopkg.in/yaml.v3" ) type BasicMessage struct { @@ -28,32 +31,67 @@ func (e StandardError) Error() string { } type Response interface { - SendResponse(w http.ResponseWriter) + SendResponse(w http.ResponseWriter, r *http.Request) Test(http.Handler) } -func (message *StandardMessage) SendReponse(w http.ResponseWriter) { +func (message *StandardMessage) SendReponse( + w http.ResponseWriter, + r *http.Request, +) { setHeader(&w, message.Status) - // Write the message to the response body. - err := json.NewEncoder(w).Encode(message.Message) - if err != nil { - log.Error("Failed to Encode") + contentType := r.Header.Get("Content-Type") + switch contentType { + case "application/yaml": + + err := yaml.NewEncoder(w).Encode(message.Message) + if err != nil { + log.Error("Failed to Encode YAML", "error", err) + return + } + default: + + err := json.NewEncoder(w).Encode(message.Message) + if err != nil { + log.Error("Failed to Encode") + return + } } } -func (message *StandardError) SendReponse(w http.ResponseWriter) { +func (message *StandardError) SendReponse( + w http.ResponseWriter, + r *http.Request, +) { setHeader(&w, message.Status) - // Write the message to the response body. - err := json.NewEncoder(w).Encode(message) - if err != nil { - log.Error("Failed to Encode") + contentType := r.Header.Get("Content-Type") + switch contentType { + case "application/yaml": + + err := yaml.NewEncoder(w).Encode(message.Message) + if err != nil { + log.Error("Failed to Encode YAML", "error", err) + return + } + default: + + err := json.NewEncoder(w).Encode(message.Message) + if err != nil { + log.Error("Failed to Encode") + return + } } } -// NewFailureResponse returns a new instance of StandardError with the given message, status code and description. -func NewFailureResponse(message string, status int, description []string) StandardError { +// NewFailureResponse returns a new instance of StandardError with the given +// message, status code and description. +func NewFailureResponse( + message string, + status int, + description []string, +) StandardError { return StandardError{ Message: message, Status: status, @@ -61,7 +99,8 @@ func NewFailureResponse(message string, status int, description []string) Standa } } -// NewMessageResponse returns a new instance of StandardMessage with the given message and status code. +// NewMessageResponse returns a new instance of StandardMessage with the given +// message and status code. func NewMessageResponse(message interface{}, status int) *StandardMessage { return &StandardMessage{ Message: message, @@ -69,7 +108,8 @@ func NewMessageResponse(message interface{}, status int) *StandardMessage { } } -// NewBasicResponse returns a new basic instance of StandardMessage with the message of OK and Status OK. +// NewBasicResponse returns a new basic instance of StandardMessage with the +// message of OK and Status OK. func NewBasicResponse() *StandardMessage { return &StandardMessage{ Message: BasicMessage{ @@ -85,7 +125,10 @@ func setHeader(w *http.ResponseWriter, statusCode int) { (*w).Header().Set("Content-Type", "application/json") } -type APIFunc func(w http.ResponseWriter, r *http.Request) (*StandardMessage, error) +type APIFunc func( + w http.ResponseWriter, + r *http.Request, +) (interface{}, error) func Make(handler APIFunc) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { @@ -93,7 +136,7 @@ func Make(handler APIFunc) http.HandlerFunc { if err != nil { var apiErr StandardError if errors.As(err, &apiErr) { - apiErr.SendReponse(w) + apiErr.SendReponse(w, r) return } resp := NewFailureResponse( @@ -101,8 +144,22 @@ func Make(handler APIFunc) http.HandlerFunc { http.StatusInternalServerError, []string{err.Error()}, ) - resp.SendReponse(w) + resp.SendReponse(w, r) + return } - resp.SendReponse(w) + + message, ok := resp.(*StandardMessage) + if ok { + message.SendReponse(w, r) + return + } + + templMessage, ok := resp.(templ.Component) + if ok { + templMessage.Render(r.Context(), w) + return + } + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte("Internal Server Error")) } } diff --git a/respond/handlers_test.go b/respond/handlers_test.go new file mode 100644 index 0000000..7502f86 --- /dev/null +++ b/respond/handlers_test.go @@ -0,0 +1,146 @@ +package respond + +// import ( +// "encoding/json" +// "net/http" +// "net/http/httptest" +// "reflect" +// "testing" +// ) +// +// func TestSendResponseStandardMessage(t *testing.T) { +// message := &BasicMessage{ +// Message: "Hello World!", +// } +// resp := &StandardMessage{ +// Status: http.StatusAccepted, +// Message: message, +// } +// w := httptest.NewRecorder() +// resp.SendReponse(w) +// +// // Check the status code is set correctly +// if w.Code != 202 { +// t.Errorf("Expected status code to be 202, but got %d", w.Code) +// } +// +// // Check that the content type header is set to "application/json" +// contentType := w.Header().Get("Content-Type") +// if contentType != "application/json" { +// t.Errorf( +// "Expected content type to be 'application/json', but got %s", +// contentType, +// ) +// } +// +// // Check that the message is written to the response body correctly +// response := &BasicMessage{} +// err := json.NewDecoder(w.Body).Decode(response) +// if err != nil { +// t.Fatal(err) +// } +// if !reflect.DeepEqual(message, response) { +// t.Errorf("Expected body to be %s but got %s", message, response) +// } +// } +// +// func TestSendResponseStandardError(t *testing.T) { +// resp := &StandardError{ +// Status: http.StatusInternalServerError, +// Message: "An error has occured", +// Description: []string{"An Error"}, +// } +// +// w := httptest.NewRecorder() +// resp.SendReponse(w) +// +// // Check the status code is set correctly +// if w.Code != 500 { +// t.Errorf("Expected status code to be 500, but got %d", w.Code) +// } +// +// // Check that the content type header is set to "application/json" +// contentType := w.Header().Get("Content-Type") +// if contentType != "application/json" { +// t.Errorf( +// "Expected content type to be 'application/json',"+ +// " but got %s", +// contentType, +// ) +// } +// +// // Check that the message is written to the response body correctly +// response := &StandardError{} +// err := json.NewDecoder(w.Body).Decode(response) +// if err != nil { +// t.Fatal(err) +// } +// +// if response.Message != resp.Message { +// t.Errorf( +// "Expected Message of %s but got %s", +// resp.Message, +// response.Message, +// ) +// } +// +// if !reflect.DeepEqual(resp, response) { +// t.Errorf("Expected Message of %v but got %v", resp, response) +// } +// } +// +// // NewFailureResponse returns a new instance of StandardError with the given +// // message, status code and description. +// func TestNewFailureResponse(t *testing.T) { +// message := "An error has occured" +// status := http.StatusInternalServerError +// description := []string{"An Error"} +// resp := NewFailureResponse(message, status, description) +// +// if resp.Status != status { +// t.Errorf( +// "Expected Status to be %d but got %d", +// status, +// resp.Status, +// ) +// } +// if resp.Message != message { +// t.Errorf( +// "Expected Status to be %s but got %s", +// message, +// resp.Message, +// ) +// } +// if !reflect.DeepEqual(description, resp.Description) { +// t.Errorf( +// "Expected Status to be %v but got %v", +// description, +// resp.Description, +// ) +// } +// } +// +// func TestNewMessageResponse(t *testing.T) { +// +// message := &BasicMessage{ +// Message: "Hello World!", +// } +// +// resp := NewMessageResponse(message, http.StatusOK) +// +// if resp.Status != http.StatusOK { +// t.Errorf( +// "Expected Status to be %d but got %d", +// http.StatusOK, +// resp.Status, +// ) +// } +// if !reflect.DeepEqual(message, resp.Message) { +// t.Errorf( +// "Expected Message to be %s but got %s", +// message, +// resp.Message, +// ) +// } +// } +//