Add secret loading from path.

master
q3k 2016-10-06 21:03:43 +03:00
parent b7ac7d04cc
commit a91fb7911c
2 changed files with 18 additions and 0 deletions

View File

@ -24,6 +24,8 @@ Here's a few useful flags:
- `--db_path`: Path to the SQLite database. The default is `switchifier.db` in the current working directory.
- `--bind_address`: Host/port to bind to. By default `:8080`, so port 8080 on all interfaces.
- `--secret`: The preshared secret used to authenticate the client.
- `--secret_path`: A file to load the preshared secret from (`--secret` is ignored).
- `--logtostderr`: Log program output to stderr.
Future work

16
main.go
View File

@ -29,7 +29,9 @@ import (
"database/sql"
"encoding/json"
"flag"
"io/ioutil"
"net/http"
"os"
"strings"
"time"
@ -190,16 +192,30 @@ var (
dbPath string
bindAddress string
secret string
secret_path string
)
func main() {
flag.StringVar(&dbPath, "db_path", "./switchifier.db", "Path to the SQlite3 database.")
flag.StringVar(&bindAddress, "bind_address", ":8080", "Address to bind HTTP server to.")
flag.StringVar(&secret, "secret", "changeme", "Secret for state updates.")
flag.StringVar(&secret_path, "secret_path", "", "File with secret for state updates.")
flag.Parse()
if dbPath == "" {
glog.Exit("Please provide a database path.")
}
if secret_path != "" {
file, err := os.Open(secret_path)
if err != nil {
glog.Exit(err)
}
secretData, err := ioutil.ReadAll(file)
if err != nil {
glog.Exit(err)
}
secretParts := strings.Split(string(secretData), "\n")
secret = secretParts[0]
}
glog.Infof("Starting switchifier...")
db, err := sql.Open("sqlite3", dbPath)