Skip to content

Commit

Permalink
rework: option parser, WebUI
Browse files Browse the repository at this point in the history
  • Loading branch information
paepckehh committed Oct 27, 2024
1 parent 415c7d9 commit e268b8b
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 55 deletions.
18 changes: 15 additions & 3 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

// global exported consts
const SemVer = "v0.1.21"
const SemVer = "v0.1.24"

// global var
var (
Expand Down Expand Up @@ -94,13 +94,25 @@ func Start(config *OPNCall) error {

// spin up Log/Display Engine
display.Add(1)

// spin up internal log / display engine
go startLog(config)

// spin up internal webserver
go startWeb(config)
state := "[DISABLED]"
if config.Httpd.Enable {
go startWeb(config)
state = "[ENABLED]"
}
displayChan <- []byte("[SERVICE][HTTPD]" + state)

// spin up internal rsyslog server
go startRSysLog(config)
state = "[DISABLED]"
if config.RSysLog.Enable {
go startRSysLog(config)
state = "[ENABLED]"
}
displayChan <- []byte("[SERVICE][RSYSLOG]" + state)

// setup hive
servers := strings.Split(config.Targets, ",")
Expand Down
2 changes: 1 addition & 1 deletion cmd/opnborg/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func main() {

// Startup
t0 := time.Now()
fmt.Println(_app + "[STARTUP][API-VERSION]" + opnborg.SemVer)
fmt.Println(_app + "[STARTUP][API-VERSION:" + opnborg.SemVer + "]")

// Configure
config, err := opnborg.Setup()
Expand Down
4 changes: 2 additions & 2 deletions example-env-config.sh → example-env-config-complex.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/bin/sh
# export OPN_TARGETS='opn01.lan:8443,opn02.lan:8443,opn03.lan:8443,opn04.lan:8443,opn05.lan:8443'
export OPN_TARGETS_HOTSTANDBY='opn00.lan:8443'
export OPN_TARGETS_IMGURL_HOTSTANDBY='https://avatars.githubusercontent.com/u/120342602?s=96&v=4'
export OPN_TARGETS_PRODUCTION='opn01.lan:8443,opn02.lan:8443'
export OPN_TARGETS_IMGURL_HOTSTANDBY='https://icon-library.com/images/freebsd-icon/freebsd-icon-16.jpg'
export OPN_TARGETS_IMGURL_PRODUCTION='https://icon-library.com/images/freebsd-icon/freebsd-icon-16.jpg'
export OPN_MASTER='opn01.lan:8443'
export OPN_APIKEY='+RIb6YWNdcDWMMM7W5ZYDkUvP4qx6e1r7e/Lg/Uh3aBH+veuWfKc7UvEELH/lajWtNxkOaOPjWR8uMcD'
export OPN_APISECRET='8VbjM3HKKqQW2ozOe5PTicMXOBVi9jZTSPCGfGrHp8rW6m+TeTxHyZyAI1GjERbuzjmz6jK/usMCWR/p'
Expand Down
5 changes: 5 additions & 0 deletions example-env-config-simple.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh
export OPN_PATH='/tmp/opn'
export OPN_TARGETS='opn01.lan:8443,opn02.lan:8443,opn03.lan:8443,opn04.lan:8443,opn05.lan:8443'
export OPN_APIKEY='+RIb6YWNdcDWMMM7W5ZYDkUvP4qx6e1r7e/Lg/Uh3aBH+veuWfKc7UvEELH/lajWtNxkOaOPjWR8uMcD'
export OPN_APISECRET='8VbjM3HKKqQW2ozOe5PTicMXOBVi9jZTSPCGfGrHp8rW6m+TeTxHyZyAI1GjERbuzjmz6jK/usMCWR/p'
20 changes: 15 additions & 5 deletions example.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
#!/bin/sh
# include ('source') configuration from example-env-config.sh
. ./example-env-config.sh

# local repository mode
go mod tidy
# include simple ('source') configuration from example-env-config-simple.sh
# . ./example-env-config-simple.sh

# include complex ('source') configuration from example-env-config-complex.sh
. ./example-env-config-complex.sh

# run via local installed binary
# opnborg

# run via local repository mode
# go mod tidy
go run cmd/opnborg/main.go

# if not in local repository, use:
# run via latest commit online
# go run paepcke.de/opnborg/cmd/opnborg@main

# run via latest release online
# go run paepcke.de/opnborg/cmd/opnborg@latest
4 changes: 2 additions & 2 deletions httpd-handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ func headHTML(r http.ResponseWriter) http.ResponseWriter {
// addSecurityHeader ...
func addSecurityHeader(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Cross-Origin-Embedder-Policy", "require-corp")
w.Header().Set("Cross-Origin-Opener-Policy", "same-origin")
// w.Header().Set("Cross-Origin-Embedder-Policy", "require-corp")
// w.Header().Set("Cross-Origin-Opener-Policy", "same-origin")
next.ServeHTTP(w, req)
})
}
Expand Down
8 changes: 1 addition & 7 deletions httpd-srv.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ import (
// httpd spinup the http internal web server
func startWeb(config *OPNCall) {

// terminate if not in daemon mode
if !config.Daemon {
return
}
// create store structure
if err := os.MkdirAll(config.Path, 0770); err != nil {
fmt.Println(err)
Expand Down Expand Up @@ -47,9 +43,7 @@ func startWeb(config *OPNCall) {
}

// info
if config.Debug {
displayChan <- []byte("[HTTPD-SRV][SPIN-UP-SERVER] " + config.Httpd.Server)
}
displayChan <- []byte("[HTTPD-SRV][SPIN-UP-SERVER] " + config.Httpd.Server)

// serve requestes, print err after httpd crash
fmt.Println(httpsrv.Serve(listener))
Expand Down
6 changes: 0 additions & 6 deletions rsyslog-srv.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@ import (
// httpd spinup the internal rsyslog server
func startRSysLog(config *OPNCall) {

// terminate if not in daemon mode
if !config.Daemon || !config.RSysLog.Enable {
displayChan <- []byte("[RSYSLOG][TERMINATED)")
return
}

// create store structure
logStore := filepath.Join(config.Path, "Logs")
if err := os.MkdirAll(logStore, 0770); err != nil {
Expand Down
57 changes: 28 additions & 29 deletions setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,40 +67,39 @@ func Setup() (*OPNCall, error) {
config.Httpd.Enable = true
if config.Daemon {
if !isEnv("OPN_HTTPD_DISABLE") {
config.Httpd.Enable = true
config.Httpd.Server = "127.0.0.1:6464"
if isEnv("OPN_HTTPD_SERVER") {
config.Httpd.Enable = true
config.Httpd.Server = os.Getenv("OPN_HTTPD_SERVER")
if config.Httpd.Server == "" {
config.Httpd.Server = "127.0.0.1:6464"
}
if len(strings.Split(config.Httpd.Server, ":")) < 1 {
return nil, fmt.Errorf("env variable 'OPN_HTTPD_SRV' format error, example \"127.0.0.1:6464\"")
}
config.Httpd.CAcert = os.Getenv("OPN_HTTPD_CACERT")
config.Httpd.CAkey = os.Getenv("OPN_HTTPD_CAKEY")
config.Httpd.CAClient = os.Getenv("OPN_HTTPD_CACLIENT")
config.Httpd.Color.FG = "white"
config.Httpd.Color.BG = "grey"
if _, ok := os.LookupEnv("OPN_HTTPD_COLOR_FG"); ok {
config.Httpd.Color.FG = os.Getenv("OPN_HTTPD_COLOR_FG")
}
if _, ok := os.LookupEnv("OPN_HTTPD_COLOR_BG"); ok {
config.Httpd.Color.BG = os.Getenv("OPN_HTTPD_COLOR_BG")
}

var s strings.Builder
s.WriteString("<head>" + _lf + "<title>" + _app + "</title>" + _lf)
s.WriteString("<meta http-equiv=\"refresh\" content=\"20\">" + _lf)
s.WriteString("<meta charset=\"UTF-8\">" + _lf)
s.WriteString("<link rel=\"icon\" type=\"image/png\" href=\"favicon.ico\">" + _lf)
s.WriteString(" <style>" + _lf)
s.WriteString(" table,th,td{" + _lf)
s.WriteString(" border: 1px solid " + config.Httpd.Color.FG + "; border-collapse: collapse; padding: 8px;}" + _lf)
s.WriteString(" body{color: " + config.Httpd.Color.FG + ";background-color: " + config.Httpd.Color.BG + ";}" + _lf)
s.WriteString(" </style>" + _lf)
s.WriteString("</head>" + _lf)
_head = s.String()
}
config.Httpd.CAcert = os.Getenv("OPN_HTTPD_CACERT")
config.Httpd.CAkey = os.Getenv("OPN_HTTPD_CAKEY")
config.Httpd.CAClient = os.Getenv("OPN_HTTPD_CACLIENT")
config.Httpd.Color.FG = "white"
config.Httpd.Color.BG = "grey"
if isEnv("OPN_HTTPD_COLOR_FG") {
config.Httpd.Color.FG = os.Getenv("OPN_HTTPD_COLOR_FG")
}
if isEnv("OPN_HTTPD_COLOR_BG") {
config.Httpd.Color.BG = os.Getenv("OPN_HTTPD_COLOR_BG")
}

var s strings.Builder
s.WriteString("<head>" + _lf + "<title>" + _app + "</title>" + _lf)
s.WriteString("<meta http-equiv=\"refresh\" content=\"20\">" + _lf)
s.WriteString("<meta charset=\"UTF-8\">" + _lf)
s.WriteString("<link rel=\"icon\" type=\"image/png\" href=\"favicon.ico\">" + _lf)
s.WriteString(" <style>" + _lf)
s.WriteString(" table,th,td{" + _lf)
s.WriteString(" border: 1px solid " + config.Httpd.Color.FG + "; border-collapse: collapse; padding: 8px;}" + _lf)
s.WriteString(" body{color: " + config.Httpd.Color.FG + ";background-color: " + config.Httpd.Color.BG + ";}" + _lf)
s.WriteString(" </style>" + _lf)
s.WriteString("</head>" + _lf)
_head = s.String()

}
}
// config Master
Expand Down Expand Up @@ -216,6 +215,6 @@ func checkSetRequired() error {
}
return fmt.Errorf("add at least one target server to env var 'OPN_TARGETS' or 'OPN_TARGETS_* '(multi valued, comma seperated)")
}
tg = append(tg, OPNGroup{Name: "Hive", Member: strings.Split(os.Getenv("OPN_TARGETS"), ",")})
tg = append(tg, OPNGroup{Name: "", Member: strings.Split(os.Getenv("OPN_TARGETS"), ",")})
return nil
}

0 comments on commit e268b8b

Please sign in to comment.