From 36a27f91b90b94f6a80b8db4ecb180bfc4768c1c Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Sun, 15 Oct 2023 20:04:22 +0200 Subject: [PATCH] Update dependencies, fix linter errors, modernize code Signed-off-by: Knut Ahlers --- ci/bundle_js.sh | 45 ++++++++++++++++--------------- frontend/.gitignore | 2 +- frontend/app.js | 33 ++++++++++++++--------- go.mod | 23 +++++++++------- go.sum | 64 +++++++++++++++++++++++++-------------------- handler.go | 25 ++++++++++++------ main.go | 46 +++++++++++++++++++++----------- state.go | 29 ++++++++++++-------- 8 files changed, 161 insertions(+), 106 deletions(-) diff --git a/ci/bundle_js.sh b/ci/bundle_js.sh index b538939..2d9c0ec 100644 --- a/ci/bundle_js.sh +++ b/ci/bundle_js.sh @@ -1,38 +1,41 @@ #!/bin/bash set -euxo pipefail -css_deps=( - # Must-have order - npm/bootstrap@4/dist/css/bootstrap.min.css - npm/bootswatch@4/dist/darkly/bootstrap.min.css - npm/bootstrap-vue@2/dist/bootstrap-vue.min.css +leaflet_ver="1.7.1" - # Other packages - npm/leaflet@1.7.1/dist/leaflet.min.css +css_deps=( + # Must-have order + npm/bootstrap@4/dist/css/bootstrap.min.css + npm/bootswatch@4/dist/darkly/bootstrap.min.css + npm/bootstrap-vue@2/dist/bootstrap-vue.min.css + + # Other packages + npm/leaflet@${leaflet_ver}/dist/leaflet.min.css ) js_deps=( - # Must-have order - npm/vue@2/dist/vue.min.js - npm/bootstrap-vue@2/dist/bootstrap-vue.min.js - npm/vue-i18n@8.21.0/dist/vue-i18n.min.js + # Must-have order + npm/vue@2/dist/vue.min.js + npm/bootstrap-vue@2/dist/bootstrap-vue.min.js + npm/vue-i18n@8.21.0/dist/vue-i18n.min.js - # Other packages - npm/axios@0.20.0/dist/axios.min.js - npm/leaflet@1.7.1/dist/leaflet.min.js - npm/moment@2.27.0/min/moment.min.js + # Other packages + npm/leaflet@${leaflet_ver}/dist/leaflet.min.js + npm/moment@2.27.0/min/moment.min.js ) leaflet_images=( - marker-icon.png - marker-icon-2x.png - marker-shadow.png + layers.png + layers-2x.png + marker-icon.png + marker-icon-2x.png + marker-shadow.png ) -rm -rf frontend/leaflet -mkdir -p frontend/leaflet +rm -rf frontend/images +mkdir -p frontend/images for img in "${leaflet_images[@]}"; do - curl -sSfLo "frontend/leaflet/${img}" "https://cdn.jsdelivr.net/npm/leaflet@1.5.1/dist/images/${img}" + curl -sSfLo "frontend/images/${img}" "https://cdn.jsdelivr.net/npm/leaflet@${leaflet_ver}/dist/images/${img}" done IFS=',' diff --git a/frontend/.gitignore b/frontend/.gitignore index 0fd7aef..d50c222 100644 --- a/frontend/.gitignore +++ b/frontend/.gitignore @@ -1,4 +1,4 @@ combine.css combine.js fontawesome -leaflet +images diff --git a/frontend/app.js b/frontend/app.js index 652cc5c..637b1e2 100644 --- a/frontend/app.js +++ b/frontend/app.js @@ -1,3 +1,5 @@ +/* global VueI18n */ + const get_locale = (fallback = 'en') => { const urlParams = new URLSearchParams(window.location.search) @@ -14,13 +16,13 @@ const get_locale = (fallback = 'en') => { } switch (typeof lc) { - case 'object': - if (lc.length > 0) { - return lc[0].split('-')[0] - } - break - case 'string': - return lc.split('-')[0] + case 'object': + if (lc.length > 0) { + return lc[0].split('-')[0] + } + break + case 'string': + return lc.split('-')[0] } } @@ -60,16 +62,16 @@ window.app = new Vue({ zoom() { const params = new URLSearchParams(window.location.search) return params.has('zoom') ? parseInt(params.get('zoom')) : 13 - } + }, }, created() { // Use defaults with custom icon paths this.icon = L.icon({ ...L.Icon.Default.prototype.options, - iconUrl: '/asset/leaflet/marker-icon.png', - iconRetinaUrl: '/asset/leaflet/marker-icon-2x.png', - shadowUrl: '/asset/leaflet/marker-shadow.png', + iconUrl: '/asset/images/marker-icon.png', + iconRetinaUrl: '/asset/images/marker-icon-2x.png', + shadowUrl: '/asset/images/marker-shadow.png', }) /* @@ -159,7 +161,14 @@ window.app = new Vue({ sender_id: this.browserID, } - return axios.put(window.location.href.split('#')[0], data) + return fetch(window.location.href.split('#')[0], { + body: JSON.stringify(data), + credentials: 'same-origin', + headers: { + 'Content-Type': 'application/json', + }, + method: 'PUT', + }) .catch(err => console.error(err)) }, diff --git a/go.mod b/go.mod index 2f41181..93e1be6 100644 --- a/go.mod +++ b/go.mod @@ -1,14 +1,19 @@ module github.com/Luzifer/mapshare -go 1.13 +go 1.21 require ( - github.com/Luzifer/rconfig/v2 v2.2.1 - github.com/gofrs/uuid v3.2.0+incompatible - github.com/gorilla/mux v1.7.3 - github.com/gorilla/websocket v1.4.1 - github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect - github.com/pkg/errors v0.8.1 - github.com/sirupsen/logrus v1.4.2 - golang.org/x/sys v0.0.0-20200106162015-b016eb3dc98e // indirect + github.com/Luzifer/rconfig/v2 v2.4.0 + github.com/gofrs/uuid v4.4.0+incompatible + github.com/gorilla/mux v1.8.0 + github.com/gorilla/websocket v1.5.0 + github.com/pkg/errors v0.9.1 + github.com/sirupsen/logrus v1.9.3 +) + +require ( + github.com/spf13/pflag v1.0.5 // indirect + golang.org/x/sys v0.13.0 // indirect + gopkg.in/validator.v2 v2.0.1 // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect ) diff --git a/go.sum b/go.sum index 62db64f..9ec1b2f 100644 --- a/go.sum +++ b/go.sum @@ -1,32 +1,38 @@ -github.com/Luzifer/rconfig v2.2.0+incompatible h1:Kle3+rshPM7LxciOheaR4EfHUzibkDDGws04sefQ5m8= -github.com/Luzifer/rconfig/v2 v2.2.1 h1:zcDdLQlnlzwcBJ8E0WFzOkQE1pCMn3EbX0dFYkeTczg= -github.com/Luzifer/rconfig/v2 v2.2.1/go.mod h1:OKIX0/JRZrPJ/ZXXWklQEFXA6tBfWaljZbW37w+sqBw= +github.com/Luzifer/rconfig/v2 v2.4.0 h1:MAdymTlExAZ8mx5VG8xOFAtFQSpWBipKYQHPOmYTn9o= +github.com/Luzifer/rconfig/v2 v2.4.0/go.mod h1:hWF3ZVSusbYlg5bEvCwalEyUSY+0JPJWUiIu7rBmav8= +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/gofrs/uuid v3.2.0+incompatible h1:y12jRkkFxsd7GpqdSZ+/KCs/fJbqpEXSGd4+jfEaewE= -github.com/gofrs/uuid v3.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= -github.com/gorilla/mux v1.7.3 h1:gnP5JzjVOuiZD07fKKToCAOjS0yOpj/qPETTXCCS6hw= -github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= -github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM= -github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= -github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk= -github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= -github.com/konsorten/go-windows-terminal-sequences v1.0.2 h1:DB17ag19krx9CFsz4o3enTrPXyIXCl+2iCXH/aMAp9s= -github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= -github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= -github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/gofrs/uuid v4.4.0+incompatible h1:3qXRTX8/NbyulANqlc0lchS1gqAVxRgsuW1YrTJupqA= +github.com/gofrs/uuid v4.4.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= +github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= +github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= +github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= +github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI= +github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= +github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +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/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4= -github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= -github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg= -github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= -github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= -golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc= -golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200106162015-b016eb3dc98e h1:LwyF2AFISC9nVbS6MgzsaQNSUsRXI49GS+YQ5KX/QH0= -golang.org/x/sys v0.0.0-20200106162015-b016eb3dc98e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= +github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +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= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE= +golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/validator.v2 v2.0.0-20180514200540-135c24b11c19 h1:WB265cn5OpO+hK3pikC9hpP1zI/KTwmyMFKloW9eOVc= -gopkg.in/validator.v2 v2.0.0-20180514200540-135c24b11c19/go.mod h1:o4V0GXN9/CAmCsvJ0oXYZvrZOe7syiDZSN1GWGZTGzc= -gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= -gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +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/validator.v2 v2.0.1 h1:xF0KWyGWXm/LM2G1TrEjqOu4pa6coO9AlWSf3msVfDY= +gopkg.in/validator.v2 v2.0.1/go.mod h1:lIUZBlB3Im4s/eYp39Ry/wkR02yOPhZ9IwIRBjuPuG8= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= +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= diff --git a/handler.go b/handler.go index e08b9e4..4a7209e 100644 --- a/handler.go +++ b/handler.go @@ -9,7 +9,12 @@ import ( "github.com/gofrs/uuid" "github.com/gorilla/mux" "github.com/gorilla/websocket" - log "github.com/sirupsen/logrus" + "github.com/sirupsen/logrus" +) + +const ( + websocketBufferSize = 1024 + websocketUpdatePoolSize = 10 ) type position struct { @@ -27,8 +32,8 @@ var ( reqRetainerLock = new(sync.RWMutex) upgrader = websocket.Upgrader{ - ReadBufferSize: 1024, - WriteBufferSize: 1024, + ReadBufferSize: websocketBufferSize, + WriteBufferSize: websocketBufferSize, } ) @@ -45,7 +50,7 @@ func handleMapSocket(w http.ResponseWriter, r *http.Request) { vars = mux.Vars(r) mapID = vars["mapID"] sockID = uuid.Must(uuid.NewV4()).String() - updates = make(chan position, 10) + updates = make(chan position, websocketUpdatePoolSize) ) // Register update channel @@ -75,15 +80,19 @@ func handleMapSocket(w http.ResponseWriter, r *http.Request) { // Open socket conn, err := upgrader.Upgrade(w, r, nil) if err != nil { - log.WithError(err).Debug("Unable to open websocket") + logrus.WithError(err).Debug("opening websocket") return } - defer conn.Close() + defer func() { + if err := conn.Close(); err != nil { + logrus.WithError(err).Error("closing socket connection (leaked fd)") + } + }() // Send updates for pos := range updates { if err = conn.WriteJSON(pos); err != nil { - log.WithError(err).Debug("Unable to send position") + logrus.WithError(err).Debug("sending position") return } } @@ -113,7 +122,7 @@ func handleMapSubmit(w http.ResponseWriter, r *http.Request) { go func() { if err := retainState(); err != nil { - log.WithError(err).Error("Unable to retain state to disk") + logrus.WithError(err).Error("retaining state to disk") } }() diff --git a/main.go b/main.go index 8ee45a6..37271ce 100644 --- a/main.go +++ b/main.go @@ -3,11 +3,11 @@ package main import ( "fmt" "net/http" - "os" "time" "github.com/gorilla/mux" - log "github.com/sirupsen/logrus" + "github.com/pkg/errors" + "github.com/sirupsen/logrus" "github.com/Luzifer/rconfig/v2" ) @@ -24,27 +24,34 @@ var ( version = "dev" ) -func init() { +func initApp() error { rconfig.AutoEnv(true) if err := rconfig.ParseAndValidate(&cfg); err != nil { - log.Fatalf("Unable to parse commandline options: %s", err) + return errors.Wrap(err, "parsing CLI options") } - if cfg.VersionAndExit { - fmt.Printf("mapshare %s\n", version) - os.Exit(0) + l, err := logrus.ParseLevel(cfg.LogLevel) + if err != nil { + return errors.Wrap(err, "parsing log level") } + logrus.SetLevel(l) - if l, err := log.ParseLevel(cfg.LogLevel); err != nil { - log.WithError(err).Fatal("Unable to parse log level") - } else { - log.SetLevel(l) - } + return nil } func main() { - if err := loadState(); err != nil { - log.WithError(err).Fatal("Unable to load state") + var err error + if err = initApp(); err != nil { + logrus.WithError(err).Fatal("initializing app") + } + + if cfg.VersionAndExit { + fmt.Printf("mapshare %s\n", version) //nolint:forbidigo + return + } + + if err = loadState(); err != nil { + logrus.WithError(err).Fatal("loading state") } r := mux.NewRouter() @@ -59,5 +66,14 @@ func main() { r.HandleFunc("/{mapID}", handleMapSubmit).Methods(http.MethodPut) r.HandleFunc("/{mapID}/ws", handleMapSocket).Methods(http.MethodGet) - log.WithError(http.ListenAndServe(cfg.Listen, r)).Error("HTTP server caused an error") + server := &http.Server{ + Addr: cfg.Listen, + Handler: r, + ReadHeaderTimeout: time.Second, + } + + logrus.WithField("version", version).Info("mapshare ready to serve") + if err = server.ListenAndServe(); err != nil { + logrus.WithError(err).Fatal("running HTTP server") + } } diff --git a/state.go b/state.go index 2a8232f..553857c 100644 --- a/state.go +++ b/state.go @@ -6,7 +6,7 @@ import ( "time" "github.com/pkg/errors" - log "github.com/sirupsen/logrus" + "github.com/sirupsen/logrus" ) func loadState() error { @@ -16,11 +16,11 @@ func loadState() error { } if _, err := os.Stat(cfg.StateFile); err != nil { - log.WithError(err).Warn("Unable to load state, using empty state") + logrus.WithError(err).Warn("Unable to load state, using empty state") if os.IsNotExist(err) { return nil } - return errors.Wrap(err, "Unable to access state file") + return errors.Wrap(err, "accessing state file") } reqRetainerLock.Lock() @@ -28,12 +28,15 @@ func loadState() error { f, err := os.Open(cfg.StateFile) if err != nil { - return errors.Wrap(err, "Unable to open state file") + return errors.Wrap(err, "opening state file") } - defer f.Close() - - return errors.Wrap(json.NewDecoder(f).Decode(&reqRetainer), "Unable to decode state file") + defer func() { + if err := f.Close(); err != nil { + logrus.WithError(err).Error("closing state file (leaked fd)") + } + }() + return errors.Wrap(json.NewDecoder(f).Decode(&reqRetainer), "decoding state file") } func retainState() error { @@ -44,14 +47,18 @@ func retainState() error { f, err := os.Create(cfg.StateFile) if err != nil { - return errors.Wrap(err, "Unable to create state file") + return errors.Wrap(err, "creating state file") } - defer f.Close() + defer func() { + if err := f.Close(); err != nil { + logrus.WithError(err).Error("closing state file (leaked fd)") + } + }() reqRetainerLock.RLock() defer reqRetainerLock.RUnlock() - var tmpState = make(map[string]position) + tmpState := make(map[string]position) for m, p := range reqRetainer { if time.Since(p.Time) > cfg.StateTimeout { continue @@ -59,5 +66,5 @@ func retainState() error { tmpState[m] = p } - return errors.Wrap(json.NewEncoder(f).Encode(tmpState), "Unable to encode state file") + return errors.Wrap(json.NewEncoder(f).Encode(tmpState), "encoding state file") }