Skip to content

Commit

Permalink
feature: Replace config file with cli args
Browse files Browse the repository at this point in the history
  • Loading branch information
g3force committed Jul 18, 2020
1 parent 81c8b04 commit 97547bc
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 77 deletions.
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,3 @@ yarn-error.log*
*.njsproj
*.sln
*.sw?

# application specific
/config
1 change: 0 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,5 @@ RUN GOOS=linux GOARCH=amd64 packr build -o ../../release/ssl-status-board_linux_
FROM alpine:3.9
COPY --from=build_go /go/src/github.com/RoboCup-SSL/ssl-status-board/release/ssl-status-board_linux_amd64 /app/ssl-status-board
EXPOSE 8082
WORKDIR "/"
ENTRYPOINT ["/app/ssl-status-board"]
CMD ["/app/ssl-status-board"]
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ You can also use pre-build docker images:
```shell script
docker pull robocupssl/ssl-status-board
docker run -p 8082:8082 robocupssl/ssl-status-board
# if you want to pass in the config file:
docker run -p 8082:8082 -v "$(pwd)/config:/config" ssl-status-board
```

By default, the UI is available at http://localhost:8082
Expand Down
30 changes: 8 additions & 22 deletions cmd/ssl-status-board/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,47 +8,33 @@ import (
"net/http"
)

var address = flag.String("address", ":8082", "The address on which the UI and API is served")
var configFile = flag.String("c", "config/board-config.yaml", "The config file to use")

func main() {
var config = board.DefaultConfig()
var address = flag.String("address", config.ListenAddress, "The address on which the UI and API is served")
var refereeAddress = flag.String("refereeAddress", config.RefereeConnection.MulticastAddress, "The multicast address of ssl-game-controller")
flag.Parse()

config := loadConfig(*configFile)
config.ListenAddress = *address
config.RefereeConnection.MulticastAddress = *refereeAddress

refereeBoard := board.NewBoard(config.RefereeConnection)
go refereeBoard.HandleIncomingMessages()
http.HandleFunc(config.RefereeConnection.SubscribePath, refereeBoard.WsHandler)

setupUi()
setupUi(config.ListenAddress)

err := http.ListenAndServe(config.ListenAddress, nil)
if err != nil {
log.Fatal(err)
}
}

func setupUi() {
func setupUi(listenAddress string) {
box := packr.NewBox("../../dist")
http.Handle("/", http.FileServer(box))
if box.Has("index.html") {
log.Printf("UI is available at http://%v", *address)
log.Printf("UI is available at http://%v", listenAddress)
} else {
log.Print("Backend-only version started. Run the UI separately or get a binary that has the UI included")
}
}

// loadConfig loads the config
func loadConfig(configFileName string) board.Config {
cfg, err := board.ReadConfig(configFileName)
if err != nil {
log.Printf("Could not load config: %v", err)
err = cfg.WriteTo(configFileName)
if err != nil {
log.Printf("Failed to write a default config file to %v: %v", configFileName, err)
} else {
log.Println("New default config has been written to", configFileName)
}
}
return cfg
}
9 changes: 0 additions & 9 deletions docker-compose.yaml

This file was deleted.

40 changes: 0 additions & 40 deletions pkg/board/serverConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@ package board

import (
"encoding/json"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
"io/ioutil"
"log"
"os"
"path/filepath"
"time"
)

Expand Down Expand Up @@ -38,40 +32,6 @@ func (c Config) String() string {
return string(str)
}

// ReadConfig reads the server config from a yaml file
func ReadConfig(fileName string) (config Config, err error) {
config = DefaultConfig()
f, err := os.Open(fileName)
if err != nil {
return
}
d, err := ioutil.ReadAll(f)
if err != nil {
log.Fatalln("Could not read config file: ", err)
}
err = yaml.Unmarshal(d, &config)
if err != nil {
log.Fatalln("Could not unmarshal config file: ", err)
}
return
}

// WriteTo writes the config to the specified file
func (c *Config) WriteTo(fileName string) (err error) {
b, err := yaml.Marshal(c)
if err != nil {
err = errors.Wrapf(err, "Could not marshal config %v", c)
return
}
err = os.MkdirAll(filepath.Dir(fileName), 0755)
if err != nil {
err = errors.Wrapf(err, "Could not create directory for config file: %v", fileName)
return
}
err = ioutil.WriteFile(fileName, b, 0600)
return
}

// DefaultConfig creates a config instance filled with default values
func DefaultConfig() Config {
return Config{
Expand Down

0 comments on commit 97547bc

Please sign in to comment.