Skip to content

Commit

Permalink
[+] add web UI start options, closes #236
Browse files Browse the repository at this point in the history
  • Loading branch information
pashagolub committed Jul 13, 2023
1 parent a1d3475 commit 2cc4312
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 4 deletions.
8 changes: 8 additions & 0 deletions src/config/cmdparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,18 @@ type StartOpts struct {
// Debug bool `long:"debug" description:"Run in debug mode. Only asynchronous chains will be executed"`
}

// WebUIOpts specifies the internal web UI server options
type WebUIOpts struct {
WebAddr string `long:"web-addr" mapstructure:"web-addr" description:"TCP address in the form 'host:port' to listen on" default:":8080" env:"PW3_WEBADDR"`
WebUser string `long:"web-user" mapstructure:"web-user" description:"Admin login" env:"PW3_WEBUSER"`
WebPassword string `long:"web-password" mapstructure:"web-password" description:"Admin password" env:"PW3_WEBPASSWORD"`
}

type CmdOptions struct {
Connection ConnectionOpts `group:"Connection" mapstructure:"Connection"`
Metric MetricOpts `group:"Metric" mapstructure:"Metric"`
Logging LoggingOpts `group:"Logging" mapstructure:"Logging"`
WebUI WebUIOpts `group:"WebUI" mapstructure:"WebUI"`
Start StartOpts `group:"Start" mapstructure:"Start"`
// Params for running based on local config files, enabled distributed "push model" based metrics gathering. Metrics are sent directly to Influx/Graphite.
Config string `short:"c" long:"config" mapstructure:"config" description:"File or folder of YAML files containing info on which DBs to monitor and where to store metrics" env:"PW3_CONFIG"`
Expand Down
2 changes: 1 addition & 1 deletion src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2521,7 +2521,7 @@ func main() {
mainContext = log.WithLogger(mainContext, logger)

uifs, _ := fs.Sub(webuifs, "webui/build")
ui := webserver.Init(":8080", uifs, uiapi, logger)
ui := webserver.Init(opts.WebUI, uifs, uiapi, logger)
if ui == nil {
exitCode.Store(ExitCodeWebUIError)
return
Expand Down
7 changes: 5 additions & 2 deletions src/webserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,30 @@ import (
"strings"
"time"

"github.com/cybertec-postgresql/pgwatch3/config"
"github.com/cybertec-postgresql/pgwatch3/log"
)

type WebUIServer struct {
l log.LoggerIface
http.Server
config.WebUIOpts
uiFS fs.FS
api apiHandler
}

func Init(addr string, webuifs fs.FS, api apiHandler, logger log.LoggerIface) *WebUIServer {
func Init(opts config.WebUIOpts, webuifs fs.FS, api apiHandler, logger log.LoggerIface) *WebUIServer {
mux := http.NewServeMux()
s := &WebUIServer{
logger,
http.Server{
Addr: addr,
Addr: opts.WebAddr,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
Handler: mux,
},
opts,
webuifs,
api,
}
Expand Down
3 changes: 2 additions & 1 deletion src/webserver/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import (
"os"
"testing"

"github.com/cybertec-postgresql/pgwatch3/config"
"github.com/cybertec-postgresql/pgwatch3/log"
"github.com/cybertec-postgresql/pgwatch3/webserver"
"github.com/stretchr/testify/assert"
)

func TestStatus(t *testing.T) {
restsrv := webserver.Init("127.0.0.1:8080", os.DirFS("../webui/build"), nil, log.FallbackLogger)
restsrv := webserver.Init(config.WebUIOpts{WebAddr: "127.0.0.1:8080"}, os.DirFS("../webui/build"), nil, log.FallbackLogger)
assert.NotNil(t, restsrv)

r, err := http.Get("http://localhost:8080/")
Expand Down

0 comments on commit 2cc4312

Please sign in to comment.