Compare commits
No commits in common. "bd9f9883bd4856f0e03b7a2efaf00c143ebd5b84" and "1c825e06068e5d1cc6a55c3e1dc3ac0dcac96ebb" have entirely different histories.
bd9f9883bd
...
1c825e0606
8
go.mod
8
go.mod
@ -1,11 +1,11 @@
|
|||||||
module git.netflux.io/rob/netflux-homepage
|
module git.netflux.io/rob/netflux-homepage
|
||||||
|
|
||||||
go 1.23.3
|
go 1.18
|
||||||
|
|
||||||
require github.com/stretchr/testify v1.9.0
|
require github.com/stretchr/testify v1.7.1
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
github.com/davecgh/go-spew v1.1.0 // indirect
|
||||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
|
||||||
)
|
)
|
||||||
|
6
go.sum
6
go.sum
@ -1,17 +1,11 @@
|
|||||||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
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/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
|
||||||
github.com/davecgh/go-spew v1.1.1/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 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
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 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
|
||||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
|
||||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
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 h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
||||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
|
||||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
|
||||||
|
@ -8,47 +8,31 @@ import (
|
|||||||
//go:embed static/index.html
|
//go:embed static/index.html
|
||||||
var indexPage string
|
var indexPage string
|
||||||
|
|
||||||
type Params struct {
|
func New(matrixHostname, matrixBaseURL string) http.HandlerFunc {
|
||||||
MatrixHostname string
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
MatrixBaseURL string
|
if r.Method != http.MethodGet && r.Method != http.MethodHead {
|
||||||
RootPath string
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||||
}
|
w.Write([]byte("405 method not allowed\n"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
type handler struct {
|
switch r.URL.Path {
|
||||||
*http.ServeMux
|
case "/.well-known/matrix/server":
|
||||||
params Params
|
w.Header().Add("content-type", "application/json")
|
||||||
}
|
w.WriteHeader(http.StatusOK)
|
||||||
|
w.Write([]byte(`{"m.server": "` + matrixHostname + `"}`))
|
||||||
func New(params Params) http.Handler {
|
case "/.well-known/matrix/client":
|
||||||
h := &handler{
|
w.Header().Add("content-type", "application/json")
|
||||||
ServeMux: http.NewServeMux(),
|
w.WriteHeader(http.StatusOK)
|
||||||
params: params,
|
w.Write([]byte(`{"m.homeserver": {"base_url": "` + matrixBaseURL + `"}}`))
|
||||||
}
|
case "/":
|
||||||
|
w.Header().Add("content-type", "text/html")
|
||||||
h.Handle("GET /.well-known/matrix/server", http.HandlerFunc(h.getMatrixServer))
|
w.WriteHeader(http.StatusOK)
|
||||||
h.Handle("GET /.well-known/matrix/client", http.HandlerFunc(h.getMatrixClient))
|
if r.Method == http.MethodGet {
|
||||||
h.Handle("GET /{$}", http.HandlerFunc(h.getHomepage))
|
w.Write([]byte(indexPage))
|
||||||
h.Handle("GET /", http.FileServer(http.Dir(params.RootPath)))
|
}
|
||||||
|
default:
|
||||||
return h
|
http.NotFound(w, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *handler) getMatrixServer(w http.ResponseWriter, r *http.Request) {
|
|
||||||
w.Header().Add("content-type", "application/json")
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
w.Write([]byte(`{"m.server": "` + h.params.MatrixHostname + `"}`))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *handler) getMatrixClient(w http.ResponseWriter, r *http.Request) {
|
|
||||||
w.Header().Add("content-type", "application/json")
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
w.Write([]byte(`{"m.homeserver": {"base_url": "` + h.params.MatrixBaseURL + `"}}`))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *handler) getHomepage(w http.ResponseWriter, r *http.Request) {
|
|
||||||
w.Header().Add("content-type", "text/html")
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
if r.Method == http.MethodGet {
|
|
||||||
w.Write([]byte(indexPage))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package handler_test
|
package handler_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"testing"
|
"testing"
|
||||||
@ -41,14 +41,6 @@ func TestHandler(t *testing.T) {
|
|||||||
wantStatusCode: http.StatusOK,
|
wantStatusCode: http.StatusOK,
|
||||||
wantBody: `{"m.homeserver": {"base_url": "https://foo.example.com"}}`,
|
wantBody: `{"m.homeserver": {"base_url": "https://foo.example.com"}}`,
|
||||||
},
|
},
|
||||||
{
|
|
||||||
name: "GET /test.html",
|
|
||||||
method: http.MethodGet,
|
|
||||||
path: "/test.html",
|
|
||||||
wantContentType: "text/html; charset=utf-8",
|
|
||||||
wantStatusCode: http.StatusOK,
|
|
||||||
wantBody: "<html>\n <head>\n <title>Test</title>\n </head>\n</html>\n",
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
name: "GET /",
|
name: "GET /",
|
||||||
method: http.MethodGet,
|
method: http.MethodGet,
|
||||||
@ -77,7 +69,7 @@ func TestHandler(t *testing.T) {
|
|||||||
method: http.MethodPost,
|
method: http.MethodPost,
|
||||||
path: "/",
|
path: "/",
|
||||||
wantStatusCode: http.StatusMethodNotAllowed,
|
wantStatusCode: http.StatusMethodNotAllowed,
|
||||||
wantBody: "Method Not Allowed\n",
|
wantBody: "405 method not allowed",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -86,13 +78,7 @@ func TestHandler(t *testing.T) {
|
|||||||
req := httptest.NewRequest(tc.method, tc.path, nil)
|
req := httptest.NewRequest(tc.method, tc.path, nil)
|
||||||
rec := httptest.NewRecorder()
|
rec := httptest.NewRecorder()
|
||||||
|
|
||||||
h := handler.New(
|
h := handler.New(matrixHostname, matrixBaseURL)
|
||||||
handler.Params{
|
|
||||||
MatrixHostname: matrixHostname,
|
|
||||||
MatrixBaseURL: matrixBaseURL,
|
|
||||||
RootPath: "testdata/static/",
|
|
||||||
},
|
|
||||||
)
|
|
||||||
h.ServeHTTP(rec, req)
|
h.ServeHTTP(rec, req)
|
||||||
resp := rec.Result()
|
resp := rec.Result()
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
@ -103,7 +89,7 @@ func TestHandler(t *testing.T) {
|
|||||||
assert.Equal(t, tc.wantContentType, resp.Header.Get("content-type"))
|
assert.Equal(t, tc.wantContentType, resp.Header.Get("content-type"))
|
||||||
}
|
}
|
||||||
|
|
||||||
respBody, err := io.ReadAll(resp.Body)
|
respBody, err := ioutil.ReadAll(resp.Body)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Contains(t, string(respBody), tc.wantBody)
|
assert.Contains(t, string(respBody), tc.wantBody)
|
||||||
})
|
})
|
||||||
|
5
handler/testdata/static/test.html
vendored
5
handler/testdata/static/test.html
vendored
@ -1,5 +0,0 @@
|
|||||||
<html>
|
|
||||||
<head>
|
|
||||||
<title>Test</title>
|
|
||||||
</head>
|
|
||||||
</html>
|
|
13
main.go
13
main.go
@ -18,9 +18,8 @@ const (
|
|||||||
func main() {
|
func main() {
|
||||||
matrixHostname := os.Getenv("NETFLUX_MATRIX_HOSTNAME")
|
matrixHostname := os.Getenv("NETFLUX_MATRIX_HOSTNAME")
|
||||||
matrixBaseURL := os.Getenv("NETFLUX_MATRIX_BASE_URL")
|
matrixBaseURL := os.Getenv("NETFLUX_MATRIX_BASE_URL")
|
||||||
rootPath := os.Getenv("NETFLUX_ROOT_PATH")
|
if matrixHostname == "" || matrixBaseURL == "" {
|
||||||
if matrixHostname == "" || matrixBaseURL == "" || rootPath == "" {
|
log.Fatal("NETFLUX_MATRIX_HOSTNAME and NETFLUX_MATRIX_BASE_URL are both required")
|
||||||
log.Fatal("NETFLUX_MATRIX_HOSTNAME and NETFLUX_MATRIX_BASE_URL and NETFLUX_ROOT_PATH are all required")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
listenAddr := os.Getenv("NETFLUX_LISTEN_ADDR")
|
listenAddr := os.Getenv("NETFLUX_LISTEN_ADDR")
|
||||||
@ -29,12 +28,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
server := http.Server{
|
server := http.Server{
|
||||||
Addr: listenAddr,
|
Addr: listenAddr,
|
||||||
Handler: handler.New(handler.Params{
|
Handler: handler.New(matrixHostname, matrixBaseURL),
|
||||||
MatrixHostname: matrixHostname,
|
|
||||||
MatrixBaseURL: matrixBaseURL,
|
|
||||||
RootPath: rootPath,
|
|
||||||
}),
|
|
||||||
ReadTimeout: readTimeout,
|
ReadTimeout: readTimeout,
|
||||||
WriteTimeout: writeTimeout,
|
WriteTimeout: writeTimeout,
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user