Tweak setup

This commit is contained in:
Neil Alexander 2022-08-01 11:03:05 +01:00
parent 6e9d22bfa2
commit 2f100c7993
No known key found for this signature in database
GPG key ID: A02A2019A2BB0944

View file

@ -381,6 +381,21 @@ func (b *BaseDendrite) SetupAndServeHTTP(
externalRouter := mux.NewRouter().SkipClean(true).UseEncodedPath() externalRouter := mux.NewRouter().SkipClean(true).UseEncodedPath()
internalRouter := externalRouter internalRouter := externalRouter
notFoundHandler := func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
_, _ = w.Write([]byte("endpoint not found"))
}
notAllowedHandler := func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusMethodNotAllowed)
_, _ = w.Write([]byte(fmt.Sprintf("%s not allowed on this endpoint", r.Method)))
}
notFoundCORSHandler := httputil.WrapHandlerInCORS(http.HandlerFunc(notFoundHandler))
notAllowedCORSHandler := httputil.WrapHandlerInCORS(http.HandlerFunc(notAllowedHandler))
externalRouter.NotFoundHandler = notFoundCORSHandler
externalRouter.MethodNotAllowedHandler = notAllowedCORSHandler
externalServ := &http.Server{ externalServ := &http.Server{
Addr: string(externalAddr), Addr: string(externalAddr),
WriteTimeout: HTTPServerTimeout, WriteTimeout: HTTPServerTimeout,
@ -400,6 +415,8 @@ func (b *BaseDendrite) SetupAndServeHTTP(
// without enabling TLS. // without enabling TLS.
internalH2S := &http2.Server{} internalH2S := &http2.Server{}
internalRouter = mux.NewRouter().SkipClean(true).UseEncodedPath() internalRouter = mux.NewRouter().SkipClean(true).UseEncodedPath()
internalRouter.NotFoundHandler = notFoundCORSHandler
internalRouter.MethodNotAllowedHandler = notAllowedCORSHandler
internalServ = &http.Server{ internalServ = &http.Server{
Addr: string(internalAddr), Addr: string(internalAddr),
Handler: h2c.NewHandler(internalRouter, internalH2S), Handler: h2c.NewHandler(internalRouter, internalH2S),
@ -451,24 +468,6 @@ func (b *BaseDendrite) SetupAndServeHTTP(
externalRouter.PathPrefix(httputil.PublicMediaPathPrefix).Handler(b.PublicMediaAPIMux) externalRouter.PathPrefix(httputil.PublicMediaPathPrefix).Handler(b.PublicMediaAPIMux)
externalRouter.PathPrefix(httputil.PublicWellKnownPrefix).Handler(b.PublicWellKnownAPIMux) externalRouter.PathPrefix(httputil.PublicWellKnownPrefix).Handler(b.PublicWellKnownAPIMux)
notFoundHandler := func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
_, _ = w.Write([]byte(fmt.Sprintf("not found: %q", r.RequestURI)))
}
notAllowedHandler := func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusMethodNotAllowed)
_, _ = w.Write([]byte(fmt.Sprintf("%s not allowed: %q", r.Method, r.RequestURI)))
}
notFoundCORSHandler := httputil.WrapHandlerInCORS(http.HandlerFunc(notFoundHandler))
notAllowedCORSHandler := httputil.WrapHandlerInCORS(http.HandlerFunc(notAllowedHandler))
internalRouter.NotFoundHandler = notFoundCORSHandler
internalRouter.MethodNotAllowedHandler = notAllowedCORSHandler
externalRouter.NotFoundHandler = notFoundCORSHandler
externalRouter.MethodNotAllowedHandler = notAllowedCORSHandler
if internalAddr != NoListener && internalAddr != externalAddr { if internalAddr != NoListener && internalAddr != externalAddr {
go func() { go func() {
var internalShutdown atomic.Bool // RegisterOnShutdown can be called more than once var internalShutdown atomic.Bool // RegisterOnShutdown can be called more than once