From 4754eb539c46315fb418682f9430d94be2befd42 Mon Sep 17 00:00:00 2001 From: Shane Clark Date: Thu, 23 Jan 2025 20:56:02 -0700 Subject: [PATCH] Commit of mostly working code. --- .gitignore | 17 +++- client.go | 25 ++++++ go.mod | 36 +++++++++ go.sum | 85 +++++++++++++++++++ hub.go | 29 +++++++ main.go | 233 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 423 insertions(+), 2 deletions(-) create mode 100644 client.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 hub.go create mode 100644 main.go diff --git a/.gitignore b/.gitignore index f775b6b..557eb0d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,7 @@ -# ---> Go +# Created by https://www.toptal.com/developers/gitignore/api/go,jetbrains+all +# Edit at https://www.toptal.com/developers/gitignore?templates=go,jetbrains+all + +### Go ### # If you prefer the allow list template instead of the deny list, see community template: # https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore # @@ -21,7 +24,7 @@ # Go workspace file go.work -# ---> JetBrains +### JetBrains+all ### # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 @@ -100,3 +103,13 @@ fabric.properties # Android studio 3.1+ serialized cache file .idea/caches/build_file_checksums.ser +### JetBrains+all Patch ### +# Ignore everything but code style settings and run configurations +# that are supposed to be shared within teams. + +.idea/* + +!.idea/codeStyles +!.idea/runConfigurations + +# End of https://www.toptal.com/developers/gitignore/api/go,jetbrains+all diff --git a/client.go b/client.go new file mode 100644 index 0000000..08bfd49 --- /dev/null +++ b/client.go @@ -0,0 +1,25 @@ +package main + +import "github.com/gorilla/websocket" + +type Client struct { + hub *Hub + conn *websocket.Conn +} + +func (c *Client) close() { + c.hub.unregisterClient(c) + _ = c.conn.Close() +} + +// keepAlive This keeps the connection alive until a disconnect happens. +func (c *Client) keepAlive() { + defer c.close() + + for { + _, _, err := c.conn.ReadMessage() + if err != nil { + break + } + } +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..a71c677 --- /dev/null +++ b/go.mod @@ -0,0 +1,36 @@ +module bigduckie.dev/edas + +go 1.23.5 + +require ( + github.com/bytedance/sonic v1.11.6 // indirect + github.com/bytedance/sonic/loader v0.1.1 // indirect + github.com/cloudwego/base64x v0.1.4 // indirect + github.com/cloudwego/iasm v0.2.0 // indirect + github.com/fsnotify/fsnotify v1.8.0 // indirect + github.com/gabriel-vasile/mimetype v1.4.3 // indirect + github.com/gin-contrib/sse v0.1.0 // indirect + github.com/gin-gonic/gin v1.10.0 // indirect + github.com/go-playground/locales v0.14.1 // indirect + github.com/go-playground/universal-translator v0.18.1 // indirect + github.com/go-playground/validator/v10 v10.20.0 // indirect + github.com/goccy/go-json v0.10.2 // indirect + github.com/gorilla/websocket v1.5.3 // indirect + github.com/json-iterator/go v1.1.12 // indirect + github.com/klauspost/cpuid/v2 v2.2.7 // indirect + github.com/leodido/go-urn v1.4.0 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect + github.com/pelletier/go-toml/v2 v2.2.2 // indirect + github.com/twitchyliquid64/golang-asm v0.15.1 // indirect + github.com/ugorji/go/codec v1.2.12 // indirect + github.com/urfave/cli/v3 v3.0.0-beta1 // indirect + golang.org/x/arch v0.8.0 // indirect + golang.org/x/crypto v0.23.0 // indirect + golang.org/x/net v0.25.0 // indirect + golang.org/x/sys v0.20.0 // indirect + golang.org/x/text v0.15.0 // indirect + google.golang.org/protobuf v1.34.1 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..9b47b41 --- /dev/null +++ b/go.sum @@ -0,0 +1,85 @@ +github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0= +github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4= +github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM= +github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU= +github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y= +github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w= +github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg= +github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/fsnotify/fsnotify v1.8.0 h1:dAwr6QBTBZIkG8roQaJjGof0pp0EeF+tNV7YBP3F/8M= +github.com/fsnotify/fsnotify v1.8.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0= +github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0= +github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk= +github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= +github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= +github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU= +github.com/gin-gonic/gin v1.10.0/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T7kzp5Y= +github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= +github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= +github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= +github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= +github.com/go-playground/validator/v10 v10.20.0 h1:K9ISHbSaI0lyB2eWMPJo+kOS/FBExVwjEviJTixqxL8= +github.com/go-playground/validator/v10 v10.20.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM= +github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= +github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg= +github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= +github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= +github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= +github.com/klauspost/cpuid/v2 v2.2.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuVLDM= +github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= +github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M= +github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ= +github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= +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/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= +github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= +github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM= +github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= +github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= +github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE= +github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= +github.com/urfave/cli/v3 v3.0.0-beta1 h1:6DTaaUarcM0wX7qj5Hcvs+5Dm3dyUTBbEwIWAjcw9Zg= +github.com/urfave/cli/v3 v3.0.0-beta1/go.mod h1:FnIeEMYu+ko8zP1F9Ypr3xkZMIDqW3DR92yUtY39q1Y= +golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= +golang.org/x/arch v0.8.0 h1:3wRIsP3pM4yUptoR96otTUOXI367OS0+c9eeRi9doIc= +golang.org/x/arch v0.8.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys= +golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI= +golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8= +golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac= +golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +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/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk= +golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +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/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50= +rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4= diff --git a/hub.go b/hub.go new file mode 100644 index 0000000..4cdd6eb --- /dev/null +++ b/hub.go @@ -0,0 +1,29 @@ +package main + +import "github.com/gorilla/websocket" + +type Hub struct { + clients map[*Client]bool +} + +func HubNew() *Hub { + return &Hub{ + clients: make(map[*Client]bool), + } +} + +func (h *Hub) registerClient(client *Client) { + h.clients[client] = true +} + +func (h *Hub) unregisterClient(client *Client) { + if _, ok := h.clients[client]; ok { + delete(h.clients, client) + } +} + +func (h *Hub) sendClient(data []byte) { + for client := range h.clients { + client.conn.WriteMessage(websocket.TextMessage, data) + } +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..c7bda0b --- /dev/null +++ b/main.go @@ -0,0 +1,233 @@ +package main + +import ( + "context" + "github.com/fsnotify/fsnotify" + "github.com/gin-gonic/gin" + "github.com/gorilla/websocket" + "github.com/urfave/cli/v3" + "log" + "net/http" + "os" + "path/filepath" + "regexp" + "strings" +) + +var upgrader = websocket.Upgrader{CheckOrigin: func(r *http.Request) bool { return true }} +var previousLine = make(map[string]int) + +func main() { + cmd := &cli.Command{ + Name: "edas", + Usage: "an api for reading data from elite dangerous", + Flags: []cli.Flag{ + &cli.StringFlag{Name: "path", Value: "%USERPROFILE%\\Saved Games\\Frontier Developments\\Elite Dangerous"}, + }, + Action: func(ctx context.Context, cmd *cli.Command) error { + r := gin.Default() + + /* These are all the routes that just serve json data straight from the files. */ + r.GET("/backpack", func(c *gin.Context) { + b, err := os.ReadFile(ExpandPath(filepath.Join(cmd.String("path"), "Backpack.json"))) + if err != nil { + c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + } + + c.Data(200, "application/json; charset=utf-8", b) + }) + + r.GET("/cargo", func(c *gin.Context) { + b, err := os.ReadFile(ExpandPath(filepath.Join(cmd.String("path"), "Cargo.json"))) + if err != nil { + c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + } + + c.Data(200, "application/json; charset=utf-8", b) + }) + + r.GET("/fcmaterials", func(c *gin.Context) { + b, err := os.ReadFile(ExpandPath(filepath.Join(cmd.String("path"), "FCMaterials.json"))) + if err != nil { + c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + } + + c.Data(200, "application/json; charset=utf-8", b) + }) + + r.GET("/market", func(c *gin.Context) { + b, err := os.ReadFile(ExpandPath(filepath.Join(cmd.String("path"), "Market.json"))) + if err != nil { + c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + } + + c.Data(200, "application/json; charset=utf-8", b) + }) + + r.GET("/modulesinfo", func(c *gin.Context) { + b, err := os.ReadFile(ExpandPath(filepath.Join(cmd.String("path"), "ModulesInfo.json"))) + if err != nil { + c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + } + + c.Data(200, "application/json; charset=utf-8", b) + }) + + r.GET("/navroute", func(c *gin.Context) { + b, err := os.ReadFile(ExpandPath(filepath.Join(cmd.String("path"), "NavRoute.json"))) + if err != nil { + c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + } + + c.Data(200, "application/json; charset=utf-8", b) + }) + + r.GET("/outfitting", func(c *gin.Context) { + b, err := os.ReadFile(ExpandPath(filepath.Join(cmd.String("path"), "Outfitting.json"))) + if err != nil { + c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + } + + c.Data(200, "application/json; charset=utf-8", b) + }) + + r.GET("/shiplocker", func(c *gin.Context) { + b, err := os.ReadFile(ExpandPath(filepath.Join(cmd.String("path"), "ShipLocker.json"))) + if err != nil { + c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + } + + c.Data(200, "application/json; charset=utf-8", b) + }) + + r.GET("/shipyard", func(c *gin.Context) { + b, err := os.ReadFile(ExpandPath(filepath.Join(cmd.String("path"), "Shipyard.json"))) + if err != nil { + c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + } + + c.Data(200, "application/json; charset=utf-8", b) + }) + + r.GET("/status", func(c *gin.Context) { + b, err := os.ReadFile(ExpandPath(filepath.Join(cmd.String("path"), "Status.json"))) + if err != nil { + c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + } + + c.Data(200, "application/json; charset=utf-8", b) + }) + + /* Shared variables for the watcher and websocket. */ + hub := HubNew() + + /* Read in all existing Journal files so we know what to ignore. */ + files, err := filepath.Glob(ExpandPath(filepath.Join(cmd.String("path"), "Journal.*.log"))) + if err != nil { + return err + } + + for _, f := range files { + data, err := os.ReadFile(f) + if err != nil { + return err + } + + lines := strings.Split(string(data), "\n") + previousLine[filepath.Base(f)] = len(lines) + } + + /* This is the watcher that waits for changes to the journal files. */ + watcher, err := fsnotify.NewWatcher() + if err != nil { + return err + } + defer watcher.Close() + + go func() { + for { + select { + case event, ok := <-watcher.Events: + if !ok { + return + } + + if event.Op.Has(fsnotify.Write) { + data, err := processChangedFile(event.Name) + if err != nil { + log.Println(err) + continue + } + hub.sendClient(data) + } + } + } + }() + + err = watcher.Add(ExpandPath(cmd.String("path"))) + if err != nil { + return err + } + + /* This is the endpoint for the websocket that publishes events. */ + r.GET("/journal", func(c *gin.Context) { + w, r := c.Writer, c.Request + con, err := upgrader.Upgrade(w, r, nil) + if err != nil { + log.Print(err) + return + } + client := &Client{hub: hub, conn: con} + client.hub.registerClient(client) + + go client.keepAlive() + }) + + return r.Run(":3000") + }, + } + + if err := cmd.Run(context.Background(), os.Args); err != nil { + log.Fatal(err) + } +} + +func ExpandPath(path string) string { + re := regexp.MustCompile(`(?m)%([^%]+)%`) + return os.ExpandEnv(re.ReplaceAllString(path, "${$1}")) +} + +func processChangedFile(path string) ([]byte, error) { + switch filepath.Base(path) { + case "Backpack.json", + "Cargo.json", + "FCMaterials.json", + "Market.json", + "ModulesInfo.json", + "NavRoute.json", + "Outfitting.json", + "ShipLocker.json", + "Shipyard.json", + "Status.json": + return os.ReadFile(path) + default: + if strings.HasPrefix(filepath.Base(path), "Journal") { + data, err := os.ReadFile(path) + if err != nil { + return nil, err + } + + offset, ok := previousLine[filepath.Base(path)] + if !ok { + offset = 0 + } + + lines := strings.Split(string(data), "\n") + nextLine := lines[offset-1] + previousLine[filepath.Base(path)]++ + + return []byte(nextLine), nil + } + } + return nil, nil +}