From ce83633feb9553553df7e7182ebb2329d8c4dbfa Mon Sep 17 00:00:00 2001 From: Florian Loch Date: Mon, 24 Jun 2024 01:04:21 +0200 Subject: [PATCH] feat: add prometheus http metrics --- .env.sample | 6 +- .gitignore | 2 +- Dockerfile | 2 +- NOTES_ON_DOKKU.md | 2 +- README.md | 24 +++++-- go.mod | 35 +++++++--- go.sum | 77 +++++++++++++-------- internal/constants/constants.go | 19 +++--- internal/e2e_test/e2e_test.go | 2 +- internal/main.go | 117 +++++++++++++++++++++++++++----- 10 files changed, 207 insertions(+), 79 deletions(-) diff --git a/.env.sample b/.env.sample index 710c86c..eef3e30 100644 --- a/.env.sample +++ b/.env.sample @@ -1,8 +1,10 @@ -CASSETTE_APP_URL=https://cassette-app.de/ +CASSETTE_APP_URL=https://cassette-for-spotify.app/ CASSETTE_MONGODB_URI= CASSETTE_NETWORK_INTERFACE=0.0.0.0 +CASSETTE_INTERNAL_NETWORK_INTERFACE=localhost CASSETTE_PORT=8082 +CASSETTE_INTERNAL_PORT=8083 CASSETTE_SPOTIFY_CLIENT_ID= CASSETTE_SPOTIFY_CLIENT_KEY= CASSETTE_ENV=DEV -CASSETTE_SECRET= \ No newline at end of file +CASSETTE_SECRET= diff --git a/.gitignore b/.gitignore index 1f4df52..4e2035e 100755 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,4 @@ coverage.out node_modules/ .idea/ .vscode/ -static/ \ No newline at end of file +static/ diff --git a/Dockerfile b/Dockerfile index 4bcc589..c051cee 100644 --- a/Dockerfile +++ b/Dockerfile @@ -23,7 +23,7 @@ RUN yarn install COPY .git/ .git/ RUN GIT_VERSION=$(git describe --always) GIT_AUTHOR_DATE=$(git log -1 --format=%aI) BUILD_DATE=$(date +%Y-%m-%dT%H:%M:%S%z) yarn build -FROM alpine +FROM alpine:3.19 RUN apk --no-cache add ca-certificates WORKDIR /app COPY ./CHECKS . diff --git a/NOTES_ON_DOKKU.md b/NOTES_ON_DOKKU.md index 407be3c..a7dc42f 100644 --- a/NOTES_ON_DOKKU.md +++ b/NOTES_ON_DOKKU.md @@ -1,4 +1,4 @@ -For the git revision to be available at build time in dokku's environment it is essential to configure dokku to keep the .git directory: +For the git revision to be available at build time in dokku's environment, it is essential to configure dokku to keep the `.git` directory: ```bash # keep the .git directory during builds diff --git a/README.md b/README.md index d412466..a0f710a 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ An "audio book helper utility for Spotify®". -***Try it out at https://cassette-app.de.*** +***Try it out at https://cassette-for-spotify.app.*** Cassette is a small web application enabling you to pause and resume audiobooks on Spotify. Spotify offers a lot of great audiobooks but as its built with music in a broader sense in mind it does not provide essential features required in order to comfortably listen to them: pausing an audiobook and listening to some music results in you needing to think about in which chapter you left off. @@ -17,17 +17,27 @@ Cassette tries to overcome this problem by enabling you to store your state, put ## How is this done? -Simply spoken by using the Spotify Web API. Cassette itself consists of two components, a REST service running on a server (this directory) and a web app (./web) running in your browser. The service is talking with the Spotify Web API and a MongoDB database in with the states get persisted. The web app talks with the service via a REST interface. +In a nutshell: By using the Spotify Web API. +Cassette itself consists of two parts: +- a REST service running on a server (this directory) +- a web app (./web) running in your browser + +The service is talking with the Spotify Web API, and a MongoDB database in with the states get persisted. +The web app talks with the service via a REST interface. -## Current status of the project -After spending a lot of time rewriting all parts of this project I finally was able to release version 2. Although version 1, which had another name that could not be kept as it violated Spotify's branding guidelines, was available to the public already it was not really too robust, not really intuitive to use and overall not meant to be used by anyone but myself. It was more kind of a proof-of-concept project hacked together in a few days. -Version 2 is way more mature, takes care of data privacy, includes an interactive introduction tour and and a polished UI with both mobile and desktop users in mind. Its goal is to be, hopefully, useful to many users aside myself. +## Current status of the project +After spending a lot of time rewriting all parts of this project, I finally was able to release version 2. +Although version 1, which had another name that could not be kept as it violated Spotify's branding guidelines, was available to the public already, it was not really too robust, not really intuitive to use and overall not meant to be used by anyone but myself. +It was more a proof-of-concept project hacked together in a few days. -Version 2 is deployed at https://cassette-app.de. +Version 2 is way more mature, takes care of data privacy, includes an interactive introduction tour and a polished UI with both mobile and desktop users in mind. +Its goal is to be, hopefully, useful to many users aside myself. +Version 2 is deployed at https://cassette-for-spotify.app. ## Disclaimer -The authors of this project are not related to Spotify in any way beside being happy users of their platform. This service is not related to Spotify except using their API and content. \ No newline at end of file +The authors of this project are not related to Spotify in any way besides being happy users of their platform. +This service is not related to Spotify; it is only using their API and content. \ No newline at end of file diff --git a/go.mod b/go.mod index 13e0b2b..25d6918 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,8 @@ module github.com/florianloch/cassette -go 1.22 +go 1.22.3 + +toolchain go1.22.4 require ( github.com/gavv/httpexpect/v2 v2.16.0 // lock to this version, test breaks when upgrading @@ -15,32 +17,42 @@ require ( golang.org/x/oauth2 v0.21.0 ) +require ( + github.com/prometheus/client_golang v1.19.1 + github.com/slok/go-http-metrics v0.12.0 +) + require ( github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2 // indirect github.com/ajg/form v1.5.1 // indirect - github.com/andybalholm/brotli v1.0.4 // indirect + github.com/andybalholm/brotli v1.1.0 // indirect + github.com/beorn7/perks v1.0.1 // indirect + github.com/cespare/xxhash/v2 v2.3.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/fatih/color v1.15.0 // indirect github.com/fatih/structs v1.1.0 // indirect github.com/gobwas/glob v0.2.3 // indirect - github.com/golang/snappy v0.0.3 // indirect + github.com/golang/snappy v0.0.4 // indirect github.com/google/go-querystring v1.1.0 // indirect github.com/gorilla/securecookie v1.1.2 // indirect github.com/gorilla/websocket v1.4.2 // indirect github.com/hpcloud/tail v1.0.0 // indirect github.com/imkira/go-interpol v1.1.0 // indirect - github.com/klauspost/compress v1.15.0 // indirect + github.com/klauspost/compress v1.17.8 // indirect github.com/mattn/go-colorable v0.1.13 // indirect - github.com/mattn/go-isatty v0.0.19 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect github.com/mitchellh/go-wordwrap v1.0.1 // indirect github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/prometheus/client_model v0.6.1 // indirect + github.com/prometheus/common v0.53.0 // indirect + github.com/prometheus/procfs v0.15.0 // indirect github.com/rs/xid v1.5.0 // indirect github.com/sanity-io/litter v1.5.5 // indirect github.com/sergi/go-diff v1.0.0 // indirect - github.com/stretchr/testify v1.7.0 // indirect + github.com/stretchr/testify v1.9.0 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect - github.com/valyala/fasthttp v1.34.0 // indirect + github.com/valyala/fasthttp v1.52.0 // indirect github.com/xdg-go/pbkdf2 v1.0.0 // indirect github.com/xdg-go/scram v1.1.2 // indirect github.com/xdg-go/stringprep v1.0.4 // indirect @@ -51,11 +63,12 @@ require ( github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect github.com/yudai/gojsondiff v1.0.0 // indirect github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82 // indirect - golang.org/x/crypto v0.17.0 // indirect - golang.org/x/net v0.10.0 // indirect - golang.org/x/sync v0.1.0 // indirect - golang.org/x/sys v0.15.0 // indirect + golang.org/x/crypto v0.22.0 // indirect + golang.org/x/net v0.24.0 // indirect + golang.org/x/sync v0.7.0 // indirect + golang.org/x/sys v0.20.0 // indirect golang.org/x/text v0.14.0 // indirect + google.golang.org/protobuf v1.34.1 // indirect gopkg.in/fsnotify.v1 v1.4.7 // indirect gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/go.sum b/go.sum index 8c80a09..54b9704 100644 --- a/go.sum +++ b/go.sum @@ -3,8 +3,12 @@ github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2 h1:ZBbLwSJqkH github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2/go.mod h1:VSw57q4QFiWDbRnjdX8Cb3Ow0SFncRw+bA/ofY6Q83w= github.com/ajg/form v1.5.1 h1:t9c7v8JUKu/XxOGBU0yjNpaMloxGEJhUkqFRq0ibGeU= github.com/ajg/form v1.5.1/go.mod h1:uL1WgH+h2mgNtvBq0339dVnzXdBETtL2LeUXaIv25UY= -github.com/andybalholm/brotli v1.0.4 h1:V7DdXeJtZscaqfNuAdSRuRFzuiKlHSC/Zh3zl9qY3JY= -github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= +github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M= +github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY= +github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= +github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= +github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= +github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= github.com/davecgh/go-spew v0.0.0-20161028175848-04cdfd42973b/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -24,11 +28,11 @@ github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5x github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc= github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/golang/snappy v0.0.3 h1:fHPg5GQYlCeLIPB9BZqMVR5nR9A+IM5zcgeTdjMYmLA= -github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= +github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM= +github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= -github.com/google/go-cmp v0.5.9/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/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8= github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU= github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0= @@ -49,13 +53,18 @@ github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/imkira/go-interpol v1.1.0 h1:KIiKr0VSG2CUW1hl1jpiyuzuJeKUUpC8iM1AIE7N1Vk= github.com/imkira/go-interpol v1.1.0/go.mod h1:z0h2/2T3XF8kyEPpRgJ3kmNv+C43p+I/CoI+jC3w2iA= -github.com/klauspost/compress v1.15.0 h1:xqfchp4whNFxn5A4XFyyYtitiWI8Hy5EW59jEwcyL6U= -github.com/klauspost/compress v1.15.0/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk= +github.com/klauspost/compress v1.17.8 h1:YcnTYrq7MikUT7k0Yb5eceMmALQPYBW/Xltxn0NAMnU= +github.com/klauspost/compress v1.17.8/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw= +github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= 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.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= -github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +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/mitchellh/go-wordwrap v1.0.1 h1:TLuKupo69TCn6TQSyGxwI1EblZZEsQ0vMlAFQflz0v0= github.com/mitchellh/go-wordwrap v1.0.1/go.mod h1:R62XHJLzvMFRBbcrT7m7WgmE1eOyTSsCt+hzestvNj0= github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe h1:iruDEfMl2E6fbMZ9s0scYfZQ84/6SPL6zC8ACM2oIL0= @@ -69,6 +78,16 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE github.com/pmezard/go-difflib v0.0.0-20151028094244-d8ed2627bdf0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 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/prometheus/client_golang v1.19.1 h1:wZWJDwK+NameRJuPGDhlnFgx8e8HN3XHQeLaYJFJBOE= +github.com/prometheus/client_golang v1.19.1/go.mod h1:mP78NwGzrVks5S2H6ab8+ZZGJLZUq1hoULYBAYBw1Ho= +github.com/prometheus/client_model v0.6.1 h1:ZKSh/rekM+n3CeS952MLRAdFwIKqeY8b62p8ais2e9E= +github.com/prometheus/client_model v0.6.1/go.mod h1:OrxVMOVHjw3lKMa8+x6HeMGkHMQyHDk9E3jmP2AmGiY= +github.com/prometheus/common v0.53.0 h1:U2pL9w9nmJwJDa4qqLQ3ZaePJ6ZTwt7cMD3AG3+aLCE= +github.com/prometheus/common v0.53.0/go.mod h1:BrxBKv3FWBIGXw89Mg1AeBq7FSyRzXWI3l3e7W3RN5U= +github.com/prometheus/procfs v0.15.0 h1:A82kmvXJq2jTu5YUhSGNlYoxh85zLnKgPz4bMZgI5Ek= +github.com/prometheus/procfs v0.15.0/go.mod h1:Y0RJ/Y5g5wJpkTisOtqwDSo4HwhGmLB4VQSw2sQJLHk= +github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= +github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog= github.com/rs/xid v1.5.0 h1:mKX4bl4iPYJtEIxp6CYiUuLQ/8DYMoz0PUdtGgMFRVc= github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8= @@ -77,18 +96,22 @@ github.com/sanity-io/litter v1.5.5 h1:iE+sBxPBzoK6uaEP5Lt3fHNgpKcHXc/A2HGETy0uJQ github.com/sanity-io/litter v1.5.5/go.mod h1:9gzJgR2i4ZpjZHsKvUXIRQVk7P+yM3e+jAF7bU2UI5U= github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ= github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= +github.com/slok/go-http-metrics v0.12.0 h1:mAb7hrX4gB4ItU6NkFoKYdBslafg3o60/HbGBRsKaG8= +github.com/slok/go-http-metrics v0.12.0/go.mod h1:Ee/mdT9BYvGrlGzlClkK05pP2hRHmVbRF9dtUVS8LNA= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY= +github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= github.com/stretchr/testify v0.0.0-20161117074351-18a02ba4a312/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= -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.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/tailscale/depaware v0.0.0-20210622194025-720c4b409502/go.mod h1:p9lPsd+cx33L3H9nNoecRRxPssFKUwwI50I3pZ0yT+8= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= -github.com/valyala/fasthttp v1.34.0 h1:d3AAQJ2DRcxJYHm7OXNXtXt2as1vMDfxeIcFvhmGGm4= -github.com/valyala/fasthttp v1.34.0/go.mod h1:epZA5N+7pY6ZaEKRmstzOuYJx9HI8DI1oaCGZpdH4h0= -github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc= +github.com/valyala/fasthttp v1.52.0 h1:wqBQpxH71XW0e2g+Og4dzQM8pk34aFYlA1Ga8db7gU0= +github.com/valyala/fasthttp v1.52.0/go.mod h1:hf5C4QnVMkNXMspnsUlfM3WitlgYflyhHYoKol/szxQ= github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c= github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI= github.com/xdg-go/scram v1.1.2 h1:FHX5I5B4i4hKRVRBCFRxq1iQRej7WO3hhBuJf+UUySY= @@ -123,9 +146,8 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= -golang.org/x/crypto v0.0.0-20220214200702-86341886e292/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= -golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k= -golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4= +golang.org/x/crypto v0.22.0 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30= +golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.4.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= @@ -137,11 +159,9 @@ golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLL golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= -golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= -golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= -golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M= -golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= +golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w= +golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8= golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.21.0 h1:tsimM75w1tF/uws5rbeHzIWxEqElMehnc+iW793zsZs= golang.org/x/oauth2 v0.21.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI= @@ -150,30 +170,26 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/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 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= -golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= +golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9/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-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= -golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y= +golang.org/x/sys v0.20.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/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.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ= golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= @@ -188,8 +204,11 @@ golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8T golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg= +google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4= gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= diff --git a/internal/constants/constants.go b/internal/constants/constants.go index e4140ba..87e9ffc 100644 --- a/internal/constants/constants.go +++ b/internal/constants/constants.go @@ -8,19 +8,22 @@ const ( ConsentNoticeHeaderName = "X-Cassette-Consent-Notice" DefaultNetworkInterface = "localhost" DefaultPort = "8080" + DefaultInternalPort = "8081" JumpBackNSeconds = 10 WebStaticContentPath = "./web/dist" OAuthCallbackRoute = "/spotify-oauth-callback" // Names of envs - EnvENV = "CASSETTE_ENV" - EnvNetworkInterface = "CASSETTE_NETWORK_INTERFACE" - EnvPort = "CASSETTE_PORT" - EnvAppURL = "CASSETTE_APP_URL" - EnvSecret = "CASSETTE_SECRET" - EnvMongoURI = "CASSETTE_MONGODB_URI" - EnvSpotifyClientID = "CASSETTE_SPOTIFY_CLIENT_ID" - EnvSpotifyClientSecret = "CASSETTE_SPOTIFY_CLIENT_KEY" + EnvENV = "CASSETTE_ENV" + EnvNetworkInterface = "CASSETTE_NETWORK_INTERFACE" + EnvInternalNetworkInterface = "CASSETTE_INTERNAL_NETWORK_INTERFACE" + EnvPort = "CASSETTE_PORT" + EnvInternalPort = "CASSETTE_INTERNAL_PORT" + EnvAppURL = "CASSETTE_APP_URL" + EnvSecret = "CASSETTE_SECRET" + EnvMongoURI = "CASSETTE_MONGODB_URI" + EnvSpotifyClientID = "CASSETTE_SPOTIFY_CLIENT_ID" + EnvSpotifyClientSecret = "CASSETTE_SPOTIFY_CLIENT_KEY" // Keys for context fields FieldKeySession = ctxKey(iota) diff --git a/internal/e2e_test/e2e_test.go b/internal/e2e_test/e2e_test.go index 71c0d9a..08ba9a2 100644 --- a/internal/e2e_test/e2e_test.go +++ b/internal/e2e_test/e2e_test.go @@ -177,7 +177,7 @@ func beforeEach(t *testing.T) (*httpexpect.Expect, *gomock.Controller, *mocks.Mo handler := main.SetupForTest(daoMock, authMock, spotClientMockCreator, webRoot) e := httpexpect.WithConfig(httpexpect.Config{ - BaseURL: "http://cassette-app.de", + BaseURL: "http://cassette-for-spotify.app", Client: &http.Client{ Transport: httpexpect.NewBinder(handler), Jar: httpexpect.NewCookieJar(), diff --git a/internal/main.go b/internal/main.go index 91382a3..9b78c48 100644 --- a/internal/main.go +++ b/internal/main.go @@ -8,26 +8,32 @@ import ( "net/http" "net/url" "os" + "os/signal" "path/filepath" "strconv" + "sync" "time" - "github.com/florianloch/cassette/internal/constants" - "github.com/florianloch/cassette/internal/handler" - "github.com/florianloch/cassette/internal/middleware" - "github.com/florianloch/cassette/internal/persistence" - "github.com/florianloch/cassette/internal/spotify" - "github.com/florianloch/cassette/internal/util" - "github.com/go-chi/chi" chiMiddleware "github.com/go-chi/chi/middleware" "github.com/gorilla/csrf" "github.com/gorilla/sessions" + "github.com/prometheus/client_golang/prometheus/promhttp" "github.com/rs/zerolog" "github.com/rs/zerolog/hlog" "github.com/rs/zerolog/log" + metrics "github.com/slok/go-http-metrics/metrics/prometheus" + httpMetricsMiddleware "github.com/slok/go-http-metrics/middleware" + "github.com/slok/go-http-metrics/middleware/std" spotifyAPI "github.com/zmb3/spotify" "golang.org/x/oauth2" + + "github.com/florianloch/cassette/internal/constants" + "github.com/florianloch/cassette/internal/handler" + "github.com/florianloch/cassette/internal/middleware" + "github.com/florianloch/cassette/internal/persistence" + "github.com/florianloch/cassette/internal/spotify" + "github.com/florianloch/cassette/internal/util" ) var ( @@ -43,6 +49,9 @@ type spotClientCreator func(token *oauth2.Token) spotify.SpotClient type m map[string]interface{} func RunInProduction() { + ctx, cancelFn := signal.NotifyContext(context.Background(), os.Interrupt) + defer cancelFn() + isDevMode := util.Env(constants.EnvENV, "") == "DEV" networkInterface := util.Env(constants.EnvNetworkInterface, constants.DefaultNetworkInterface) @@ -88,13 +97,86 @@ func RunInProduction() { if err != nil { log.Fatal().Err(err).Msg("Could not get current working directory.") } - r := setupAPI(cwd, isDevMode) - interfacePort := networkInterface + ":" + port - log.Info().Msgf("Webserver started: http://%s", interfacePort) + publicRouter := chi.NewRouter() + + internalRouter := chi.NewRouter() + + promMiddleware := httpMetricsMiddleware.New(httpMetricsMiddleware.Config{ + Recorder: metrics.NewRecorder(metrics.Config{}), + }) + + publicRouter.Use(std.HandlerProvider("", promMiddleware)) + + setupAPI(cwd, isDevMode, publicRouter) + + internalRouter.Handle("/internal/metrics", promhttp.Handler()) + + publicServerAddr := fmt.Sprintf("%s:%s", networkInterface, port) + internalServerAddr := fmt.Sprintf("%s:%s", + util.Env(constants.EnvInternalNetworkInterface, constants.DefaultNetworkInterface), + util.Env(constants.EnvInternalPort, constants.DefaultInternalPort)) + + publicServer := http.Server{ + Addr: publicServerAddr, + Handler: publicRouter, + } + internalServer := http.Server{ + Addr: internalServerAddr, + Handler: internalRouter, + } + + serveWG := &sync.WaitGroup{} + serveWG.Add(3) - err = http.ListenAndServe(interfacePort, r) - log.Fatal().Err(err).Msg("Server terminated.") + go func() { + defer serveWG.Done() + + <-ctx.Done() + + log.Debug().Msg("Shutdown signal received. Shutting down server...") + + timeout, cancelFn := context.WithTimeout(context.Background(), 10*time.Second) + defer cancelFn() + + if err := publicServer.Shutdown(timeout); err != nil { + log.Error().Err(err).Msg("Failed to shutdown public server gracefully.") + } + + timeout, cancelFn = context.WithTimeout(context.Background(), 10*time.Second) + defer cancelFn() + + if err := internalServer.Shutdown(timeout); err != nil { + log.Error().Err(err).Msg("Failed to shutdown internal server gracefully.") + } + }() + + go func() { + defer serveWG.Done() + + if err := publicServer.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) { + log.Fatal().Err(err).Msg("Failed running public server.") + } + }() + + go func() { + defer serveWG.Done() + + if err := internalServer.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) { + log.Fatal().Err(err).Msg("Failed running internal server.") + } + }() + + log.Info().Msgf("Public server is ready to handle requests at http://%s", publicServerAddr) + log.Info().Msgf("Internal server is ready to handle requests at http://%s", internalServerAddr) + + <-ctx.Done() + + log.Info().Msg("Waiting for connections to be closed and for server to shutdown...") + + serveWG.Wait() + + log.Info().Msg("Server have been shut down. Bye.") } func SetupForTest( @@ -111,10 +193,13 @@ func SetupForTest( createSpotClient = spotClientMockCreator - return setupAPI(webRoot, true) + r := chi.NewRouter() + setupAPI(webRoot, true, r) + + return r } -func setupAPI(webRoot string, isDevMode bool) http.Handler { +func setupAPI(webRoot string, isDevMode bool, r chi.Router) { if isDevMode { zerolog.SetGlobalLevel(zerolog.DebugLevel) } else { @@ -155,8 +240,6 @@ func setupAPI(webRoot string, isDevMode bool) http.Handler { SetFileServer(http.FileServer(http.Dir(staticAssetsPath))) log.Info().Msgf("Loading assets from: '%s'", staticAssetsPath) - r := chi.NewRouter() - r.Use(chiMiddleware.Compress(5)) r.Use(chiMiddleware.RequestID) r.Use(hlog.NewHandler(log.Logger)) @@ -214,8 +297,6 @@ func setupAPI(webRoot string, isDevMode bool) http.Handler { consentMiddleware := middleware.CreateConsentMiddleware(spaHandler) chain := consentMiddleware(spotAuthMiddleware(spaHandler)) r.NotFound(chain.ServeHTTP) - - return r } func attachSession(next http.Handler) http.Handler {