diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..5cb2428 --- /dev/null +++ b/.drone.yml @@ -0,0 +1,14 @@ +--- +kind: pipeline +type: kubernetes +name: default + +steps: +- name: backend + image: golang:1.18 + commands: + - go install honnef.co/go/tools/cmd/staticcheck@latest + - go build ./... + - go vet ./... + - staticcheck ./... + - go test -bench=. -benchmem -cover ./... diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..2b3e903 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,16 @@ +FROM golang:1.18-alpine3.15 as go-builder +ENV GOPATH "" + +RUN apk add git + +WORKDIR /app +ADD go.mod go.sum ./ +RUN go mod download +ADD ./ . +RUN go build -o ./server . + +FROM alpine:3.14 + +COPY --from=go-builder /app/server /app/server + +ENTRYPOINT ["/app/server"] diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..f7b1f29 --- /dev/null +++ b/go.mod @@ -0,0 +1,11 @@ +module git.netflux.io/rob/netflux-homepage + +go 1.18 + +require ( + github.com/davecgh/go-spew v1.1.0 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/stretchr/objx v0.1.0 // indirect + github.com/stretchr/testify v1.7.1 // indirect + gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..9f0da9a --- /dev/null +++ b/go.sum @@ -0,0 +1,11 @@ +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/handler/handler.go b/handler/handler.go new file mode 100644 index 0000000..ebea200 --- /dev/null +++ b/handler/handler.go @@ -0,0 +1,33 @@ +package handler + +import ( + _ "embed" + "net/http" +) + +//go:embed static/index.html +var indexPage string + +func New(matrixHostname, matrixBaseURL string) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodGet { + http.NotFound(w, r) + return + } + + switch r.URL.Path { + case "/.well-known/matrix/server": + w.Header().Add("content-type", "application/json") + w.WriteHeader(http.StatusOK) + w.Write([]byte(`{"m.server": "` + matrixHostname + `"}`)) + case "/.well-known/matrix/client": + w.Header().Add("content-type", "application/json") + w.WriteHeader(http.StatusOK) + w.Write([]byte(`{"m.homeserver": {"base_url": "` + matrixBaseURL + `"}}`)) + default: + w.Header().Add("content-type", "text/html") + w.WriteHeader(http.StatusOK) + w.Write([]byte(indexPage)) + } + } +} diff --git a/handler/handler_test.go b/handler/handler_test.go new file mode 100644 index 0000000..5e0aa5f --- /dev/null +++ b/handler/handler_test.go @@ -0,0 +1,74 @@ +package handler_test + +import ( + "io/ioutil" + "net/http" + "net/http/httptest" + "testing" + + "git.netflux.io/rob/netflux-homepage/handler" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestHandler(t *testing.T) { + const ( + matrixHostname = "foo.example.com:443" + matrixBaseURL = "https://foo.example.com" + ) + + testCases := []struct { + name string + method string + path string + wantContentType string + wantStatusCode int + wantBody string + }{ + { + name: "GET /.well-known/matrix/server", + method: http.MethodGet, + path: "/.well-known/matrix/server", + wantContentType: "application/json", + wantStatusCode: http.StatusOK, + wantBody: `{"m.server": "foo.example.com:443"}`, + }, + { + name: "GET /.well-known/matrix/client", + method: http.MethodGet, + path: "/.well-known/matrix/client", + wantContentType: "application/json", + wantStatusCode: http.StatusOK, + wantBody: `{"m.homeserver": {"base_url": "https://foo.example.com"}}`, + }, + { + name: "GET /", + method: http.MethodGet, + path: "/", + wantContentType: "text/html", + wantStatusCode: http.StatusOK, + wantBody: "Welcome to netflux.io", + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + req := httptest.NewRequest(tc.method, tc.path, nil) + rec := httptest.NewRecorder() + + h := handler.New(matrixHostname, matrixBaseURL) + h.ServeHTTP(rec, req) + resp := rec.Result() + defer resp.Body.Close() + + assert.Equal(t, tc.wantContentType, resp.Header.Get("content-type")) + assert.Equal(t, tc.wantStatusCode, resp.StatusCode) + + if tc.wantBody != "" { + respBody, err := ioutil.ReadAll(resp.Body) + require.NoError(t, err) + assert.Contains(t, string(respBody), tc.wantBody) + } + }) + } +} diff --git a/handler/static/index.html b/handler/static/index.html new file mode 100644 index 0000000..31f3ac8 --- /dev/null +++ b/handler/static/index.html @@ -0,0 +1,29 @@ + + + + + + netflux | home + + + + +
+

netflux.io

+ +

+ Welcome to netflux.io. This is a domain maintained by Rob Watson, mostly + for the purposes of personal projects and various self-hosted web + services. +

+ +

+ This page will probably be updated in due course. In the meantime, you + can browse my Git projects, or + contact me via email: mail [at] this domain. +

+ + © Rob Watson, 2022. +
+ + diff --git a/main.go b/main.go new file mode 100644 index 0000000..22fddfd --- /dev/null +++ b/main.go @@ -0,0 +1,40 @@ +package main + +import ( + "log" + "net/http" + "os" + "time" + + "git.netflux.io/rob/netflux-homepage/handler" +) + +const ( + readTimeout = time.Second * 3 + writeTimeout = time.Second * 3 + defaultListenAddr = ":9000" +) + +func main() { + matrixHostname := os.Getenv("NETFLUX_MATRIX_HOSTNAME") + matrixBaseURL := os.Getenv("NETFLUX_MATRIX_BASE_URL") + if matrixHostname == "" || matrixBaseURL == "" { + log.Fatal("NETFLUX_MATRIX_HOSTNAME and NETFLUX_MATRIX_BASE_URL are both required") + } + + listenAddr := os.Getenv("NETFLUX_LISTEN_ADDR") + if listenAddr == "" { + listenAddr = defaultListenAddr + } + + server := http.Server{ + Addr: listenAddr, + Handler: handler.New(matrixHostname, matrixBaseURL), + ReadTimeout: readTimeout, + WriteTimeout: writeTimeout, + } + + if err := server.ListenAndServe(); err != nil { + log.Fatalf("server error: %v", err) + } +}