Skip to content

Commit

Permalink
feat(server): add config to disable web feature
Browse files Browse the repository at this point in the history
  • Loading branch information
rot1024 committed Feb 14, 2023
1 parent 8455319 commit 6547586
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 65 deletions.
13 changes: 12 additions & 1 deletion server/internal/app/app.go
Expand Up @@ -110,7 +110,14 @@ func initEcho(ctx context.Context, cfg *ServerConfig) *echo.Echo {
published.GET("/:name/", PublishedIndex("", true))

serveFiles(e, cfg.Gateways.File)
web(e, cfg.Config.Web, cfg.Config.AuthForWeb(), cfg.Config.Published.Host, nil)
(&WebHandler{
Disabled: cfg.Config.Web_Disabled,
AppDisabled: cfg.Config.Web_App_Disabled,
WebConfig: cfg.Config.Web,
AuthConfig: cfg.Config.AuthForWeb(),
HostPattern: cfg.Config.Published.Host,
FS: nil,
}).Handler(e)

return e
}
Expand All @@ -121,6 +128,10 @@ func errorHandler(next func(error, echo.Context)) func(error, echo.Context) {
return
}

if errors.Is(err, echo.ErrNotFound) {
err = rerror.ErrNotFound
}

code, msg := errorMessage(err, func(f string, args ...interface{}) {
c.Echo().Logger.Errorf(f, args...)
})
Expand Down
48 changes: 25 additions & 23 deletions server/internal/app/config.go
Expand Up @@ -20,29 +20,31 @@ import (
const configPrefix = "reearth"

type Config struct {
Port string `default:"8080" envconfig:"PORT"`
ServerHost string
Host string `default:"http://localhost:8080"`
Host_Web string
Dev bool
DB string `default:"mongodb://localhost"`
Mailer string
SMTP SMTPConfig
SendGrid SendGridConfig
GraphQL GraphQLConfig
Published PublishedConfig
GCPProject string `envconfig:"GOOGLE_CLOUD_PROJECT"`
Profiler string
Tracer string
TracerSample float64
GCS GCSConfig
Marketplace MarketplaceConfig
AssetBaseURL string `default:"http://localhost:8080/assets"`
Origins []string
Policy PolicyConfig
Web WebConfig
SignupSecret string
SignupDisabled bool
Port string `default:"8080" envconfig:"PORT"`
ServerHost string
Host string `default:"http://localhost:8080"`
Host_Web string
Dev bool
DB string `default:"mongodb://localhost"`
Mailer string
SMTP SMTPConfig
SendGrid SendGridConfig
GraphQL GraphQLConfig
Published PublishedConfig
GCPProject string `envconfig:"GOOGLE_CLOUD_PROJECT"`
Profiler string
Tracer string
TracerSample float64
GCS GCSConfig
Marketplace MarketplaceConfig
AssetBaseURL string `default:"http://localhost:8080/assets"`
Origins []string
Policy PolicyConfig
Web_Disabled bool
Web_App_Disabled bool
Web WebConfig
SignupSecret string
SignupDisabled bool
// auth
Auth AuthConfigs
Auth0 Auth0Config
Expand Down
13 changes: 9 additions & 4 deletions server/internal/app/public.go
Expand Up @@ -154,10 +154,10 @@ func PublishedData(pattern string, useParam bool) echo.HandlerFunc {
}

func PublishedIndex(pattern string, useParam bool) echo.HandlerFunc {
return PublishedIndexMiddleware(pattern, useParam)(nil)
return PublishedIndexMiddleware(pattern, useParam, true)(nil)
}

func PublishedIndexMiddleware(pattern string, useParam bool) echo.MiddlewareFunc {
func PublishedIndexMiddleware(pattern string, useParam, errorIfNotFound bool) echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
contr, err := publishedController(c)
Expand All @@ -166,8 +166,13 @@ func PublishedIndexMiddleware(pattern string, useParam bool) echo.MiddlewareFunc
}

alias := resolveAlias(c, pattern, useParam)
if alias == "" && next != nil {
return next(c)
if alias == "" && (next != nil || errorIfNotFound) {
if errorIfNotFound {
return rerror.ErrNotFound
}
if next != nil {
return next(c)
}
}

index, err := contr.Index(c.Request().Context(), alias, &url.URL{
Expand Down
43 changes: 28 additions & 15 deletions server/internal/app/web.go
Expand Up @@ -11,30 +11,43 @@ import (

type WebConfig map[string]string

func web(e *echo.Echo, wc WebConfig, ac *AuthConfig, pattern string, fs afero.Fs) {
if fs == nil {
fs = afero.NewOsFs()
type WebHandler struct {
Disabled bool
AppDisabled bool
WebConfig WebConfig
AuthConfig *AuthConfig
HostPattern string
FS afero.Fs
}

func (w *WebHandler) Handler(e *echo.Echo) {
if w.Disabled {
return
}

if w.FS == nil {
w.FS = afero.NewOsFs()
}
if _, err := fs.Stat("web"); err != nil {
if _, err := w.FS.Stat("web"); err != nil {
return // web won't be delivered
}

e.Logger.Info("web: web directory will be delivered\n")

config := map[string]string{}
if ac != nil {
if ac.ISS != "" {
config["auth0Domain"] = strings.TrimSuffix(ac.ISS, "/")
if w.AuthConfig != nil {
if w.AuthConfig.ISS != "" {
config["auth0Domain"] = strings.TrimSuffix(w.AuthConfig.ISS, "/")
}
if ac.ClientID != nil {
config["auth0ClientId"] = *ac.ClientID
if w.AuthConfig.ClientID != nil {
config["auth0ClientId"] = *w.AuthConfig.ClientID
}
if len(ac.AUD) > 0 {
config["auth0Audience"] = ac.AUD[0]
if len(w.AuthConfig.AUD) > 0 {
config["auth0Audience"] = w.AuthConfig.AUD[0]
}
}

for k, v := range wc {
for k, v := range w.WebConfig {
config[k] = v
}

Expand All @@ -43,17 +56,17 @@ func web(e *echo.Echo, wc WebConfig, ac *AuthConfig, pattern string, fs afero.Fs
Index: "index.html",
Browse: false,
HTML5: true,
Filesystem: afero.NewHttpFs(fs),
Filesystem: afero.NewHttpFs(w.FS),
})
notFound := func(c echo.Context) error { return echo.ErrNotFound }

e.GET("/reearth_config.json", func(c echo.Context) error {
return c.JSON(http.StatusOK, config)
})
e.GET("/data.json", PublishedData(pattern, false))
e.GET("/data.json", PublishedData(w.HostPattern, false))
e.GET("/index.html", func(c echo.Context) error {
return c.Redirect(http.StatusPermanentRedirect, "/")
})
e.GET("/", notFound, PublishedIndexMiddleware(pattern, false), m)
e.GET("/", notFound, PublishedIndexMiddleware(w.HostPattern, false, w.AppDisabled), m)
e.GET("*", notFound, m)
}
68 changes: 46 additions & 22 deletions server/internal/app/web_test.go
Expand Up @@ -43,38 +43,25 @@ func TestWeb(t *testing.T) {
lo.Must0(prjr.Save(ctx, prj))
fileg := lo.Must(fs.NewFile(mfs, ""))
lo.Must0(fileg.UploadBuiltScene(ctx, strings.NewReader(dataJSON), prj.Alias()))
e := echo.New()
e.HTTPErrorHandler = func(err error, c echo.Context) {
if errors.Is(err, rerror.ErrNotFound) {
_ = c.JSON(http.StatusNotFound, map[string]any{"error": "not found"})
return
}
_ = c.JSON(http.StatusInternalServerError, err.Error())
}

e.Use(ContextMiddleware(func(ctx context.Context) context.Context {
return adapter.AttachUsecases(ctx, &interfaces.Container{
Published: interactor.NewPublished(prjr, fileg, publishedHTML),
})
}))

web(e, WebConfig{
"a": "b",
}, &AuthConfig{
ISS: "https://iss.example.com",
AUD: []string{"https://aud.example.com"},
ClientID: lo.ToPtr("clientID"),
}, `{}.example.com`, mfs)

tests := []struct {
name string
path string
host string
disabled bool
appDisabled bool
statusCode int
body string
contentType string
assertBody func(t *testing.T, body string)
}{
{
name: "disabled",
disabled: true,
path: "/reearth_config.json",
statusCode: http.StatusNotFound,
body: `{"error":"not found"}`,
},
{
name: "reearth_config.json",
path: "/reearth_config.json",
Expand Down Expand Up @@ -125,6 +112,12 @@ func TestWeb(t *testing.T) {
body: indexHTML,
contentType: "text/html; charset=utf-8",
},
{
name: "index file with app disabled",
appDisabled: true,
path: "/",
statusCode: http.StatusNotFound,
},
{
name: "index file with app host",
path: "/",
Expand Down Expand Up @@ -158,6 +151,37 @@ func TestWeb(t *testing.T) {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

e := echo.New()
e.HTTPErrorHandler = func(err error, c echo.Context) {
if errors.Is(err, rerror.ErrNotFound) || errors.Is(err, echo.ErrNotFound) {
_ = c.JSON(http.StatusNotFound, map[string]any{"error": "not found"})
return
}
_ = c.JSON(http.StatusInternalServerError, err.Error())
}

e.Use(ContextMiddleware(func(ctx context.Context) context.Context {
return adapter.AttachUsecases(ctx, &interfaces.Container{
Published: interactor.NewPublished(prjr, fileg, publishedHTML),
})
}))

(&WebHandler{
Disabled: tt.disabled,
AppDisabled: tt.appDisabled,
WebConfig: WebConfig{
"a": "b",
},
AuthConfig: &AuthConfig{
ISS: "https://iss.example.com",
AUD: []string{"https://aud.example.com"},
ClientID: lo.ToPtr("clientID"),
},
HostPattern: `{}.example.com`,
FS: mfs,
}).Handler(e)

r := httptest.NewRequest("GET", tt.path, nil)
if tt.host != "" {
r.Host = tt.host
Expand Down

0 comments on commit 6547586

Please sign in to comment.