2020-01-06 20:57:17 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2020-01-06 23:49:40 +00:00
|
|
|
"time"
|
2020-01-06 20:57:17 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2023-10-15 18:04:22 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/sirupsen/logrus"
|
2020-01-06 20:57:17 +00:00
|
|
|
|
|
|
|
"github.com/Luzifer/rconfig/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
cfg = struct {
|
2020-01-06 23:49:40 +00:00
|
|
|
Listen string `flag:"listen" default:":3000" description:"Port/IP to listen on"`
|
|
|
|
LogLevel string `flag:"log-level" default:"info" description:"Log level (debug, info, warn, error, fatal)"`
|
|
|
|
StateFile string `flag:"state-file" default:"" description:"Where to store retained locations (empty for no state)"`
|
|
|
|
StateTimeout time.Duration `flag:"state-timeout" default:"24h" description:"When to drop retained states"`
|
|
|
|
VersionAndExit bool `flag:"version" default:"false" description:"Prints current version and exits"`
|
2020-01-06 20:57:17 +00:00
|
|
|
}{}
|
|
|
|
|
|
|
|
version = "dev"
|
|
|
|
)
|
|
|
|
|
2023-10-15 18:04:22 +00:00
|
|
|
func initApp() error {
|
2020-01-06 23:49:40 +00:00
|
|
|
rconfig.AutoEnv(true)
|
2020-01-06 20:57:17 +00:00
|
|
|
if err := rconfig.ParseAndValidate(&cfg); err != nil {
|
2023-10-15 18:04:22 +00:00
|
|
|
return errors.Wrap(err, "parsing CLI options")
|
2020-01-06 20:57:17 +00:00
|
|
|
}
|
|
|
|
|
2023-10-15 18:04:22 +00:00
|
|
|
l, err := logrus.ParseLevel(cfg.LogLevel)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "parsing log level")
|
2020-01-06 20:57:17 +00:00
|
|
|
}
|
2023-10-15 18:04:22 +00:00
|
|
|
logrus.SetLevel(l)
|
2020-01-06 20:57:17 +00:00
|
|
|
|
2023-10-15 18:04:22 +00:00
|
|
|
return nil
|
2020-01-06 20:57:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2023-10-15 18:04:22 +00:00
|
|
|
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")
|
2020-01-06 23:49:40 +00:00
|
|
|
}
|
|
|
|
|
2020-01-06 20:57:17 +00:00
|
|
|
r := mux.NewRouter()
|
|
|
|
|
|
|
|
r.HandleFunc("/", handleRedirectRandom).Methods(http.MethodGet)
|
|
|
|
|
|
|
|
r.PathPrefix("/asset/").Handler(
|
|
|
|
http.StripPrefix("/asset/", http.FileServer(http.Dir("frontend"))),
|
|
|
|
).Methods(http.MethodGet)
|
|
|
|
|
|
|
|
r.HandleFunc("/{mapID}", handleMapFrontend).Methods(http.MethodGet)
|
|
|
|
r.HandleFunc("/{mapID}", handleMapSubmit).Methods(http.MethodPut)
|
|
|
|
r.HandleFunc("/{mapID}/ws", handleMapSocket).Methods(http.MethodGet)
|
|
|
|
|
2023-10-15 18:04:22 +00:00
|
|
|
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")
|
|
|
|
}
|
2020-01-06 20:57:17 +00:00
|
|
|
}
|