From b0fe2c6c9af9a243b287b76371f2359af9942cba Mon Sep 17 00:00:00 2001 From: Jaz Volpert Date: Fri, 4 Oct 2024 13:40:45 -0700 Subject: [PATCH 1/4] Serve static assets from a CDN host if provided --- bskyweb/cmd/bskyweb/server.go | 49 +++++++++++++++++++++-------------- bskyweb/templates/base.html | 12 ++++----- scripts/post-web-build.js | 4 +-- 3 files changed, 37 insertions(+), 28 deletions(-) diff --git a/bskyweb/cmd/bskyweb/server.go b/bskyweb/cmd/bskyweb/server.go index ea102802e7..5932809b78 100644 --- a/bskyweb/cmd/bskyweb/server.go +++ b/bskyweb/cmd/bskyweb/server.go @@ -41,12 +41,13 @@ type Server struct { } type Config struct { - debug bool - httpAddress string - appviewHost string - ogcardHost string - linkHost string - ipccHost string + debug bool + httpAddress string + appviewHost string + ogcardHost string + linkHost string + ipccHost string + staticCDNHost string } func serve(cctx *cli.Context) error { @@ -58,6 +59,7 @@ func serve(cctx *cli.Context) error { ipccHost := cctx.String("ipcc-host") basicAuthPassword := cctx.String("basic-auth-password") corsOrigins := cctx.StringSlice("cors-allowed-origins") + staticCDNHost := cctx.String("static-cdn-host") // Echo e := echo.New() @@ -94,12 +96,13 @@ func serve(cctx *cli.Context) error { echo: e, xrpcc: xrpcc, cfg: &Config{ - debug: debug, - httpAddress: httpAddress, - appviewHost: appviewHost, - ogcardHost: ogcardHost, - linkHost: linkHost, - ipccHost: ipccHost, + debug: debug, + httpAddress: httpAddress, + appviewHost: appviewHost, + ogcardHost: ogcardHost, + linkHost: linkHost, + ipccHost: ipccHost, + staticCDNHost: staticCDNHost, }, } @@ -333,15 +336,21 @@ func (srv *Server) Shutdown() error { return srv.httpd.Shutdown(ctx) } +// NewTemplateContext returns a new pongo2 context with some default values. +func (srv *Server) NewTemplateContext() pongo2.Context { + return pongo2.Context{ + "staticCDNHost": srv.cfg.staticCDNHost, + } +} + func (srv *Server) errorHandler(err error, c echo.Context) { code := http.StatusInternalServerError if he, ok := err.(*echo.HTTPError); ok { code = he.Code } c.Logger().Error(err) - data := pongo2.Context{ - "statusCode": code, - } + data := srv.NewTemplateContext() + data["statusCode"] = code c.Render(code, "error.html", data) } @@ -385,18 +394,18 @@ func (srv *Server) LinkProxyMiddleware(url *url.URL) echo.MiddlewareFunc { // handler for endpoint that have no specific server-side handling func (srv *Server) WebGeneric(c echo.Context) error { - data := pongo2.Context{} + data := srv.NewTemplateContext() return c.Render(http.StatusOK, "base.html", data) } func (srv *Server) WebHome(c echo.Context) error { - data := pongo2.Context{} + data := srv.NewTemplateContext() return c.Render(http.StatusOK, "home.html", data) } func (srv *Server) WebPost(c echo.Context) error { ctx := c.Request().Context() - data := pongo2.Context{} + data := srv.NewTemplateContext() // sanity check arguments. don't 4xx, just let app handle if not expected format rkeyParam := c.Param("rkey") @@ -471,7 +480,7 @@ func (srv *Server) WebPost(c echo.Context) error { func (srv *Server) WebStarterPack(c echo.Context) error { req := c.Request() ctx := req.Context() - data := pongo2.Context{} + data := srv.NewTemplateContext() data["requestURI"] = fmt.Sprintf("https://%s%s", req.Host, req.URL.Path) // sanity check arguments. don't 4xx, just let app handle if not expected format rkeyParam := c.Param("rkey") @@ -509,7 +518,7 @@ func (srv *Server) WebStarterPack(c echo.Context) error { func (srv *Server) WebProfile(c echo.Context) error { ctx := c.Request().Context() - data := pongo2.Context{} + data := srv.NewTemplateContext() // sanity check arguments. don't 4xx, just let app handle if not expected format handleOrDIDParam := c.Param("handleOrDID") diff --git a/bskyweb/templates/base.html b/bskyweb/templates/base.html index 885d13d2a7..5f0f1ca78a 100644 --- a/bskyweb/templates/base.html +++ b/bskyweb/templates/base.html @@ -13,8 +13,8 @@ - - + + {% include "scripts.html" %} - - - - + + + + diff --git a/scripts/post-web-build.js b/scripts/post-web-build.js index 7bbee38554..5619b251b7 100644 --- a/scripts/post-web-build.js +++ b/scripts/post-web-build.js @@ -23,10 +23,10 @@ const outputFile = entrypoints const ext = path.extname(file) if (ext === '.js') { - return `` + return `` } if (ext === '.css') { - return `` + return `` } return '' From eb5a6c891159a449d97764806fbe9b66ddb32281 Mon Sep 17 00:00:00 2001 From: Jaz Volpert Date: Fri, 4 Oct 2024 13:43:15 -0700 Subject: [PATCH 2/4] Propagate env var and trim trailing slash --- bskyweb/cmd/bskyweb/main.go | 7 +++++++ bskyweb/cmd/bskyweb/server.go | 1 + 2 files changed, 8 insertions(+) diff --git a/bskyweb/cmd/bskyweb/main.go b/bskyweb/cmd/bskyweb/main.go index 985879f4a9..5c46af418d 100644 --- a/bskyweb/cmd/bskyweb/main.go +++ b/bskyweb/cmd/bskyweb/main.go @@ -87,6 +87,13 @@ func run(args []string) { Value: cli.NewStringSlice("https://bsky.app", "https://main.bsky.dev", "https://app.staging.bsky.dev"), EnvVars: []string{"CORS_ALLOWED_ORIGINS"}, }, + &cli.StringFlag{ + Name: "static-cdn-host", + Usage: "scheme, hostname, and port of static content CDN, don't end with a slash", + Required: false, + Value: "", + EnvVars: []string{"STATIC_CDN_HOST"}, + }, }, }, } diff --git a/bskyweb/cmd/bskyweb/server.go b/bskyweb/cmd/bskyweb/server.go index 5932809b78..1089e3c1af 100644 --- a/bskyweb/cmd/bskyweb/server.go +++ b/bskyweb/cmd/bskyweb/server.go @@ -60,6 +60,7 @@ func serve(cctx *cli.Context) error { basicAuthPassword := cctx.String("basic-auth-password") corsOrigins := cctx.StringSlice("cors-allowed-origins") staticCDNHost := cctx.String("static-cdn-host") + staticCDNHost = strings.TrimSuffix(staticCDNHost, "/") // Echo e := echo.New() From 0d99b8b0550084ef55f294b33ef5e12608fd5035 Mon Sep 17 00:00:00 2001 From: Jaz Volpert Date: Fri, 4 Oct 2024 13:49:59 -0700 Subject: [PATCH 3/4] Consistent spacing for variable handlebars --- scripts/post-web-build.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/post-web-build.js b/scripts/post-web-build.js index 5619b251b7..3b1b62dfd8 100644 --- a/scripts/post-web-build.js +++ b/scripts/post-web-build.js @@ -23,10 +23,10 @@ const outputFile = entrypoints const ext = path.extname(file) if (ext === '.js') { - return `` + return `` } if (ext === '.css') { - return `` + return `` } return '' From 795e2c0629ad5d2d659b2dd6658cdd3f6ce071e0 Mon Sep 17 00:00:00 2001 From: Jaz Volpert Date: Fri, 4 Oct 2024 14:31:37 -0700 Subject: [PATCH 4/4] New assets to static CDN --- bskyweb/templates/base.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bskyweb/templates/base.html b/bskyweb/templates/base.html index c27262426a..9a50fae69e 100644 --- a/bskyweb/templates/base.html +++ b/bskyweb/templates/base.html @@ -26,14 +26,14 @@ */ @font-face { font-family: 'InterVariable'; - src: url(/static/media/InterVariable.c9f788f6e7ebaec75d7c.ttf) format('truetype'); + src: url("{{ staticCDNHost }}/static/media/InterVariable.c9f788f6e7ebaec75d7c.ttf") format('truetype'); font-weight: 300 1000; font-style: normal; font-display: swap; } @font-face { font-family: 'InterVariableItalic'; - src: url(/static/media/InterVariable-Italic.55d6a3f35e9b605ba6f4.ttf) format('truetype'); + src: url("{{ staticCDNHost }}/static/media/InterVariable-Italic.55d6a3f35e9b605ba6f4.ttf") format('truetype'); font-weight: 300 1000; font-style: italic; font-display: swap;