From e27a8abc47d44ce0e91bada7747b3254901d717f Mon Sep 17 00:00:00 2001 From: Pushkar Anand Date: Wed, 3 Nov 2021 23:46:11 +0530 Subject: [PATCH 1/4] :sparkles: Embed the admin build in go binary --- internal/admin/admin.go | 26 ++++++++++++++++++++++++++ internal/server/server.go | 2 +- internal/server/web/web.go | 10 +++++++--- scripts/build.sh | 6 ++++++ 4 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 internal/admin/admin.go diff --git a/internal/admin/admin.go b/internal/admin/admin.go new file mode 100644 index 0000000..d4d0208 --- /dev/null +++ b/internal/admin/admin.go @@ -0,0 +1,26 @@ +package admin + +import ( + "embed" + "io/fs" + "net/http" + + "go.uber.org/zap" + + "github.com/gorilla/mux" +) + +//go:embed ui/dist +var adminUI embed.FS + +func Serve(r *mux.Router, log *zap.Logger) { + sub, err := fs.Sub(adminUI, "ui/dist") + if err != nil { + log.Error("failed to get subtree for admin pages", zap.Error(err)) + return + } + + adminFS := http.FileServer(http.FS(sub)) + + r.PathPrefix("/admin").Handler(http.StripPrefix("/admin", adminFS)) +} diff --git a/internal/server/server.go b/internal/server/server.go index 3144dce..ee968ac 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -70,7 +70,7 @@ func (s *Server) setup() { defer s.graceFullShutdown() if !s.apiOnly { - web.Routes(s.router) + web.Routes(s.router, s.logger) } apiRouter := s.router.PathPrefix("/api").Subrouter() diff --git a/internal/server/web/web.go b/internal/server/web/web.go index 7770cd0..3a7f801 100644 --- a/internal/server/web/web.go +++ b/internal/server/web/web.go @@ -1,7 +1,11 @@ package web -import "github.com/gorilla/mux" - -func Routes(r *mux.Router) { +import ( + "github.com/aveloper/blog/internal/admin" + "github.com/gorilla/mux" + "go.uber.org/zap" +) +func Routes(r *mux.Router, log *zap.Logger) { + admin.Serve(r, log) } diff --git a/scripts/build.sh b/scripts/build.sh index 01b4594..86c2ab3 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -1,5 +1,11 @@ #!/bin/sh +echo "Building Admin UI" + +cd internal/admin/ui && npm run build + +cd ../../.. + echo "Running go generate" go generate ./... From 5f5132707845b9bd7621d668ac76b304aaff93f8 Mon Sep 17 00:00:00 2001 From: Pushkar Anand Date: Wed, 3 Nov 2021 23:51:43 +0530 Subject: [PATCH 2/4] :green_heart: Add vue build step --- .github/workflows/ci.yaml | 6 +++ .github/workflows/codeql-analysis.yml | 53 ++++++++++++++------------- .github/workflows/staticcheck.yaml | 3 ++ 3 files changed, 37 insertions(+), 25 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 65ed95a..8acd53b 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -24,6 +24,9 @@ jobs: - name: Checkout Code uses: actions/checkout@v2 + - name: Build Vue App + run: cd internal/admin/ui && npm ci && npm run build + - name: Cache go mod uses: actions/cache@v2 with: @@ -55,6 +58,9 @@ jobs: - name: Checkout Code uses: actions/checkout@v2 + - name: Build Vue App + run: cd internal/admin/ui && npm ci && npm run build + - name: Cache go mod uses: actions/cache@v2 with: diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index 7d39b28..d402df3 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -38,34 +38,37 @@ jobs: # https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed steps: - - name: Checkout repository - uses: actions/checkout@v2 + - name: Checkout repository + uses: actions/checkout@v2 - # Initializes the CodeQL tools for scanning. - - name: Initialize CodeQL - uses: github/codeql-action/init@v1 - with: - languages: ${{ matrix.language }} - # If you wish to specify custom queries, you can do so here or in a config file. - # By default, queries listed here will override any specified in a config file. - # Prefix the list here with "+" to use these queries and those in the config file. - # queries: ./path/to/local/query, your-org/your-repo/queries@main + - name: Build Vue App + run: cd internal/admin/ui && npm ci && npm run build - # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). - # If this step fails, then you should remove it and run the build manually (see below) - - name: Autobuild - uses: github/codeql-action/autobuild@v1 + # Initializes the CodeQL tools for scanning. + - name: Initialize CodeQL + uses: github/codeql-action/init@v1 + with: + languages: ${{ matrix.language }} + # If you wish to specify custom queries, you can do so here or in a config file. + # By default, queries listed here will override any specified in a config file. + # Prefix the list here with "+" to use these queries and those in the config file. + # queries: ./path/to/local/query, your-org/your-repo/queries@main - # â„šī¸ Command-line programs to run using the OS shell. - # 📚 https://git.io/JvXDl + # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). + # If this step fails, then you should remove it and run the build manually (see below) + - name: Autobuild + uses: github/codeql-action/autobuild@v1 - # âœī¸ If the Autobuild fails above, remove it and uncomment the following three lines - # and modify them (or add more) to build your code if your project - # uses a compiled language + # â„šī¸ Command-line programs to run using the OS shell. + # 📚 https://git.io/JvXDl - #- run: | - # make bootstrap - # make release + # âœī¸ If the Autobuild fails above, remove it and uncomment the following three lines + # and modify them (or add more) to build your code if your project + # uses a compiled language - - name: Perform CodeQL Analysis - uses: github/codeql-action/analyze@v1 + #- run: | + # make bootstrap + # make release + + - name: Perform CodeQL Analysis + uses: github/codeql-action/analyze@v1 diff --git a/.github/workflows/staticcheck.yaml b/.github/workflows/staticcheck.yaml index 382465e..af5f2c6 100644 --- a/.github/workflows/staticcheck.yaml +++ b/.github/workflows/staticcheck.yaml @@ -22,6 +22,9 @@ jobs: - name: Checkout Code uses: actions/checkout@v2 + - name: Build Vue App + run: cd internal/admin/ui && npm ci && npm run build + - name: Cache go mod uses: actions/cache@v2 with: From 8a72d815365c9815e871d59a31c26b9c048455dc Mon Sep 17 00:00:00 2001 From: Adith A Danthi Date: Fri, 5 Nov 2021 17:54:46 +0530 Subject: [PATCH 3/4] =?UTF-8?q?=F0=9F=94=A7=20Added=20base=20public=20path?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/admin/ui/vite.config.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/internal/admin/ui/vite.config.js b/internal/admin/ui/vite.config.js index 315212d..7d481c7 100644 --- a/internal/admin/ui/vite.config.js +++ b/internal/admin/ui/vite.config.js @@ -3,5 +3,6 @@ import vue from '@vitejs/plugin-vue' // https://vitejs.dev/config/ export default defineConfig({ - plugins: [vue()] + plugins: [vue()], + base: '/admin/' }) From 716752693a3da0ef2f99f81cfc9340caed3b548d Mon Sep 17 00:00:00 2001 From: Pushkar Anand Date: Fri, 5 Nov 2021 18:20:27 +0530 Subject: [PATCH 4/4] :hammer: Exit script on command failure --- .gitignore | 1 + scripts/build.sh | 8 ++++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 7dd4c14..bc35d94 100644 --- a/.gitignore +++ b/.gitignore @@ -35,6 +35,7 @@ Desktop.ini # Build build/ output/ +dist/ # Config config.yaml \ No newline at end of file diff --git a/scripts/build.sh b/scripts/build.sh index 86c2ab3..20ca64b 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -2,18 +2,18 @@ echo "Building Admin UI" -cd internal/admin/ui && npm run build +cd internal/admin/ui && npm install && npm run build || exit 1 -cd ../../.. +cd ../../.. || exit 1 echo "Running go generate" -go generate ./... +go generate ./... || exit 1 echo "Fetching latest version information" # Fetch the latest to ensure we have the latest tag -git fetch origin +git fetch origin || exit 1 # Get the version from the git tag # If not available, default to v0.0.0