Skip to content

Commit 7f69390

Browse files
authored
feat(server): serve web contents (#656)
1 parent 18f5253 commit 7f69390

File tree

6 files changed

+119
-39
lines changed

6 files changed

+119
-39
lines changed

.vscode/settings.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,5 @@
44
"go.lintFlags": [
55
"--config=${workspaceFolder}/.golangci.yml"
66
],
7-
"gopls": { "experimentalWorkspaceModule": true}
8-
7+
"go.useLanguageServer": true
98
}

server/e2e/publicapi_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ func TestPublicAPI(t *testing.T) {
9797
},
9898
},
9999
"totalCount": 3,
100+
"hasMore": false,
101+
"limit": 50,
102+
"offset": 0,
103+
"page": 1,
100104
})
101105

102106
// offset pagination
@@ -114,6 +118,10 @@ func TestPublicAPI(t *testing.T) {
114118
},
115119
},
116120
"totalCount": 3,
121+
"hasMore": false,
122+
"limit": 1,
123+
"offset": 1,
124+
"page": 2,
117125
})
118126

119127
// cursor pagination
@@ -207,6 +215,10 @@ func TestPublicAPI(t *testing.T) {
207215
},
208216
},
209217
"totalCount": 3,
218+
"hasMore": false,
219+
"limit": 50,
220+
"offset": 0,
221+
"page": 1,
210222
})
211223

212224
e.GET("/api/p/{project}/{model}/{item}", publicAPIProjectAlias, publicAPIModelKey, publicAPIItem1ID).

server/internal/app/app.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func initEcho(ctx context.Context, cfg *ServerConfig) *echo.Echo {
9393
), integration.NewStrictHandler(integration.NewServer(), nil))
9494

9595
serveFiles(e, cfg.Gateways.File)
96-
webConfig(e, nil, cfg.Config.Auths())
96+
Web(e, cfg.Config.Web, cfg.Config.AuthForWeb(), cfg.Config.Web_Disabled, nil)
9797
return e
9898
}
9999

server/internal/app/config.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ type Config struct {
3232
GCS GCSConfig
3333
Task gcp.TaskConfig
3434
AssetBaseURL string
35+
Web WebConfig
36+
Web_Disabled bool
3537
// auth
3638
Auth AuthConfigs
3739
Auth0 Auth0Config
@@ -118,6 +120,29 @@ func (c Config) JWTProviders() (res []appx.JWTProvider) {
118120
return c.Auths().JWTProviders()
119121
}
120122

123+
func (c Config) AuthForWeb() *AuthConfig {
124+
if ac := c.Auth0.AuthConfigForWeb(); ac != nil {
125+
return ac
126+
}
127+
if c.Auth_ISS != "" {
128+
var aud []string
129+
if len(c.Auth_AUD) > 0 {
130+
aud = append(aud, c.Auth_AUD)
131+
}
132+
return &AuthConfig{
133+
ISS: c.Auth_ISS,
134+
AUD: aud,
135+
ALG: c.Auth_ALG,
136+
TTL: c.Auth_TTL,
137+
ClientID: c.Auth_ClientID,
138+
}
139+
}
140+
// if ac := c.AuthSrv.AuthConfig(c.Dev, c.Host); ac != nil {
141+
// return ac
142+
// }
143+
return nil
144+
}
145+
121146
func (c Auth0Config) AuthConfig() *AuthConfig {
122147
domain := c.Domain
123148
if c.Domain == "" {
@@ -139,6 +164,22 @@ func (c Auth0Config) AuthConfig() *AuthConfig {
139164
}
140165
}
141166

167+
func (c Auth0Config) AuthConfigForWeb() *AuthConfig {
168+
if c.Domain == "" || c.WebClientID == "" {
169+
return nil
170+
}
171+
domain := prepareUrl(c.Domain)
172+
var aud []string
173+
if len(c.Audience) > 0 {
174+
aud = []string{c.Audience}
175+
}
176+
return &AuthConfig{
177+
ISS: domain,
178+
AUD: aud,
179+
ClientID: &c.WebClientID,
180+
}
181+
}
182+
142183
func (a AuthConfig) JWTProvider() appx.JWTProvider {
143184
return appx.JWTProvider{
144185
ISS: a.ISS,
@@ -214,3 +255,11 @@ func (c Config) Print() string {
214255
}
215256
return s
216257
}
258+
259+
func prepareUrl(url string) string {
260+
if !strings.HasPrefix(url, "https://") && !strings.HasPrefix(url, "http://") {
261+
url = "https://" + url
262+
}
263+
url = strings.TrimSuffix(url, "/")
264+
return url
265+
}

server/internal/app/web.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package app
2+
3+
import (
4+
"net/http"
5+
"strings"
6+
7+
"github.com/labstack/echo/v4"
8+
"github.com/labstack/echo/v4/middleware"
9+
"github.com/spf13/afero"
10+
)
11+
12+
type WebConfig map[string]string
13+
14+
func Web(e *echo.Echo, wc WebConfig, ac *AuthConfig, disabled bool, fs afero.Fs) {
15+
if disabled {
16+
return
17+
}
18+
19+
if fs == nil {
20+
fs = afero.NewOsFs()
21+
}
22+
if _, err := fs.Stat("web"); err != nil {
23+
return // web won't be delivered
24+
}
25+
26+
e.Logger.Info("web: web directory will be delivered\n")
27+
28+
config := map[string]string{}
29+
if ac != nil {
30+
if ac.ISS != "" {
31+
config["auth0Domain"] = strings.TrimSuffix(ac.ISS, "/")
32+
}
33+
if ac.ClientID != nil {
34+
config["auth0ClientId"] = *ac.ClientID
35+
}
36+
if len(ac.AUD) > 0 {
37+
config["auth0Audience"] = ac.AUD[0]
38+
}
39+
}
40+
41+
for k, v := range wc {
42+
config[k] = v
43+
}
44+
45+
e.GET("/reearth_config.json", func(c echo.Context) error {
46+
return c.JSON(http.StatusOK, config)
47+
})
48+
49+
e.Use(middleware.StaticWithConfig(middleware.StaticConfig{
50+
Root: "web",
51+
Index: "index.html",
52+
Browse: false,
53+
HTML5: true,
54+
Filesystem: afero.NewHttpFs(fs),
55+
}))
56+
}

server/internal/app/web_config.go

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)