Skip to content

Commit

Permalink
ref: use json config file for server
Browse files Browse the repository at this point in the history
  • Loading branch information
b1naryth1ef committed Sep 12, 2021
1 parent b915326 commit 566d245
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 69 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
npm-debug.log*
yarn-debug.log*
yarn-error.log*
/scratch.txt
/scratch.txt
/config.json
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ An example of Sneaker can be viewed [here](https://hoggit.brrt.me/).

Sneaker features a backend server which connects to a TacView real-time server and process the stream of simulation data. Events are pushed via SSE to frontend clients at a configurable radar refresh rate.

### Example Configuration

```json
{
"servers": [
{"name": "test", "hostname": "localhost", "password": "example", "port": 12345, "radar_refresh_rate": 1}
]
}
```

## Web UI

The Sneaker web UI presents an emulated radar scope over top a [Open Street Map](https://openstreetmap.org) rendered via [maptalks](https://maptalks.org). The web UI is updated at a configurable simulated refresh rate (by default 5 seconds).
Expand Down
59 changes: 18 additions & 41 deletions cmd/sneaker-server/main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package main

import (
"errors"
"fmt"
"encoding/json"
"io/ioutil"
"log"
"os"
"strconv"
"strings"

"github.com/b1naryth1ef/sneaker/server"
"github.com/urfave/cli/v2"
Expand All @@ -19,55 +17,34 @@ func main() {
Flags: []cli.Flag{
&cli.StringFlag{
Name: "bind",
Value: "localhost:7788",
Usage: "the server bind address",
},
&cli.StringSliceFlag{
Name: "server",
&cli.PathFlag{
Name: "config",
Usage: "path to configuration file",
Required: true,
Usage: "provide a server connection in format name:host(:port(:password))",
},
},
Action: func(c *cli.Context) error {
var config server.Config
config.Bind = c.String("bind")

var servers = c.StringSlice(("server"))
if len(servers) == 0 {
return errors.New("no servers provided")
configData, err := ioutil.ReadFile(c.Path("config"))
if err != nil {
return err
}

config.Servers = make([]server.DCSServer, len(servers))
for idx, serverString := range servers {
parts := strings.SplitN(serverString, ":", 4)
if len(parts) < 2 {
return fmt.Errorf("Failed to parse server connection: %v", serverString)
}

var (
port int64
err error
)
if len(parts) >= 3 {
port, err = strconv.ParseInt(parts[2], 10, 64)
if err != nil {
return err
}
}

var password string
if len(parts) == 4 {
password = parts[3]
}
err = json.Unmarshal(configData, &config)
if err != nil {
return err
}

config.Servers[idx] = server.DCSServer{
Name: parts[0],
RadarRefreshRate: 5,
Hostname: parts[1],
Port: int(port),
Password: password,
}
if c.IsSet("bind") {
config.Bind = c.String("bind")
}
if config.Bind == "" {
config.Bind = "localhost:7788"
}

return server.Run(&config)
},
}
Expand Down
15 changes: 15 additions & 0 deletions server/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package server

type Config struct {
Bind string `json:"bind"`
Servers []TacViewServerConfig `json:"servers"`
}

type TacViewServerConfig struct {
Name string `json:"name"`
Hostname string `json:"hostname"`

RadarRefreshRate int64 `json:"radar_refresh_rate"`
Port int `json:"port"`
Password string `json:"password"`
}
11 changes: 0 additions & 11 deletions server/dcs.go

This file was deleted.

29 changes: 17 additions & 12 deletions server/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,13 @@ import (
"log"
"net/http"
"sync"
"time"

"github.com/alioygur/gores"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/cors"
)

const RADAR_REFRESH_RATE = time.Second * 5

type Config struct {
Bind string
Servers []DCSServer
}

type httpServer struct {
sync.Mutex

Expand All @@ -37,14 +29,25 @@ func newHttpServer(config *Config) *httpServer {

// Returns a list of available servers
func (h *httpServer) getServerList(w http.ResponseWriter, r *http.Request) {
gores.JSON(w, 200, h.config.Servers)
result := make([]*tacViewServerMetadata, len(h.config.Servers))
for idx, server := range h.config.Servers {
result[idx] = &tacViewServerMetadata{
Name: server.Name,
}
}

gores.JSON(w, 200, result)
}

type tacViewServerMetadata struct {
Name string `json:"name"`
}

// Return information about a specific server
func (h *httpServer) getServer(w http.ResponseWriter, r *http.Request) {
serverName := chi.URLParam(r, "serverName")

var server *DCSServer
var server *TacViewServerConfig
for _, checkServer := range h.config.Servers {
if checkServer.Name == serverName {
server = &checkServer
Expand All @@ -56,7 +59,9 @@ func (h *httpServer) getServer(w http.ResponseWriter, r *http.Request) {
return
}

gores.JSON(w, 200, server)
gores.JSON(w, 200, &tacViewServerMetadata{
Name: server.Name,
})
}

var errNoServerFound = errors.New("no server by that name was found")
Expand All @@ -70,7 +75,7 @@ func (h *httpServer) getOrCreateSession(serverName string) (*serverSession, erro
return existingSession, nil
}

var server *DCSServer
var server *TacViewServerConfig
for _, checkServer := range h.config.Servers {
if checkServer.Name == serverName {
server = &checkServer
Expand Down
10 changes: 7 additions & 3 deletions server/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,23 @@ type sessionStateData struct {
type serverSession struct {
sync.Mutex

server *DCSServer
server *TacViewServerConfig

subscriberIdx int
subscribers map[int]chan<- []byte
state sessionState
}

func newServerSession(server *DCSServer) (*serverSession, error) {
func newServerSession(server *TacViewServerConfig) (*serverSession, error) {
return &serverSession{server: server, subscribers: make(map[int]chan<- []byte)}, nil
}

func (s *serverSession) updateLoop() {
ticker := time.NewTicker(time.Second * time.Duration(s.server.RadarRefreshRate))
refreshRate := time.Duration(5)
if s.server.RadarRefreshRate != 0 {
refreshRate = time.Duration(s.server.RadarRefreshRate)
}
ticker := time.NewTicker(time.Second * refreshRate)

var currentOffset int64
for {
Expand Down
2 changes: 1 addition & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ function ServerContainer({ serverName }: { serverName: string }) {
let dcsMap: DCSMap | null = null;
if ((refLat > 28 && refLat < 32) && (refLng > 29 && refLng < 33)) {
dcsMap = Syria;
} else if ((refLat > 37 && refLat < 41) && (refLng > 33 && refLng < 38)) {
} else if ((refLat > 37 && refLat < 41) && (refLng > 31 && refLng < 38)) {
dcsMap = Caucasus;
} else if ((refLat > 18 && refLat < 24) && (refLng > 48 && refLng < 54)) {
dcsMap = PersianGulf;
Expand Down

0 comments on commit 566d245

Please sign in to comment.