mirror of
https://github.com/matrix-org/dendrite.git
synced 2026-01-19 03:53:09 -06:00
Embed the template, add test
This commit is contained in:
parent
3dd1fed642
commit
37d557202f
|
|
@ -18,8 +18,10 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"embed"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"html/template"
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
@ -65,6 +67,9 @@ import (
|
||||||
userapiinthttp "github.com/matrix-org/dendrite/userapi/inthttp"
|
userapiinthttp "github.com/matrix-org/dendrite/userapi/inthttp"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
//go:embed static/*.gotmpl
|
||||||
|
var staticContent embed.FS
|
||||||
|
|
||||||
// BaseDendrite is a base for creating new instances of dendrite. It parses
|
// BaseDendrite is a base for creating new instances of dendrite. It parses
|
||||||
// command line flags and config, and exposes methods for creating various
|
// command line flags and config, and exposes methods for creating various
|
||||||
// resources. All errors are handled by logging then exiting, so all methods
|
// resources. All errors are handled by logging then exiting, so all methods
|
||||||
|
|
@ -493,8 +498,16 @@ func (b *BaseDendrite) SetupAndServeHTTP(
|
||||||
|
|
||||||
b.ConfigureAdminEndpoints()
|
b.ConfigureAdminEndpoints()
|
||||||
|
|
||||||
|
tmpl := template.Must(template.ParseFS(staticContent, "static/*.gotmpl"))
|
||||||
|
|
||||||
b.PublicStaticMux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
b.PublicStaticMux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
http.ServeFile(w, r, "static/index.html")
|
if err := tmpl.ExecuteTemplate(w, "index.gotmpl", map[string]string{
|
||||||
|
"Version": internal.VersionString(),
|
||||||
|
}); err != nil {
|
||||||
|
logrus.WithError(err).Error("failed to execute landing page template")
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
_, _ = w.Write([]byte("failed to execute template"))
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
var clientHandler http.Handler
|
var clientHandler http.Handler
|
||||||
|
|
|
||||||
57
setup/base/base_test.go
Normal file
57
setup/base/base_test.go
Normal file
|
|
@ -0,0 +1,57 @@
|
||||||
|
package base_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"embed"
|
||||||
|
"html/template"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/matrix-org/dendrite/internal"
|
||||||
|
"github.com/matrix-org/dendrite/setup/config"
|
||||||
|
"github.com/matrix-org/dendrite/test/testrig"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
//go:embed static/*.gotmpl
|
||||||
|
var staticContent embed.FS
|
||||||
|
|
||||||
|
func TestLandingPage(t *testing.T) {
|
||||||
|
// generate the expected result
|
||||||
|
tmpl := template.Must(template.ParseFS(staticContent, "static/*.gotmpl"))
|
||||||
|
expectedRes := &bytes.Buffer{}
|
||||||
|
err := tmpl.ExecuteTemplate(expectedRes, "index.gotmpl", map[string]string{
|
||||||
|
"Version": internal.VersionString(),
|
||||||
|
})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
b, _, _ := testrig.Base(nil)
|
||||||
|
defer b.Close()
|
||||||
|
|
||||||
|
// hack: create a server and close it immediately, just to get a random port assigned
|
||||||
|
s := httptest.NewServer(nil)
|
||||||
|
s.Close()
|
||||||
|
|
||||||
|
// start base with the listener and wait for it to be started
|
||||||
|
go b.SetupAndServeHTTP("", config.HTTPAddress(s.URL), nil, nil)
|
||||||
|
time.Sleep(time.Millisecond * 10)
|
||||||
|
|
||||||
|
// When hitting /, we should be redirected to /_matrix/static, which should contain the landing page
|
||||||
|
req, err := http.NewRequest(http.MethodGet, s.URL, nil)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
// do the request
|
||||||
|
resp, err := s.Client().Do(req)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
||||||
|
|
||||||
|
// read the response
|
||||||
|
buf := &bytes.Buffer{}
|
||||||
|
_, err = buf.ReadFrom(resp.Body)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
// Using .String() for user friendly output
|
||||||
|
assert.Equal(t, expectedRes.String(), buf.String(), "response mismatch")
|
||||||
|
}
|
||||||
|
|
@ -46,7 +46,7 @@
|
||||||
</g>
|
</g>
|
||||||
</svg>
|
</svg>
|
||||||
</div>
|
</div>
|
||||||
<h1>It works! Dendrite is running</h1>
|
<h1>It works! Dendrite {{ .Version }} is running</h1>
|
||||||
<p>Your Dendrite server is listening on this port and is ready for messages.</p>
|
<p>Your Dendrite server is listening on this port and is ready for messages.</p>
|
||||||
<p>To use this server you'll need <a href="https://matrix.org/docs/projects/try-matrix-now.html#clients" target="_blank" rel="noopener noreferrer">a Matrix client</a>.
|
<p>To use this server you'll need <a href="https://matrix.org/docs/projects/try-matrix-now.html#clients" target="_blank" rel="noopener noreferrer">a Matrix client</a>.
|
||||||
</p>
|
</p>
|
||||||
Loading…
Reference in a new issue