mirror of
https://github.com/Luzifer/mapshare.git
synced 2024-11-09 13:40:00 +00:00
Update dependencies, fix linter errors, modernize code
Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
b04c7e48cf
commit
36a27f91b9
8 changed files with 161 additions and 106 deletions
|
@ -1,6 +1,8 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
set -euxo pipefail
|
set -euxo pipefail
|
||||||
|
|
||||||
|
leaflet_ver="1.7.1"
|
||||||
|
|
||||||
css_deps=(
|
css_deps=(
|
||||||
# Must-have order
|
# Must-have order
|
||||||
npm/bootstrap@4/dist/css/bootstrap.min.css
|
npm/bootstrap@4/dist/css/bootstrap.min.css
|
||||||
|
@ -8,7 +10,7 @@ css_deps=(
|
||||||
npm/bootstrap-vue@2/dist/bootstrap-vue.min.css
|
npm/bootstrap-vue@2/dist/bootstrap-vue.min.css
|
||||||
|
|
||||||
# Other packages
|
# Other packages
|
||||||
npm/leaflet@1.7.1/dist/leaflet.min.css
|
npm/leaflet@${leaflet_ver}/dist/leaflet.min.css
|
||||||
)
|
)
|
||||||
|
|
||||||
js_deps=(
|
js_deps=(
|
||||||
|
@ -18,21 +20,22 @@ js_deps=(
|
||||||
npm/vue-i18n@8.21.0/dist/vue-i18n.min.js
|
npm/vue-i18n@8.21.0/dist/vue-i18n.min.js
|
||||||
|
|
||||||
# Other packages
|
# Other packages
|
||||||
npm/axios@0.20.0/dist/axios.min.js
|
npm/leaflet@${leaflet_ver}/dist/leaflet.min.js
|
||||||
npm/leaflet@1.7.1/dist/leaflet.min.js
|
|
||||||
npm/moment@2.27.0/min/moment.min.js
|
npm/moment@2.27.0/min/moment.min.js
|
||||||
)
|
)
|
||||||
|
|
||||||
leaflet_images=(
|
leaflet_images=(
|
||||||
|
layers.png
|
||||||
|
layers-2x.png
|
||||||
marker-icon.png
|
marker-icon.png
|
||||||
marker-icon-2x.png
|
marker-icon-2x.png
|
||||||
marker-shadow.png
|
marker-shadow.png
|
||||||
)
|
)
|
||||||
|
|
||||||
rm -rf frontend/leaflet
|
rm -rf frontend/images
|
||||||
mkdir -p frontend/leaflet
|
mkdir -p frontend/images
|
||||||
for img in "${leaflet_images[@]}"; do
|
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
|
done
|
||||||
|
|
||||||
IFS=','
|
IFS=','
|
||||||
|
|
2
frontend/.gitignore
vendored
2
frontend/.gitignore
vendored
|
@ -1,4 +1,4 @@
|
||||||
combine.css
|
combine.css
|
||||||
combine.js
|
combine.js
|
||||||
fontawesome
|
fontawesome
|
||||||
leaflet
|
images
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
/* global VueI18n */
|
||||||
|
|
||||||
const get_locale = (fallback = 'en') => {
|
const get_locale = (fallback = 'en') => {
|
||||||
const urlParams = new URLSearchParams(window.location.search)
|
const urlParams = new URLSearchParams(window.location.search)
|
||||||
|
|
||||||
|
@ -60,16 +62,16 @@ window.app = new Vue({
|
||||||
zoom() {
|
zoom() {
|
||||||
const params = new URLSearchParams(window.location.search)
|
const params = new URLSearchParams(window.location.search)
|
||||||
return params.has('zoom') ? parseInt(params.get('zoom')) : 13
|
return params.has('zoom') ? parseInt(params.get('zoom')) : 13
|
||||||
}
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
created() {
|
created() {
|
||||||
// Use defaults with custom icon paths
|
// Use defaults with custom icon paths
|
||||||
this.icon = L.icon({
|
this.icon = L.icon({
|
||||||
...L.Icon.Default.prototype.options,
|
...L.Icon.Default.prototype.options,
|
||||||
iconUrl: '/asset/leaflet/marker-icon.png',
|
iconUrl: '/asset/images/marker-icon.png',
|
||||||
iconRetinaUrl: '/asset/leaflet/marker-icon-2x.png',
|
iconRetinaUrl: '/asset/images/marker-icon-2x.png',
|
||||||
shadowUrl: '/asset/leaflet/marker-shadow.png',
|
shadowUrl: '/asset/images/marker-shadow.png',
|
||||||
})
|
})
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -159,7 +161,14 @@ window.app = new Vue({
|
||||||
sender_id: this.browserID,
|
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))
|
.catch(err => console.error(err))
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
23
go.mod
23
go.mod
|
@ -1,14 +1,19 @@
|
||||||
module github.com/Luzifer/mapshare
|
module github.com/Luzifer/mapshare
|
||||||
|
|
||||||
go 1.13
|
go 1.21
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/Luzifer/rconfig/v2 v2.2.1
|
github.com/Luzifer/rconfig/v2 v2.4.0
|
||||||
github.com/gofrs/uuid v3.2.0+incompatible
|
github.com/gofrs/uuid v4.4.0+incompatible
|
||||||
github.com/gorilla/mux v1.7.3
|
github.com/gorilla/mux v1.8.0
|
||||||
github.com/gorilla/websocket v1.4.1
|
github.com/gorilla/websocket v1.5.0
|
||||||
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
|
github.com/pkg/errors v0.9.1
|
||||||
github.com/pkg/errors v0.8.1
|
github.com/sirupsen/logrus v1.9.3
|
||||||
github.com/sirupsen/logrus v1.4.2
|
)
|
||||||
golang.org/x/sys v0.0.0-20200106162015-b016eb3dc98e // indirect
|
|
||||||
|
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
|
||||||
)
|
)
|
||||||
|
|
64
go.sum
64
go.sum
|
@ -1,32 +1,38 @@
|
||||||
github.com/Luzifer/rconfig v2.2.0+incompatible h1:Kle3+rshPM7LxciOheaR4EfHUzibkDDGws04sefQ5m8=
|
github.com/Luzifer/rconfig/v2 v2.4.0 h1:MAdymTlExAZ8mx5VG8xOFAtFQSpWBipKYQHPOmYTn9o=
|
||||||
github.com/Luzifer/rconfig/v2 v2.2.1 h1:zcDdLQlnlzwcBJ8E0WFzOkQE1pCMn3EbX0dFYkeTczg=
|
github.com/Luzifer/rconfig/v2 v2.4.0/go.mod h1:hWF3ZVSusbYlg5bEvCwalEyUSY+0JPJWUiIu7rBmav8=
|
||||||
github.com/Luzifer/rconfig/v2 v2.2.1/go.mod h1:OKIX0/JRZrPJ/ZXXWklQEFXA6tBfWaljZbW37w+sqBw=
|
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/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 v4.4.0+incompatible h1:3qXRTX8/NbyulANqlc0lchS1gqAVxRgsuW1YrTJupqA=
|
||||||
github.com/gofrs/uuid v3.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
|
github.com/gofrs/uuid v4.4.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
|
||||||
github.com/gorilla/mux v1.7.3 h1:gnP5JzjVOuiZD07fKKToCAOjS0yOpj/qPETTXCCS6hw=
|
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
|
||||||
github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
|
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
|
||||||
github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM=
|
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
|
||||||
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||||
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
|
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
|
||||||
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
|
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
|
||||||
github.com/konsorten/go-windows-terminal-sequences v1.0.2 h1:DB17ag19krx9CFsz4o3enTrPXyIXCl+2iCXH/aMAp9s=
|
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
|
||||||
github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
|
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||||
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
|
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||||
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
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/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.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
|
||||||
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
|
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
|
||||||
github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
|
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
|
||||||
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
|
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||||
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
|
||||||
golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc=
|
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.0.0-20200106162015-b016eb3dc98e h1:LwyF2AFISC9nVbS6MgzsaQNSUsRXI49GS+YQ5KX/QH0=
|
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
|
||||||
golang.org/x/sys v0.0.0-20200106162015-b016eb3dc98e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
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/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/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||||
gopkg.in/validator.v2 v2.0.0-20180514200540-135c24b11c19/go.mod h1:o4V0GXN9/CAmCsvJ0oXYZvrZOe7syiDZSN1GWGZTGzc=
|
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||||
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
|
gopkg.in/validator.v2 v2.0.1 h1:xF0KWyGWXm/LM2G1TrEjqOu4pa6coO9AlWSf3msVfDY=
|
||||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
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=
|
||||||
|
|
25
handler.go
25
handler.go
|
@ -9,7 +9,12 @@ import (
|
||||||
"github.com/gofrs/uuid"
|
"github.com/gofrs/uuid"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
log "github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
websocketBufferSize = 1024
|
||||||
|
websocketUpdatePoolSize = 10
|
||||||
)
|
)
|
||||||
|
|
||||||
type position struct {
|
type position struct {
|
||||||
|
@ -27,8 +32,8 @@ var (
|
||||||
reqRetainerLock = new(sync.RWMutex)
|
reqRetainerLock = new(sync.RWMutex)
|
||||||
|
|
||||||
upgrader = websocket.Upgrader{
|
upgrader = websocket.Upgrader{
|
||||||
ReadBufferSize: 1024,
|
ReadBufferSize: websocketBufferSize,
|
||||||
WriteBufferSize: 1024,
|
WriteBufferSize: websocketBufferSize,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -45,7 +50,7 @@ func handleMapSocket(w http.ResponseWriter, r *http.Request) {
|
||||||
vars = mux.Vars(r)
|
vars = mux.Vars(r)
|
||||||
mapID = vars["mapID"]
|
mapID = vars["mapID"]
|
||||||
sockID = uuid.Must(uuid.NewV4()).String()
|
sockID = uuid.Must(uuid.NewV4()).String()
|
||||||
updates = make(chan position, 10)
|
updates = make(chan position, websocketUpdatePoolSize)
|
||||||
)
|
)
|
||||||
|
|
||||||
// Register update channel
|
// Register update channel
|
||||||
|
@ -75,15 +80,19 @@ func handleMapSocket(w http.ResponseWriter, r *http.Request) {
|
||||||
// Open socket
|
// Open socket
|
||||||
conn, err := upgrader.Upgrade(w, r, nil)
|
conn, err := upgrader.Upgrade(w, r, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.WithError(err).Debug("Unable to open websocket")
|
logrus.WithError(err).Debug("opening websocket")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer conn.Close()
|
defer func() {
|
||||||
|
if err := conn.Close(); err != nil {
|
||||||
|
logrus.WithError(err).Error("closing socket connection (leaked fd)")
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
// Send updates
|
// Send updates
|
||||||
for pos := range updates {
|
for pos := range updates {
|
||||||
if err = conn.WriteJSON(pos); err != nil {
|
if err = conn.WriteJSON(pos); err != nil {
|
||||||
log.WithError(err).Debug("Unable to send position")
|
logrus.WithError(err).Debug("sending position")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -113,7 +122,7 @@ func handleMapSubmit(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
if err := retainState(); err != nil {
|
if err := retainState(); err != nil {
|
||||||
log.WithError(err).Error("Unable to retain state to disk")
|
logrus.WithError(err).Error("retaining state to disk")
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
|
46
main.go
46
main.go
|
@ -3,11 +3,11 @@ package main
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
log "github.com/sirupsen/logrus"
|
"github.com/pkg/errors"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
|
||||||
"github.com/Luzifer/rconfig/v2"
|
"github.com/Luzifer/rconfig/v2"
|
||||||
)
|
)
|
||||||
|
@ -24,27 +24,34 @@ var (
|
||||||
version = "dev"
|
version = "dev"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func initApp() error {
|
||||||
rconfig.AutoEnv(true)
|
rconfig.AutoEnv(true)
|
||||||
if err := rconfig.ParseAndValidate(&cfg); err != nil {
|
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 {
|
l, err := logrus.ParseLevel(cfg.LogLevel)
|
||||||
fmt.Printf("mapshare %s\n", version)
|
if err != nil {
|
||||||
os.Exit(0)
|
return errors.Wrap(err, "parsing log level")
|
||||||
}
|
}
|
||||||
|
logrus.SetLevel(l)
|
||||||
|
|
||||||
if l, err := log.ParseLevel(cfg.LogLevel); err != nil {
|
return nil
|
||||||
log.WithError(err).Fatal("Unable to parse log level")
|
|
||||||
} else {
|
|
||||||
log.SetLevel(l)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
if err := loadState(); err != nil {
|
var err error
|
||||||
log.WithError(err).Fatal("Unable to load state")
|
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()
|
r := mux.NewRouter()
|
||||||
|
@ -59,5 +66,14 @@ func main() {
|
||||||
r.HandleFunc("/{mapID}", handleMapSubmit).Methods(http.MethodPut)
|
r.HandleFunc("/{mapID}", handleMapSubmit).Methods(http.MethodPut)
|
||||||
r.HandleFunc("/{mapID}/ws", handleMapSocket).Methods(http.MethodGet)
|
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")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
29
state.go
29
state.go
|
@ -6,7 +6,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
log "github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
func loadState() error {
|
func loadState() error {
|
||||||
|
@ -16,11 +16,11 @@ func loadState() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := os.Stat(cfg.StateFile); err != nil {
|
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) {
|
if os.IsNotExist(err) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return errors.Wrap(err, "Unable to access state file")
|
return errors.Wrap(err, "accessing state file")
|
||||||
}
|
}
|
||||||
|
|
||||||
reqRetainerLock.Lock()
|
reqRetainerLock.Lock()
|
||||||
|
@ -28,12 +28,15 @@ func loadState() error {
|
||||||
|
|
||||||
f, err := os.Open(cfg.StateFile)
|
f, err := os.Open(cfg.StateFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "Unable to open state file")
|
return errors.Wrap(err, "opening state file")
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer func() {
|
||||||
|
if err := f.Close(); err != nil {
|
||||||
return errors.Wrap(json.NewDecoder(f).Decode(&reqRetainer), "Unable to decode state file")
|
logrus.WithError(err).Error("closing state file (leaked fd)")
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
return errors.Wrap(json.NewDecoder(f).Decode(&reqRetainer), "decoding state file")
|
||||||
}
|
}
|
||||||
|
|
||||||
func retainState() error {
|
func retainState() error {
|
||||||
|
@ -44,14 +47,18 @@ func retainState() error {
|
||||||
|
|
||||||
f, err := os.Create(cfg.StateFile)
|
f, err := os.Create(cfg.StateFile)
|
||||||
if err != nil {
|
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()
|
reqRetainerLock.RLock()
|
||||||
defer reqRetainerLock.RUnlock()
|
defer reqRetainerLock.RUnlock()
|
||||||
|
|
||||||
var tmpState = make(map[string]position)
|
tmpState := make(map[string]position)
|
||||||
for m, p := range reqRetainer {
|
for m, p := range reqRetainer {
|
||||||
if time.Since(p.Time) > cfg.StateTimeout {
|
if time.Since(p.Time) > cfg.StateTimeout {
|
||||||
continue
|
continue
|
||||||
|
@ -59,5 +66,5 @@ func retainState() error {
|
||||||
tmpState[m] = p
|
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")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue