Initial iteration
continuous-integration/drone Build is passing Details

This commit is contained in:
Rob Watson 2022-06-06 18:48:53 +02:00
parent 5580884b42
commit d0fd4d1461
8 changed files with 228 additions and 0 deletions

14
.drone.yml Normal file
View File

@ -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 ./...

16
Dockerfile Normal file
View File

@ -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"]

11
go.mod Normal file
View File

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

11
go.sum Normal file
View File

@ -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=

33
handler/handler.go Normal file
View File

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

74
handler/handler_test.go Normal file
View File

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

29
handler/static/index.html Normal file
View File

@ -0,0 +1,29 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>netflux | home</title>
<meta name="description" content="Welcome to the netflux.io homepage">
<meta name="author" content="Rob Watson">
</head>
<body>
<center style="max-width: 800px; margin: 0 auto">
<h1>netflux.io</h1>
<p>
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.
</p>
<p>
This page will probably be updated in due course. In the meantime, you
can <a href="https://git.netflux.io/rob/">browse my Git projects</a>, or
contact me via email: mail [at] this domain.
</p>
<small>&copy; Rob Watson, 2022.</small>
</center>
</body>
</html>

40
main.go Normal file
View File

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