From f7acde85c03e7b85e4c2b3c2ab93416b5a589108 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20=C5=A0trauch?= Date: Mon, 9 Dec 2024 02:00:38 +0100 Subject: [PATCH] node.json path instead of dir --- .gitea/workflows/build.yml | 15 --------------- Taskfile.yml | 2 +- cli/actions.go | 2 +- cli/config.go | 6 +++--- refresher/main.go | 25 +++++++++++++------------ 5 files changed, 18 insertions(+), 32 deletions(-) diff --git a/.gitea/workflows/build.yml b/.gitea/workflows/build.yml index 13f6cd3..20b9948 100644 --- a/.gitea/workflows/build.yml +++ b/.gitea/workflows/build.yml @@ -23,11 +23,6 @@ jobs: with: go-version: '1.23' - # - name: Install dependencies - # run: | - # sudo apt-get update - # sudo apt-get install -y task - - name: Run tests run: task test @@ -35,16 +30,6 @@ jobs: run: | task build VERSION=${{ github.event.inputs.version }} - # - name: Upload Release Asset - # uses: actions/upload-release-asset@v1 - # env: - # GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - # with: - # upload_url: ${{ github.event.release.upload_url }} - # asset_path: ./lobby2-${{ github.ref_name }}-amd64 - # asset_name: lobby2-${{ github.ref_name }}-amd64 - # asset_content_type: application/octet-stream - - uses: actions/forgejo-release@v2 with: token: ${{ secrets.GITHUB_TOKEN }} diff --git a/Taskfile.yml b/Taskfile.yml index 45706b0..913a567 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -19,4 +19,4 @@ tasks: cmds: - go mod tidy - mkdir -p bin - - go build -o bin/lobby2-{{ .VERSION }}-amd64 cli/*.go + - go build -o bin/lobby2-{{ .VERSION }}-linux-amd64 cli/*.go diff --git a/cli/actions.go b/cli/actions.go index 5336439..134b4d4 100644 --- a/cli/actions.go +++ b/cli/actions.go @@ -28,7 +28,7 @@ func masterAction(c *cli.Context) error { func nodeAction(c *cli.Context) error { cfg := GetConfig() - r := refresher.NewRefresher(cfg.NodeDirPath, cfg.ConfigPath) + r := refresher.NewRefresher(cfg.NodePath, cfg.ConfigPath) r.Loop() return nil diff --git a/cli/config.go b/cli/config.go index ccf4299..683b21c 100644 --- a/cli/config.go +++ b/cli/config.go @@ -10,9 +10,9 @@ type Config struct { APIListen string `envconfig:"LISTEN" default:"0.0.0.0:1352"` APIToken string `envconfig:"TOKEN" default:""` - DumpPath string `envconfig:"DUMP_PATH" default:"/var/lib/lobby2/nodes.json"` - ConfigPath string `envconfig:"CONFIG_PATH" default:"/var/lib/lobby2/config.json"` - NodeDirPath string `envconfig:"NODE_DIR_PATH" default:"/var/lib/lobby2/node"` + DumpPath string `envconfig:"DUMP_PATH" default:"/var/lib/lobby2/nodes.json"` + ConfigPath string `envconfig:"CONFIG_PATH" default:"/etc/lobby2/config.json"` + NodePath string `envconfig:"NODE_PATH" default:"/etc/lobby2/node.json"` DropAfterSeconds int64 `envconfig:"DROP_AFTER_SECONDS" default:"60"` } diff --git a/refresher/main.go b/refresher/main.go index 849eae1..101d707 100644 --- a/refresher/main.go +++ b/refresher/main.go @@ -7,25 +7,24 @@ import ( "log" "net/http" "os" - "path" + "path/filepath" "time" "gitea.ceperka.net/rosti/lobby2/nodes" ) const refreshIntervalSeconds = 15 -const nodeFileName = "node.json" // Refresher loads local node info and sends them to the master node. type Refresher struct { - configPath string - nodeDirPath string + configPath string + nodePath string } -func NewRefresher(nodeDirPath string, configPath string) *Refresher { +func NewRefresher(nodePath string, configPath string) *Refresher { return &Refresher{ - nodeDirPath: nodeDirPath, - configPath: configPath, + nodePath: nodePath, + configPath: configPath, } } @@ -113,7 +112,7 @@ func (r *Refresher) getConfig() (NodeConfig, error) { // TODO: rewrite this to load Node structure func (r *Refresher) loadNode() (*nodes.Node, error) { - filePath := path.Join(r.nodeDirPath, nodeFileName) + filePath := r.nodePath _, err := os.Stat(filePath) if os.IsNotExist(err) { err = r.initNodeFile() @@ -122,7 +121,7 @@ func (r *Refresher) loadNode() (*nodes.Node, error) { } } - content, err := os.ReadFile(path.Join(r.nodeDirPath, nodeFileName)) + content, err := os.ReadFile(r.nodePath) if err != nil { return nil, fmt.Errorf("failed to read file: %w", err) } @@ -156,7 +155,7 @@ func (r *Refresher) initNodeFile() error { return fmt.Errorf("failed to marshal node: %w", err) } - err = os.WriteFile(path.Join(r.nodeDirPath, nodeFileName), nodeBytes, 0640) + err = os.WriteFile(r.nodePath, nodeBytes, 0640) if err != nil { return fmt.Errorf("failed to write node file: %w", err) } @@ -165,9 +164,11 @@ func (r *Refresher) initNodeFile() error { } func (r *Refresher) createNodePath() error { - _, err := os.Stat(r.nodeDirPath) + d := filepath.Dir(r.nodePath) + + _, err := os.Stat(d) if os.IsNotExist(err) { - err = os.MkdirAll(r.nodeDirPath, 0755) + err = os.MkdirAll(d, 0755) if err != nil { return fmt.Errorf("failed to create node dir: %w", err) }