mirror of
https://github.com/NginxProxyManager/nginx-proxy-manager.git
synced 2025-08-28 11:40:04 +00:00
Rejig embedded assets to a central folder
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -6,7 +6,7 @@
|
|||||||
vendor
|
vendor
|
||||||
bin/*
|
bin/*
|
||||||
backend/config.json
|
backend/config.json
|
||||||
backend/internal/api/handler/assets
|
backend/embed/assets
|
||||||
test/node_modules
|
test/node_modules
|
||||||
*/node_modules
|
*/node_modules
|
||||||
docs/.vuepress/dist
|
docs/.vuepress/dist
|
||||||
|
15
backend/embed/main.go
Normal file
15
backend/embed/main.go
Normal 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
|
@@ -1,7 +1,6 @@
|
|||||||
package handler
|
package handler
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"embed"
|
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
@@ -10,19 +9,19 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"npm/embed"
|
||||||
h "npm/internal/api/http"
|
h "npm/internal/api/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:embed assets
|
var (
|
||||||
var assets embed.FS
|
assetsSub fs.FS
|
||||||
var assetsSub fs.FS
|
errIsDir = errors.New("path is dir")
|
||||||
|
)
|
||||||
var errIsDir = errors.New("path is dir")
|
|
||||||
|
|
||||||
// NotFound is a json error handler for 404's and method not allowed.
|
// 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.
|
// It also serves the react frontend as embedded files in the golang binary.
|
||||||
func NotFound() func(http.ResponseWriter, *http.Request) {
|
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) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
path := strings.TrimLeft(r.URL.Path, "/")
|
path := strings.TrimLeft(r.URL.Path, "/")
|
||||||
|
@@ -3,10 +3,11 @@ package handler
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/fs"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"npm/doc"
|
"npm/embed"
|
||||||
"npm/internal/api/schema"
|
"npm/internal/api/schema"
|
||||||
"npm/internal/config"
|
"npm/internal/config"
|
||||||
"npm/internal/logger"
|
"npm/internal/logger"
|
||||||
@@ -15,7 +16,10 @@ import (
|
|||||||
"github.com/jc21/jsref/provider"
|
"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
|
// Schema simply reads the swagger schema from disk and returns is raw
|
||||||
// Route: GET /schema
|
// Route: GET /schema
|
||||||
@@ -29,8 +33,10 @@ func Schema() func(http.ResponseWriter, *http.Request) {
|
|||||||
|
|
||||||
func getSchema() []byte {
|
func getSchema() []byte {
|
||||||
if swaggerSchema == nil {
|
if swaggerSchema == nil {
|
||||||
|
apiDocsSub, _ = fs.Sub(embed.APIDocFiles, "api_docs")
|
||||||
|
|
||||||
// nolint:gosec
|
// nolint:gosec
|
||||||
swaggerSchema, _ = doc.SwaggerFiles.ReadFile("api.swagger.json")
|
swaggerSchema, _ = fs.ReadFile(apiDocsSub, "api.swagger.json")
|
||||||
|
|
||||||
// Replace {{VERSION}} with Config Version
|
// Replace {{VERSION}} with Config Version
|
||||||
swaggerSchema = []byte(strings.ReplaceAll(string(swaggerSchema), "{{VERSION}}", config.Version))
|
swaggerSchema = []byte(strings.ReplaceAll(string(swaggerSchema), "{{VERSION}}", config.Version))
|
||||||
@@ -42,7 +48,7 @@ func getSchema() []byte {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
provider := provider.NewIoFS(doc.SwaggerFiles, "")
|
provider := provider.NewIoFS(apiDocsSub, "")
|
||||||
resolver := jsref.New()
|
resolver := jsref.New()
|
||||||
err := resolver.AddProvider(provider)
|
err := resolver.AddProvider(provider)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@@ -2,7 +2,6 @@ package database
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"embed"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"path"
|
"path"
|
||||||
@@ -11,15 +10,13 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"npm/embed"
|
||||||
"npm/internal/logger"
|
"npm/internal/logger"
|
||||||
"npm/internal/util"
|
"npm/internal/util"
|
||||||
|
|
||||||
"github.com/jmoiron/sqlx"
|
"github.com/jmoiron/sqlx"
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:embed migrations/*.sql
|
|
||||||
var migrationFiles embed.FS
|
|
||||||
|
|
||||||
// MigrationConfiguration options for the migrator.
|
// MigrationConfiguration options for the migrator.
|
||||||
type MigrationConfiguration struct {
|
type MigrationConfiguration struct {
|
||||||
Table string `json:"table"`
|
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.
|
// tableExists will check the database for the existence of the specified table.
|
||||||
func tableExists(db *sqlx.DB, tableName string) bool {
|
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)
|
row := db.QueryRowx(query, tableName)
|
||||||
if row == nil {
|
if row == nil {
|
||||||
@@ -131,7 +128,7 @@ func performFileMigrations(db *sqlx.DB) (int, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// List up the ".sql" files on disk
|
// 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() {
|
if !d.IsDir() {
|
||||||
shortFile := filepath.Base(file)
|
shortFile := filepath.Base(file)
|
||||||
|
|
||||||
@@ -143,7 +140,7 @@ func performFileMigrations(db *sqlx.DB) (int, error) {
|
|||||||
|
|
||||||
logger.Info("Migration: Importing %v", shortFile)
|
logger.Info("Migration: Importing %v", shortFile)
|
||||||
|
|
||||||
sqlContents, ioErr := migrationFiles.ReadFile(path.Clean(file))
|
sqlContents, ioErr := embed.MigrationFiles.ReadFile(path.Clean(file))
|
||||||
if ioErr != nil {
|
if ioErr != nil {
|
||||||
return ioErr
|
return ioErr
|
||||||
}
|
}
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
set -e
|
set -e
|
||||||
|
|
||||||
BACKEND_ASSETS=backend/internal/api/handler/assets
|
BACKEND_ASSETS=backend/embed/assets
|
||||||
|
|
||||||
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||||
. "$DIR/../.common.sh"
|
. "$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}"
|
echo -e "${BLUE}❯ ${GREEN}Building Frontend Complete${RESET}"
|
||||||
|
|
||||||
# to avoid CRA ejection, just copy these build files over to embed in the backend
|
# 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}"
|
cp -pr frontend/build "${BACKEND_ASSETS}"
|
||||||
echo -e "${BLUE}❯ ${GREEN}Copied build to ${BACKEND_ASSETS}${RESET}"
|
echo -e "${BLUE}❯ ${GREEN}Copied build to ${BACKEND_ASSETS}${RESET}"
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user