-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split ./pkg/auth/webapp_request_middleware.go from ./pkg/lib/deps/mid…
…dleware_request.go
- Loading branch information
1 parent
fe7d464
commit 7ebf318
Showing
8 changed files
with
83 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package auth | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"net/http" | ||
|
||
"github.com/authgear/authgear-server/pkg/auth/handler/webapp/viewmodels" | ||
"github.com/authgear/authgear-server/pkg/lib/config" | ||
"github.com/authgear/authgear-server/pkg/lib/config/configsource" | ||
"github.com/authgear/authgear-server/pkg/lib/deps" | ||
"github.com/authgear/authgear-server/pkg/lib/otelauthgear" | ||
"github.com/authgear/authgear-server/pkg/lib/web" | ||
"github.com/authgear/authgear-server/pkg/util/httputil" | ||
"github.com/authgear/authgear-server/pkg/util/template" | ||
) | ||
|
||
var TemplateWebAppNotFoundHTML = template.RegisterHTML( | ||
"web/app_not_found.html", | ||
web.ComponentsHTML..., | ||
) | ||
|
||
// WebAppRequestMiddleware is placed at /pkg/auth because it depends on /pkg/auth/handler/webapp/viewmodels | ||
// So it CANNOT be replaced at /pkg/auth/webapp. | ||
type WebAppRequestMiddleware struct { | ||
HTTPHost httputil.HTTPHost | ||
RootProvider *deps.RootProvider | ||
ConfigSource *configsource.ConfigSource | ||
TemplateEngine *template.Engine | ||
BaseViewModeler *viewmodels.BaseViewModeler | ||
} | ||
|
||
func (m *WebAppRequestMiddleware) Handle(next http.Handler) http.Handler { | ||
logger := m.RootProvider.LoggerFactory.New("request") | ||
|
||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
logger.WithFields(map[string]interface{}{ | ||
"request.host": r.Host, | ||
"request.url": r.URL.String(), | ||
"request.header.host": r.Header.Get("Host"), | ||
"request.header.x-forwarded-host": r.Header.Get("X-Forwarded-Host"), | ||
"request.header.x-original-host": r.Header.Get("X-Original-Host"), | ||
}).Debug("serving request") | ||
err := m.ConfigSource.ProvideContext(r.Context(), r, func(ctx context.Context, appCtx *config.AppContext) error { | ||
r = r.WithContext(ctx) | ||
|
||
ap := m.RootProvider.NewAppProvider(r.Context(), appCtx) | ||
r = r.WithContext(deps.WithAppProvider(r.Context(), ap)) | ||
|
||
otelauthgear.SetProjectID(r.Context(), string(appCtx.Config.AppConfig.ID)) | ||
next.ServeHTTP(w, r) | ||
return nil | ||
}) | ||
if err != nil { | ||
if errors.Is(err, configsource.ErrAppNotFound) { | ||
data := map[string]interface{}{ | ||
"HTTPHost": string(m.HTTPHost), | ||
} | ||
baseViewModel := m.BaseViewModeler.ViewModel(r, w) | ||
viewmodels.Embed(data, baseViewModel) | ||
m.TemplateEngine.RenderStatus(w, r, http.StatusNotFound, TemplateWebAppNotFoundHTML, data) | ||
} else { | ||
logger.WithError(err).Error("failed to resolve config") | ||
http.Error(w, "internal server error", http.StatusInternalServerError) | ||
} | ||
return | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters