More Updates to github actions and bundled frontend files

This commit is contained in:
Justin Hammond 2022-08-16 12:12:50 +08:00
parent e7212e49e1
commit d0079821d8
3 changed files with 33 additions and 8 deletions

View file

@ -49,7 +49,7 @@ jobs:
with:
node-version: 16
- name: Build Frontend
run: cd frontend && npm run build
run: cd frontend && npm install && npm run build
- name: Initialize CodeQL
uses: github/codeql-action/init@v1
with:
@ -63,13 +63,20 @@ jobs:
runs-on: ubuntu-latest
needs: [ test ]
steps:
- name: Install Go
- name: Install Go
if: success()
uses: actions/setup-go@v1
with:
go-version: 1.18.x
- name: Checkout code
- name: Checkout code
uses: actions/checkout@v1
- name: Install NPM
if: success()
uses: actions/setup-node@v3
with:
node-version: 16
- name: Build Frontend
run: cd frontend && npm install && npm run build
- name: Run tests
run: go mod tidy && go test -v -race -covermode=atomic -coverprofile=coverage.out ./...
- name: CodeCov

View file

@ -2,10 +2,9 @@ package frontend
import (
"embed"
_ "embed"
)
//go:generate npm run build
//go:embed dist/*
//go:embed dist
var FrontEndFiles embed.FS

25
main.go
View file

@ -29,14 +29,16 @@ import (
// "context"
"fmt"
"net/http"
"io/fs"
// "reflect"
"strings"
// "unsafe"
"runtime/debug"
"encoding/json"
"os"
"runtime/debug"
"github.com/Fishwaldo/mouthpiece/frontend"
_ "github.com/Fishwaldo/mouthpiece/frontend"
mouthpiece "github.com/Fishwaldo/mouthpiece/internal"
"github.com/Fishwaldo/mouthpiece/internal/app"
@ -63,6 +65,11 @@ import (
"github.com/spf13/viper"
)
func init() {
viper.SetDefault("frontend.path", "frontend/dist")
viper.SetDefault("frontend.external", false)
}
// FileServer conveniently sets up a http.FileServer handler to serve static files from a http.FileSystem.
// Borrowed from https://github.com/go-chi/chi/blob/master/_examples/fileserver/main.go
func fileServer(r chi.Router, path string, root http.FileSystem) {
@ -132,7 +139,7 @@ func main() {
// Create a new router & CLI with default middleware.
InitLogger()
db.InitializeDB()
humucli := cli.NewRouter("MouthPiece", "0.0.1")
humucli := cli.NewRouter(bi.Name, bi.GitVersion)
humucli.DisableSchemaProperty()
humucli.PreStart(transport.InitializeTransports)
humucli.PreStart(msg.InitializeMessage)
@ -156,7 +163,19 @@ func main() {
mux.Mount("/auth", authRoutes)
mux.Mount("/avatar", avaRoutes)
fileServer(mux, "/static", http.Dir("frontend/dist/"))
var httpfiles http.FileSystem
if viper.GetBool("frontend.external") {
Log.Info("Serving frontend from external location", "path", viper.GetString("frontend.path"))
httpfiles = http.Dir(viper.GetString("frontend.path"))
} else {
Log.Info("Serving frontend from Bundled Files")
subdir, err := fs.Sub(frontend.FrontEndFiles, "dist")
if err != nil {
Log.Error(err, "Failed to get subdir")
}
httpfiles = http.FS(subdir)
}
fileServer(mux, "/static", httpfiles)
// Declare the root resource and a GET operation on it.
humucli.Resource("/health").Get("get-health", "Get Health of the Service",