From df5fcf36cda5141bafd1307eaedbe8ebb382a3a6 Mon Sep 17 00:00:00 2001 From: Pavlo Golub Date: Wed, 15 Jun 2022 18:21:46 +0200 Subject: [PATCH 1/2] [+] add built-in http server, closes #28 --- .vscode/tasks.json | 7 +++++-- src/go.mod | 4 ++++ src/go.sum | 2 -- src/main.go | 5 +++++ src/webui/status.go | 41 ++++++++++++++++++++++++++++++++++++++++ src/webui/status_test.go | 25 ++++++++++++++++++++++++ 6 files changed, 80 insertions(+), 4 deletions(-) create mode 100644 src/webui/status.go create mode 100644 src/webui/status_test.go diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 7d73836ae9..65aaaccb56 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -6,7 +6,10 @@ { "label": "Unit Test", "type": "shell", - "command": "gotest -failfast -timeout=300s -parallel=1 ./${relativeFileDirname}/... -coverprofile='coverage.out' && go tool cover -func='coverage.out'", + "options": { + "cwd": "${workspaceFolder}/src/" + }, + "command": "gotest -failfast -timeout=300s -parallel=1 ..${pathSeparator}${relativeFileDirname}${pathSeparator}... -coverprofile='coverage.out' && go tool cover -func='coverage.out'", "problemMatcher": [ "$go" ], @@ -23,7 +26,7 @@ { "label": "Coverage Report", "type": "shell", - "command": "go tool cover -html='coverage.out'", + "command": "cd src && go tool cover -html='coverage.out'", "problemMatcher": [ "$go" ], diff --git a/src/go.mod b/src/go.mod index e368e96ce1..021baa29ee 100644 --- a/src/go.mod +++ b/src/go.mod @@ -14,6 +14,7 @@ require ( github.com/samuel/go-zookeeper v0.0.0-20201211165307-7117e9ea2414 github.com/shirou/gopsutil/v3 v3.22.4 github.com/shopspring/decimal v1.3.1 + github.com/stretchr/testify v1.7.1 go.etcd.io/etcd v3.3.27+incompatible go.etcd.io/etcd/client/v2 v2.305.4 golang.org/x/crypto v0.0.0-20220517005047-85d78b3ac167 @@ -27,6 +28,7 @@ require ( github.com/coreos/etcd v3.3.27+incompatible // indirect github.com/coreos/go-semver v0.3.0 // indirect github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f // indirect + github.com/davecgh/go-spew v1.1.1 // indirect github.com/fatih/color v1.13.0 // indirect github.com/go-ole/go-ole v1.2.6 // indirect github.com/golang/protobuf v1.5.2 // indirect @@ -45,6 +47,7 @@ require ( github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/power-devops/perfstat v0.0.0-20220216144756-c35f1ee13d7c // indirect github.com/prometheus/client_model v0.2.0 // indirect github.com/prometheus/common v0.34.0 // indirect @@ -56,4 +59,5 @@ require ( go.etcd.io/etcd/client/pkg/v3 v3.5.4 // indirect golang.org/x/sys v0.0.0-20220513210249-45d2b4557a2a // indirect google.golang.org/protobuf v1.28.0 // indirect + gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect ) diff --git a/src/go.sum b/src/go.sum index f381a21917..4df9216303 100644 --- a/src/go.sum +++ b/src/go.sum @@ -204,8 +204,6 @@ github.com/hashicorp/serf v0.9.6/go.mod h1:TXZNMjZQijwlDvp+r0b63xZ45H7JmCmgg4gpT github.com/hashicorp/serf v0.9.8 h1:JGklO/2Drf1QGa312EieQN3zhxQ+aJg6pG+aC3MFaVo= github.com/hashicorp/serf v0.9.8/go.mod h1:TXZNMjZQijwlDvp+r0b63xZ45H7JmCmgg4gpTwn9UV4= github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= -github.com/influxdata/influxdb1-client v0.0.0-20220302092344-a9ab5670611c h1:qSHzRbhzK8RdXOsAdfDgO49TtqC1oZ+acxPrkfTxcCs= -github.com/influxdata/influxdb1-client v0.0.0-20220302092344-a9ab5670611c/go.mod h1:qj24IKcXYK6Iy9ceXlo3Tc+vtHo9lIhSX5JddghvEPo= github.com/jessevdk/go-flags v1.5.0 h1:1jKYvbxEjfUl0fmqTCOfonvskHHXMjBySTLW4y9LFvc= github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4= github.com/jmoiron/sqlx v1.3.5 h1:vFFPA71p1o5gAeqtEAwLU4dnX2napprKtHr7PYIcN3g= diff --git a/src/main.go b/src/main.go index 8bf4489a14..98eb94c6b4 100644 --- a/src/main.go +++ b/src/main.go @@ -42,6 +42,8 @@ import ( "github.com/shopspring/decimal" "golang.org/x/crypto/pbkdf2" "gopkg.in/yaml.v2" + + "github.com/cybertec-postgresql/pgwatch3/webui" ) var commit = "" // Git hash. Will be set on build time by build_gatherer.sh / goreleaser @@ -4909,6 +4911,9 @@ type Options struct { var opts Options func main() { + + _ = webui.Init(":8080") + var err error parser := flags.NewParser(&opts, flags.Default) diff --git a/src/webui/status.go b/src/webui/status.go new file mode 100644 index 0000000000..fea30c9b47 --- /dev/null +++ b/src/webui/status.go @@ -0,0 +1,41 @@ +package webui + +import ( + "net/http" + "time" +) + +// StatusReporter is a common interface describing the current status of a connection +type StatusReporter interface { + IsReady() bool +} + +type WebUIServer struct { + // l log.Logger + http.Server +} + +func Init(addr string) *WebUIServer { + s := &WebUIServer{ + // nil, + // logger, + http.Server{ + Addr: addr, + ReadTimeout: 10 * time.Second, + WriteTimeout: 10 * time.Second, + MaxHeaderBytes: 1 << 20, + }, + } + http.HandleFunc("/", s.rootHandler) + if 8080 != 0 { + // logger.WithField("port", opts.Port).Info("Starting REST API server...") + go func() { panic(s.ListenAndServe()) }() + } + return s +} + +func (Server *WebUIServer) rootHandler(w http.ResponseWriter, r *http.Request) { + // Server.l.Debug("Received / web request") + w.WriteHeader(http.StatusOK) + w.Write([]byte("Hello world!")) +} diff --git a/src/webui/status_test.go b/src/webui/status_test.go new file mode 100644 index 0000000000..48cc4113d0 --- /dev/null +++ b/src/webui/status_test.go @@ -0,0 +1,25 @@ +package webui_test + +import ( + "io" + "net/http" + "testing" + + "github.com/cybertec-postgresql/pgwatch3/webui" + "github.com/stretchr/testify/assert" +) + +func TestStatus(t *testing.T) { + restsrv := webui.Init("127.0.0.1:8080") + assert.NotNil(t, restsrv) + + _, err := http.Get("http://localhost:80/") + assert.Error(t, err) + + r, err := http.Get("http://localhost:8080/") + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, r.StatusCode) + b, err := io.ReadAll(r.Body) + assert.NoError(t, err) + assert.Equal(t, "Hello world!", string(b)) +} From 37ecbe2c22fcd0b651cf6c99b32fa04a865aceae Mon Sep 17 00:00:00 2001 From: Pavlo Golub Date: Wed, 15 Jun 2022 18:27:54 +0200 Subject: [PATCH 2/2] remove extra test code --- src/webui/status_test.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/webui/status_test.go b/src/webui/status_test.go index 48cc4113d0..fa5fd62fb4 100644 --- a/src/webui/status_test.go +++ b/src/webui/status_test.go @@ -13,9 +13,6 @@ func TestStatus(t *testing.T) { restsrv := webui.Init("127.0.0.1:8080") assert.NotNil(t, restsrv) - _, err := http.Get("http://localhost:80/") - assert.Error(t, err) - r, err := http.Get("http://localhost:8080/") assert.NoError(t, err) assert.Equal(t, http.StatusOK, r.StatusCode)