From a763e7caec652aee6127bd07d8e532f80253d568 Mon Sep 17 00:00:00 2001 From: Alexander Bezzubov Date: Tue, 27 Oct 2020 20:02:55 +0100 Subject: [PATCH 1/3] server: serve FS content of ./frontend/dist/* from /static path Signed-off-by: Alexander Bezzubov --- cmd/server/server.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/cmd/server/server.go b/cmd/server/server.go index d454989..2878baf 100644 --- a/cmd/server/server.go +++ b/cmd/server/server.go @@ -12,7 +12,9 @@ import ( "log" "net/http" "os" + "path/filepath" "sort" + "strings" "github.com/bzz/scholar-alert-digest/gmailutils" "github.com/bzz/scholar-alert-digest/gmailutils/token" @@ -104,6 +106,11 @@ func main() { r.Get("/login", handleLogin) r.Get("/login/authorized", handleAuth) + // static + workDir, _ := os.Getwd() + filesDir := http.Dir(filepath.Join(workDir, "frontend", "dist")) + FileServer(r, "/static", filesDir) + r.Route("/json", func(j chi.Router) { j.Use(setContentType("application/json")) if *dev { @@ -281,6 +288,27 @@ func tokenAndLabelCookiesCtx(next http.Handler) http.Handler { }) } +// FileServer conveniently sets up a http.FileServer handler to serve +// static files from a http.FileSystem. +func FileServer(r chi.Router, path string, root http.FileSystem) { + if strings.ContainsAny(path, "{}*") { + panic("FileServer does not permit any URL parameters.") + } + + if path != "/" && path[len(path)-1] != '/' { + r.Get(path, http.RedirectHandler(path+"/", 301).ServeHTTP) + path += "/" + } + path += "*" + + r.Get(path, func(w http.ResponseWriter, r *http.Request) { + rctx := chi.RouteContext(r.Context()) + pathPrefix := strings.TrimSuffix(rctx.RoutePattern(), "/*") + fs := http.StripPrefix(pathPrefix, http.FileServer(root)) + fs.ServeHTTP(w, r) + }) +} + //// JSON func setContentType(contentType string) func(next http.Handler) http.Handler { From 95d274cfc29c4f0185ce7b775d35876986245636 Mon Sep 17 00:00:00 2001 From: Alexander Bezzubov Date: Tue, 27 Oct 2020 21:24:38 +0100 Subject: [PATCH 2/3] server: disable /static in -dev mode Signed-off-by: Alexander Bezzubov --- cmd/server/server.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/cmd/server/server.go b/cmd/server/server.go index 2878baf..8632c06 100644 --- a/cmd/server/server.go +++ b/cmd/server/server.go @@ -106,10 +106,11 @@ func main() { r.Get("/login", handleLogin) r.Get("/login/authorized", handleAuth) - // static - workDir, _ := os.Getwd() - filesDir := http.Dir(filepath.Join(workDir, "frontend", "dist")) - FileServer(r, "/static", filesDir) + if !*dev { // static + workDir, _ := os.Getwd() + filesDir := http.Dir(filepath.Join(workDir, "frontend", "dist")) + FileServer(r, "/static", filesDir) + } r.Route("/json", func(j chi.Router) { j.Use(setContentType("application/json")) From 91131cef5b8ea58a802fa07ce1dcc0c462e3fddf Mon Sep 17 00:00:00 2001 From: Alexander Bezzubov Date: Mon, 9 Nov 2020 23:05:43 +0100 Subject: [PATCH 3/3] initial build automation for deployment Signed-off-by: Alexander Bezzubov --- README.md | 11 +++++++++++ build.sh | 15 +++++++++++++++ 2 files changed, 26 insertions(+) create mode 100755 build.sh diff --git a/README.md b/README.md index e677f59..7e67f70 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,7 @@ a Gmail label from the Google Scholar alerts, grouping papers by title and produ * [Configure](#configure-1) * [Test](#test) * [Run](#run-1) + * [Deploy](#deploy) * [License](#license) # How to use @@ -146,6 +147,16 @@ to spin up a server at http://localhost:8080 Start by visiting http://localhost:8080/login to get the user OAuth access token. Visit http://localhost:8080/labels to chose your label name. +## Deploy +To deploy, you need to build a backend and the frontend first: +```sh +./build.sh +``` + +At the moment, `server` will serve static resources from the `./ frontend/dist` from `/static`. + +Actual deployment \w k8s descriptor is TBD. + # License Apache License, Version 2.0. See [LICENSE](LICENSE) diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..b3d2755 --- /dev/null +++ b/build.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env bash + +set -e + +echo "building Frontend" +pushd frontend/ +npm i +npm run build -- --env baseUrl=/json +popd + +echo "building Backend" +go build ./cmd/server + +echo "run ./server to start a server" +echo "go to http://localhost:8080/static"