From 07b523cd4d18a86630a112d60ac30fa4f614dfc2 Mon Sep 17 00:00:00 2001 From: Anton Date: Mon, 4 Dec 2023 23:57:03 +0500 Subject: [PATCH] refactor: configs dir --- .github/workflows/build.yml | 3 +-- .goreleaser.yaml | 2 +- cmd/main.go | 2 +- boxes.json => configs/boxes.json | 0 commands.json => configs/commands.json | 0 config.json => configs/config.json | 0 docs/add-sandbox.md | 8 ++++---- internal/config/load.go | 15 +++++++++++---- internal/config/load_test.go | 6 +----- 9 files changed, 19 insertions(+), 17 deletions(-) rename boxes.json => configs/boxes.json (100%) rename commands.json => configs/commands.json (100%) rename config.json => configs/config.json (100%) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 8e7208c..45f64c5 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -34,8 +34,7 @@ jobs: name: codapi path: | build/codapi + configs/ images/ - *.json - codapi.service Makefile retention-days: 7 diff --git a/.goreleaser.yaml b/.goreleaser.yaml index 523e74c..ea03a73 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -11,7 +11,7 @@ builds: archives: - files: - - "*.json" + - configs/* - images/* - codapi.service - LICENSE diff --git a/cmd/main.go b/cmd/main.go index 4b0fd74..3ea8457 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -42,7 +42,7 @@ func main() { port := flag.Int("port", 1313, "server port") flag.Parse() - cfg, err := config.Read("config.json", "boxes.json", "commands.json") + cfg, err := config.Read("configs") if err != nil { logx.Log("missing config file") os.Exit(1) diff --git a/boxes.json b/configs/boxes.json similarity index 100% rename from boxes.json rename to configs/boxes.json diff --git a/commands.json b/configs/commands.json similarity index 100% rename from commands.json rename to configs/commands.json diff --git a/config.json b/configs/config.json similarity index 100% rename from config.json rename to configs/config.json diff --git a/docs/add-sandbox.md b/docs/add-sandbox.md index 12b0f05..40da229 100644 --- a/docs/add-sandbox.md +++ b/docs/add-sandbox.md @@ -45,7 +45,7 @@ Build the image: docker build --file images/python/Dockerfile --tag codapi/python:latest images/python/ ``` -And register the image as a Codapi _box_ in `boxes.json`: +And register the image as a Codapi _box_ in `configs/boxes.json`: ```json { @@ -56,7 +56,7 @@ And register the image as a Codapi _box_ in `boxes.json`: } ``` -Finally, let's configure what happens when the client executes the `run` command in the `python` sandbox. To do this, we edit `commands.json`: +Finally, let's configure what happens when the client executes the `run` command in the `python` sandbox. To do this, we edit `configs/commands.json`: ```json { @@ -80,7 +80,7 @@ This is essentially what it says: > When the client executes the `run` command in the `python` sandbox, save their code to the `main.py` file, then run it in the `python` box (Docker container) using the `python main.py` shell command. -What if we want to add another command (say, `test`) to the same sandbox? Let's edit `commands.json` again: +What if we want to add another command (say, `test`) to the same sandbox? Let's edit `configs/commands.json` again: ```json { @@ -104,7 +104,7 @@ What if we want to add another command (say, `test`) to the same sandbox? Let's } ``` -Besides configuring a different shell command, here we increased the maximum output size to 8Kb, as tests tend to be quite chatty (you can see the default value in `config.json`). +Besides configuring a different shell command, here we increased the maximum output size to 8Kb, as tests tend to be quite chatty (you can see the default value in `configs/config.json`). To apply the changed configuration, restart Codapi (as root): diff --git a/internal/config/load.go b/internal/config/load.go index aa417bb..789b963 100644 --- a/internal/config/load.go +++ b/internal/config/load.go @@ -3,21 +3,28 @@ package config import ( "encoding/json" "os" + "path/filepath" +) + +const ( + configFilename = "config.json" + boxesFilename = "boxes.json" + commandsFilename = "commands.json" ) // Read reads application config from JSON files. -func Read(cfgPath, boxPath, cmdPath string) (*Config, error) { - cfg, err := ReadConfig(cfgPath) +func Read(path string) (*Config, error) { + cfg, err := ReadConfig(filepath.Join(path, configFilename)) if err != nil { return nil, err } - cfg, err = ReadBoxes(cfg, boxPath) + cfg, err = ReadBoxes(cfg, filepath.Join(path, boxesFilename)) if err != nil { return nil, err } - cfg, err = ReadCommands(cfg, cmdPath) + cfg, err = ReadCommands(cfg, filepath.Join(path, commandsFilename)) if err != nil { return nil, err } diff --git a/internal/config/load_test.go b/internal/config/load_test.go index 14e8993..84ab62c 100644 --- a/internal/config/load_test.go +++ b/internal/config/load_test.go @@ -1,15 +1,11 @@ package config import ( - "path/filepath" "testing" ) func TestRead(t *testing.T) { - cfgPath := filepath.Join("testdata", "config.json") - boxPath := filepath.Join("testdata", "boxes.json") - cmdPath := filepath.Join("testdata", "commands.json") - cfg, err := Read(cfgPath, boxPath, cmdPath) + cfg, err := Read("testdata") if err != nil { t.Fatalf("unexpected error: %v", err) }