Skip to content

Commit

Permalink
fix(server): add schema to published field in web config
Browse files Browse the repository at this point in the history
  • Loading branch information
rot1024 committed Feb 24, 2023
1 parent 18fdf71 commit b17807a
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 2 deletions.
1 change: 1 addition & 0 deletions server/internal/app/public.go
Expand Up @@ -248,6 +248,7 @@ func getAliasFromHost(host, pattern string) string {
}

const placeholder = "<>"
pattern = strings.TrimPrefix(strings.TrimPrefix(pattern, "https://"), "http://")
pattern = strings.ReplaceAll(pattern, "{}", placeholder)
re, err := regexp.Compile(strings.ReplaceAll(regexp.QuoteMeta(pattern), placeholder, "(.+?)"))
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions server/internal/app/public_test.go
Expand Up @@ -241,5 +241,7 @@ func TestGetAliasFromHost(t *testing.T) {
assert.Equal(t, "", getAliasFromHost("", ".example.com")) // invalid regexp
assert.Equal(t, "", getAliasFromHost("", "{}.example.com"))
assert.Equal(t, "aaa", getAliasFromHost("aaa.example.com", "{}.example.com"))
assert.Equal(t, "aaa", getAliasFromHost("aaa.example.com", "https://{}.example.com"))
assert.Equal(t, "aaa", getAliasFromHost("aaa.example.com", "http://{}.example.com"))
assert.Equal(t, "bbb.aaa", getAliasFromHost("bbb.aaa.example.com", "{}.example.com"))
}
9 changes: 8 additions & 1 deletion server/internal/app/web.go
Expand Up @@ -47,7 +47,7 @@ func (w *WebHandler) Handler(e *echo.Echo) {
}
}
if w.HostPattern != "" {
config["published"] = w.HostPattern
config["published"] = w.hostWithSchema()
}

for k, v := range w.WebConfig {
Expand All @@ -73,3 +73,10 @@ func (w *WebHandler) Handler(e *echo.Echo) {
e.GET("/", notFound, PublishedIndexMiddleware(w.HostPattern, false, w.AppDisabled), m)
e.GET("*", notFound, m)
}

func (w *WebHandler) hostWithSchema() string {
if strings.HasPrefix(w.HostPattern, "http://") || strings.HasPrefix(w.HostPattern, "https://") {
return w.HostPattern
}
return "https://" + w.HostPattern
}
2 changes: 1 addition & 1 deletion server/internal/app/web_test.go
Expand Up @@ -66,7 +66,7 @@ func TestWeb(t *testing.T) {
name: "reearth_config.json",
path: "/reearth_config.json",
statusCode: http.StatusOK,
body: `{"a":"b","auth0Audience":"https://aud.example.com","auth0ClientId":"clientID","auth0Domain":"https://iss.example.com"}`,
body: `{"a":"b","auth0Audience":"https://aud.example.com","auth0ClientId":"clientID","auth0Domain":"https://iss.example.com","published":"https://{}.example.com"}`,
contentType: "application/json; charset=UTF-8",
},
{
Expand Down

0 comments on commit b17807a

Please sign in to comment.