Skip to content

Commit

Permalink
added config-validation mode
Browse files Browse the repository at this point in the history
  • Loading branch information
superstes committed Sep 25, 2023
1 parent 5213feb commit 26bb5ac
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 6 deletions.
6 changes: 3 additions & 3 deletions lib/cnf/cnf_file/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ func readConfig() (config []byte) {
return config
}
}
config, err = readConfigFile(cnf.CONFIG_FILE_ABS)
config, err = readConfigFile(cnf.ConfigFileAbs)
if err == nil && config != nil {
return config
}
log.ErrorS("config", fmt.Sprintf(
"Neither config file could be read: (%s, %s)", cwdConfig, cnf.CONFIG_FILE_ABS,
"Neither config file could be read: (%s, %s)", cwdConfig, cnf.ConfigFileAbs,
))
panic(fmt.Errorf("no valid config file found! (%s, %s)", cwdConfig, cnf.CONFIG_FILE_ABS))
panic(fmt.Errorf("no valid config file found! (%s, %s)", cwdConfig, cnf.ConfigFileAbs))
}

func Load() {
Expand Down
3 changes: 2 additions & 1 deletion lib/cnf/hardcoded.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ const (
LOG_TIME_FORMAT string = "2006-01-02 15:04:05"
UDP_TTL = 30 * time.Second
UDP_BUFFER_SIZE int = 4096
CONFIG_FILE_ABS string = "/etc/calamary/config.yml"
)

var ConfigFileAbs string = "/etc/calamary/config.yml"
16 changes: 16 additions & 0 deletions lib/main/main.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package main

import (
"flag"
"fmt"
"os"

"github.com/superstes/calamary/cnf"
"github.com/superstes/calamary/cnf/cnf_file"
"github.com/superstes/calamary/log"
)

func welcome() {
Expand All @@ -18,7 +21,20 @@ func welcome() {
}

func main() {
var modeValidate bool
flag.BoolVar(&modeValidate, "v", false, "Only validate config")
flag.StringVar(&cnf.ConfigFileAbs, "f", cnf.ConfigFileAbs, "Path to the config file")
flag.Parse()

cnf.C = &cnf.Config{}

if modeValidate {
cnf.C.Service.Debug = true
cnf_file.Load()
log.Info("config", "Config validated successfully")
os.Exit(0)
}

welcome()
cnf_file.Load()
service := &service{}
Expand Down
4 changes: 2 additions & 2 deletions scripts/buildrun.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
set -e

cd "$(dirname "$0")/../lib/main"
go build
./main
go build -o calamary
./calamary
30 changes: 30 additions & 0 deletions systemd.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[Unit]
Description=Calamary Forward Proxy
Documentation=https://docs.calamary.net
Documentation=https://github.com/superstes/calamary
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
Environment=CONFIG=/etc/calamary/config.yml

# validate before start/restart
ExecStartPre=/usr/bin/calamary -f $CONFIG -v
ExecStart=/usr/bin/calamary -f $CONFIG

# validate before reload
ExecReload=/usr/bin/calamary -f $CONFIG -v
ExecReload=/bin/kill -HUP $MAINPID

User=proxy
Group=proxy
Restart=on-failure
RestartSec=5s

StandardOutput=journal
StandardError=journal
SyslogIdentifier=calamary

[Install]
WantedBy=multi-user.target

0 comments on commit 26bb5ac

Please sign in to comment.