Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[+] add built-in http server, closes #28 #30

Merged
merged 2 commits into from
Jun 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
],
Expand All @@ -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"
],
Expand Down
4 changes: 4 additions & 0 deletions src/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
)
2 changes: 0 additions & 2 deletions src/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
5 changes: 5 additions & 0 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -4909,6 +4911,9 @@ type Options struct {
var opts Options

func main() {

_ = webui.Init(":8080")

var err error
parser := flags.NewParser(&opts, flags.Default)

Expand Down
41 changes: 41 additions & 0 deletions src/webui/status.go
Original file line number Diff line number Diff line change
@@ -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!"))
}
22 changes: 22 additions & 0 deletions src/webui/status_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
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)

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))
}