feat: serve static files
continuous-integration/drone Build is passing
Details
continuous-integration/drone Build is passing
Details
This commit is contained in:
parent
f9b4c35f59
commit
a9caca2772
|
@ -8,31 +8,47 @@ import (
|
||||||
//go:embed static/index.html
|
//go:embed static/index.html
|
||||||
var indexPage string
|
var indexPage string
|
||||||
|
|
||||||
func New(matrixHostname, matrixBaseURL string) http.HandlerFunc {
|
type Params struct {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
MatrixHostname string
|
||||||
if r.Method != http.MethodGet && r.Method != http.MethodHead {
|
MatrixBaseURL string
|
||||||
w.WriteHeader(http.StatusMethodNotAllowed)
|
RootPath string
|
||||||
w.Write([]byte("405 method not allowed\n"))
|
}
|
||||||
return
|
|
||||||
|
type handler struct {
|
||||||
|
*http.ServeMux
|
||||||
|
params Params
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(params Params) http.Handler {
|
||||||
|
h := &handler{
|
||||||
|
ServeMux: http.NewServeMux(),
|
||||||
|
params: params,
|
||||||
}
|
}
|
||||||
|
|
||||||
switch r.URL.Path {
|
h.Handle("GET /.well-known/matrix/server", http.HandlerFunc(h.getMatrixServer))
|
||||||
case "/.well-known/matrix/server":
|
h.Handle("GET /.well-known/matrix/client", http.HandlerFunc(h.getMatrixClient))
|
||||||
|
h.Handle("GET /{$}", http.HandlerFunc(h.getHomepage))
|
||||||
|
h.Handle("GET /", http.FileServer(http.Dir(params.RootPath)))
|
||||||
|
|
||||||
|
return h
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *handler) getMatrixServer(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Add("content-type", "application/json")
|
w.Header().Add("content-type", "application/json")
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
w.Write([]byte(`{"m.server": "` + matrixHostname + `"}`))
|
w.Write([]byte(`{"m.server": "` + h.params.MatrixHostname + `"}`))
|
||||||
case "/.well-known/matrix/client":
|
}
|
||||||
|
|
||||||
|
func (h *handler) getMatrixClient(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Add("content-type", "application/json")
|
w.Header().Add("content-type", "application/json")
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
w.Write([]byte(`{"m.homeserver": {"base_url": "` + matrixBaseURL + `"}}`))
|
w.Write([]byte(`{"m.homeserver": {"base_url": "` + h.params.MatrixBaseURL + `"}}`))
|
||||||
case "/":
|
}
|
||||||
|
|
||||||
|
func (h *handler) getHomepage(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Add("content-type", "text/html")
|
w.Header().Add("content-type", "text/html")
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
if r.Method == http.MethodGet {
|
if r.Method == http.MethodGet {
|
||||||
w.Write([]byte(indexPage))
|
w.Write([]byte(indexPage))
|
||||||
}
|
}
|
||||||
default:
|
|
||||||
http.NotFound(w, r)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
package handler_test
|
package handler_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -41,6 +41,14 @@ 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,
|
||||||
|
@ -69,7 +77,7 @@ func TestHandler(t *testing.T) {
|
||||||
method: http.MethodPost,
|
method: http.MethodPost,
|
||||||
path: "/",
|
path: "/",
|
||||||
wantStatusCode: http.StatusMethodNotAllowed,
|
wantStatusCode: http.StatusMethodNotAllowed,
|
||||||
wantBody: "405 method not allowed",
|
wantBody: "Method Not Allowed\n",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -78,7 +86,13 @@ 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(matrixHostname, matrixBaseURL)
|
h := handler.New(
|
||||||
|
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()
|
||||||
|
@ -89,7 +103,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 := ioutil.ReadAll(resp.Body)
|
respBody, err := io.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)
|
||||||
})
|
})
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Test</title>
|
||||||
|
</head>
|
||||||
|
</html>
|
11
main.go
11
main.go
|
@ -18,8 +18,9 @@ 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")
|
||||||
if matrixHostname == "" || matrixBaseURL == "" {
|
rootPath := os.Getenv("NETFLUX_ROOT_PATH")
|
||||||
log.Fatal("NETFLUX_MATRIX_HOSTNAME and NETFLUX_MATRIX_BASE_URL are both required")
|
if matrixHostname == "" || matrixBaseURL == "" || rootPath == "" {
|
||||||
|
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,7 +30,11 @@ func main() {
|
||||||
|
|
||||||
server := http.Server{
|
server := http.Server{
|
||||||
Addr: listenAddr,
|
Addr: listenAddr,
|
||||||
Handler: handler.New(matrixHostname, matrixBaseURL),
|
Handler: handler.New(handler.Params{
|
||||||
|
MatrixHostname: matrixHostname,
|
||||||
|
MatrixBaseURL: matrixBaseURL,
|
||||||
|
RootPath: rootPath,
|
||||||
|
}),
|
||||||
ReadTimeout: readTimeout,
|
ReadTimeout: readTimeout,
|
||||||
WriteTimeout: writeTimeout,
|
WriteTimeout: writeTimeout,
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue