Rejig embedded assets to a central folder

This commit is contained in:
Jamie Curnow
2021-06-30 10:50:55 +10:00
parent 4be9d4d509
commit 80a31b3041
71 changed files with 38 additions and 20 deletions

2
.gitignore vendored
View File

@@ -6,7 +6,7 @@
vendor
bin/*
backend/config.json
backend/internal/api/handler/assets
backend/embed/assets
test/node_modules
*/node_modules
docs/.vuepress/dist

15
backend/embed/main.go Normal file
View File

@@ -0,0 +1,15 @@
package embed
import "embed"
// APIDocFiles contain all the files used for swagger schema generation
//go:embed api_docs
var APIDocFiles embed.FS
// Assets are frontend assets served from within this app
//go:embed assets
var Assets embed.FS
// MigrationFiles are database migrations
//go:embed migrations/*.sql
var MigrationFiles embed.FS

View File

@@ -1,7 +1,6 @@
package handler
import (
"embed"
"errors"
"io"
"io/fs"
@@ -10,19 +9,19 @@ import (
"path/filepath"
"strings"
"npm/embed"
h "npm/internal/api/http"
)
//go:embed assets
var assets embed.FS
var assetsSub fs.FS
var errIsDir = errors.New("path is dir")
var (
assetsSub fs.FS
errIsDir = errors.New("path is dir")
)
// NotFound is a json error handler for 404's and method not allowed.
// It also serves the react frontend as embedded files in the golang binary.
func NotFound() func(http.ResponseWriter, *http.Request) {
assetsSub, _ = fs.Sub(assets, "assets")
assetsSub, _ = fs.Sub(embed.Assets, "assets")
return func(w http.ResponseWriter, r *http.Request) {
path := strings.TrimLeft(r.URL.Path, "/")

View File

@@ -3,10 +3,11 @@ package handler
import (
"encoding/json"
"fmt"
"io/fs"
"net/http"
"strings"
"npm/doc"
"npm/embed"
"npm/internal/api/schema"
"npm/internal/config"
"npm/internal/logger"
@@ -15,7 +16,10 @@ import (
"github.com/jc21/jsref/provider"
)
var swaggerSchema []byte
var (
swaggerSchema []byte
apiDocsSub fs.FS
)
// Schema simply reads the swagger schema from disk and returns is raw
// Route: GET /schema
@@ -29,8 +33,10 @@ func Schema() func(http.ResponseWriter, *http.Request) {
func getSchema() []byte {
if swaggerSchema == nil {
apiDocsSub, _ = fs.Sub(embed.APIDocFiles, "api_docs")
// nolint:gosec
swaggerSchema, _ = doc.SwaggerFiles.ReadFile("api.swagger.json")
swaggerSchema, _ = fs.ReadFile(apiDocsSub, "api.swagger.json")
// Replace {{VERSION}} with Config Version
swaggerSchema = []byte(strings.ReplaceAll(string(swaggerSchema), "{{VERSION}}", config.Version))
@@ -42,7 +48,7 @@ func getSchema() []byte {
return nil
}
provider := provider.NewIoFS(doc.SwaggerFiles, "")
provider := provider.NewIoFS(apiDocsSub, "")
resolver := jsref.New()
err := resolver.AddProvider(provider)
if err != nil {

View File

@@ -2,7 +2,6 @@ package database
import (
"database/sql"
"embed"
"fmt"
"io/fs"
"path"
@@ -11,15 +10,13 @@ import (
"sync"
"time"
"npm/embed"
"npm/internal/logger"
"npm/internal/util"
"github.com/jmoiron/sqlx"
)
//go:embed migrations/*.sql
var migrationFiles embed.FS
// MigrationConfiguration options for the migrator.
type MigrationConfiguration struct {
Table string `json:"table"`
@@ -100,7 +97,7 @@ func createMigrationTable(db *sqlx.DB) error {
// tableExists will check the database for the existence of the specified table.
func tableExists(db *sqlx.DB, tableName string) bool {
query := `SELECT name FROM sqlite_master WHERE type='table' AND name = $1`
query := `SELECT CASE name WHEN $1 THEN true ELSE false END AS found FROM sqlite_master WHERE type='table' AND name = $1`
row := db.QueryRowx(query, tableName)
if row == nil {
@@ -131,7 +128,7 @@ func performFileMigrations(db *sqlx.DB) (int, error) {
}
// List up the ".sql" files on disk
err := fs.WalkDir(migrationFiles, ".", func(file string, d fs.DirEntry, err error) error {
err := fs.WalkDir(embed.MigrationFiles, ".", func(file string, d fs.DirEntry, err error) error {
if !d.IsDir() {
shortFile := filepath.Base(file)
@@ -143,7 +140,7 @@ func performFileMigrations(db *sqlx.DB) (int, error) {
logger.Info("Migration: Importing %v", shortFile)
sqlContents, ioErr := migrationFiles.ReadFile(path.Clean(file))
sqlContents, ioErr := embed.MigrationFiles.ReadFile(path.Clean(file))
if ioErr != nil {
return ioErr
}

View File

@@ -1,7 +1,7 @@
#!/bin/bash
set -e
BACKEND_ASSETS=backend/internal/api/handler/assets
BACKEND_ASSETS=backend/embed/assets
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
. "$DIR/../.common.sh"
@@ -26,6 +26,7 @@ docker_cmd chown -R "$(id -u):$(id -g)" /app/frontend
echo -e "${BLUE} ${GREEN}Building Frontend Complete${RESET}"
# to avoid CRA ejection, just copy these build files over to embed in the backend
rm -rf ${BACKEND_ASSETS}
cp -pr frontend/build "${BACKEND_ASSETS}"
echo -e "${BLUE} ${GREEN}Copied build to ${BACKEND_ASSETS}${RESET}"