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. - `--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. - `--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`: 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 Future work

16
main.go
View File

@ -29,7 +29,9 @@ import (
"database/sql" "database/sql"
"encoding/json" "encoding/json"
"flag" "flag"
"io/ioutil"
"net/http" "net/http"
"os"
"strings" "strings"
"time" "time"
@ -190,16 +192,30 @@ var (
dbPath string dbPath string
bindAddress string bindAddress string
secret string secret string
secret_path string
) )
func main() { func main() {
flag.StringVar(&dbPath, "db_path", "./switchifier.db", "Path to the SQlite3 database.") 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(&bindAddress, "bind_address", ":8080", "Address to bind HTTP server to.")
flag.StringVar(&secret, "secret", "changeme", "Secret for state updates.") flag.StringVar(&secret, "secret", "changeme", "Secret for state updates.")
flag.StringVar(&secret_path, "secret_path", "", "File with secret for state updates.")
flag.Parse() flag.Parse()
if dbPath == "" { if dbPath == "" {
glog.Exit("Please provide a database path.") 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...") glog.Infof("Starting switchifier...")
db, err := sql.Open("sqlite3", dbPath) db, err := sql.Open("sqlite3", dbPath)