refactor: configs dir
This commit is contained in:
3
.github/workflows/build.yml
vendored
3
.github/workflows/build.yml
vendored
@@ -34,8 +34,7 @@ jobs:
|
|||||||
name: codapi
|
name: codapi
|
||||||
path: |
|
path: |
|
||||||
build/codapi
|
build/codapi
|
||||||
|
configs/
|
||||||
images/
|
images/
|
||||||
*.json
|
|
||||||
codapi.service
|
|
||||||
Makefile
|
Makefile
|
||||||
retention-days: 7
|
retention-days: 7
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ builds:
|
|||||||
|
|
||||||
archives:
|
archives:
|
||||||
- files:
|
- files:
|
||||||
- "*.json"
|
- configs/*
|
||||||
- images/*
|
- images/*
|
||||||
- codapi.service
|
- codapi.service
|
||||||
- LICENSE
|
- LICENSE
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ func main() {
|
|||||||
port := flag.Int("port", 1313, "server port")
|
port := flag.Int("port", 1313, "server port")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
cfg, err := config.Read("config.json", "boxes.json", "commands.json")
|
cfg, err := config.Read("configs")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logx.Log("missing config file")
|
logx.Log("missing config file")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ Build the image:
|
|||||||
docker build --file images/python/Dockerfile --tag codapi/python:latest images/python/
|
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
|
```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
|
```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.
|
> 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
|
```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):
|
To apply the changed configuration, restart Codapi (as root):
|
||||||
|
|
||||||
|
|||||||
@@ -3,21 +3,28 @@ package config
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
configFilename = "config.json"
|
||||||
|
boxesFilename = "boxes.json"
|
||||||
|
commandsFilename = "commands.json"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Read reads application config from JSON files.
|
// Read reads application config from JSON files.
|
||||||
func Read(cfgPath, boxPath, cmdPath string) (*Config, error) {
|
func Read(path string) (*Config, error) {
|
||||||
cfg, err := ReadConfig(cfgPath)
|
cfg, err := ReadConfig(filepath.Join(path, configFilename))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
cfg, err = ReadBoxes(cfg, boxPath)
|
cfg, err = ReadBoxes(cfg, filepath.Join(path, boxesFilename))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
cfg, err = ReadCommands(cfg, cmdPath)
|
cfg, err = ReadCommands(cfg, filepath.Join(path, commandsFilename))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +1,11 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"path/filepath"
|
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestRead(t *testing.T) {
|
func TestRead(t *testing.T) {
|
||||||
cfgPath := filepath.Join("testdata", "config.json")
|
cfg, err := Read("testdata")
|
||||||
boxPath := filepath.Join("testdata", "boxes.json")
|
|
||||||
cmdPath := filepath.Join("testdata", "commands.json")
|
|
||||||
cfg, err := Read(cfgPath, boxPath, cmdPath)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unexpected error: %v", err)
|
t.Fatalf("unexpected error: %v", err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user