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

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 {