Skip to content

Commit

Permalink
Merge pull request #252 from MadAppGang/quickfix/admin_host_settings
Browse files Browse the repository at this point in the history
add config file serve on top of current one
  • Loading branch information
erudenko authored Oct 19, 2021
2 parents 5ad97cc + 16ba022 commit e02fe39
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 1 deletion.
18 changes: 18 additions & 0 deletions storage/fs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package fs

import (
"io/fs"
"path"

"github.com/madappgang/identifo/model"
"github.com/spf13/afero"
Expand All @@ -20,3 +21,20 @@ func NewFS(settings model.FileStorageLocal) fs.FS {
),
)
}

// NewFSWithFiles creates the fs which already has predefined files on top of the base fs
func NewFSWithFiles(settings model.FileStorageLocal, files map[string][]byte) fs.FS {
base := afero.NewBasePathFs(afero.NewOsFs(), settings.FolderPath)
layer := afero.NewMemMapFs()
for filename, data := range files {
afero.WriteFile(layer, path.Join(settings.FolderPath, filename), data, 0644)
}
combined := afero.NewCopyOnWriteFs(base, layer)

return afero.NewIOFS(
afero.NewBasePathFs(
combined,
settings.FolderPath,
),
)
}
37 changes: 37 additions & 0 deletions storage/mem/map_overlay_fs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package mem

import (
"io/fs"
"os"
"path"

"github.com/spf13/afero"
)

func NewMapOverlayFS(base fs.FS, files map[string][]byte) fs.FS {
layer := afero.NewMemMapFs()
for filename, data := range files {
afero.WriteFile(layer, path.Clean(filename), data, 0644)
}

return &mapFS{
base: base,
memFS: afero.NewIOFS(layer),
}
}

type mapFS struct {
base fs.FS
memFS fs.FS
}

func (f *mapFS) Open(name string) (fs.File, error) {
mf, err := f.memFS.Open(name)
if err != nil {
if os.IsNotExist(err) || fs.ErrInvalid == err {
return f.base.Open(name)
}
return nil, err
}
return mf, nil
}
43 changes: 43 additions & 0 deletions storage/mem/map_overlay_fs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package mem_test

import (
"io/fs"
"path"
"testing"

"github.com/madappgang/identifo/storage/mem"
"github.com/spf13/afero"
)

func TestNewMapOverlayFS(t *testing.T) {
base := afero.NewMemMapFs()
afero.WriteFile(base, "./test1.txt", []byte("file1"), 644)
afero.WriteFile(base, "./path/test2.txt", []byte("file2"), 644)

files := map[string][]byte{
"./path/test3.txt": []byte("file3"),
"test4.txt": []byte("file4"),
}

m := mem.NewMapOverlayFS(afero.NewIOFS(base), files)

d1, _ := fs.ReadFile(m, "test1.txt")
if string(d1) != "file1" {
t.Fatalf("Error getting data, got %s, expected: %s", d1, "file1")
}

d2, _ := fs.ReadFile(m, path.Clean("./path/test2.txt"))
if string(d2) != "file2" {
t.Fatalf("Error getting data, got %s, expected: %s", d2, "file2")
}

d3, _ := fs.ReadFile(m, path.Clean("path/test3.txt"))
if string(d3) != "file3" {
t.Fatalf("Error getting data, got %s, expected: %s", d3, "file3")
}

d4, _ := fs.ReadFile(m, path.Clean("test4.txt"))
if string(d4) != "file4" {
t.Fatalf("Error getting data, got %s, expected: %s", d4, "file4")
}
}
12 changes: 11 additions & 1 deletion web/router.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package web

import (
"io/fs"
"log"
"net/http"

"github.com/madappgang/identifo/model"
"github.com/madappgang/identifo/storage/mem"
"github.com/madappgang/identifo/web/admin"
"github.com/madappgang/identifo/web/api"
"github.com/madappgang/identifo/web/authorization"
Expand Down Expand Up @@ -92,11 +94,12 @@ func NewRouter(settings RouterSetting) (model.Router, error) {
if err != nil {
return nil, err
}

// init admin panel web app
adminPanelAppSettings := spa.SPASettings{
Name: "ADMIN_PANEL",
Root: "/",
FileSystem: http.FS(settings.Server.Storages().AdminPanelFS),
FileSystem: http.FS(fsWithConfig(settings.Server.Storages().AdminPanelFS)),
}
r.AdminPanelRouter, err = spa.NewRouter(adminPanelAppSettings, nil, settings.Logger)
if err != nil {
Expand All @@ -108,6 +111,13 @@ func NewRouter(settings RouterSetting) (model.Router, error) {
return &r, nil
}

func fsWithConfig(fs fs.FS) fs.FS {
files := map[string][]byte{
"config.json": []byte(`{"apiUrl": "/admin"}`),
}
return mem.NewMapOverlayFS(fs, files)
}

// Router is a root router to handle REST API, web, and admin requests.
type Router struct {
APIRouter model.Router
Expand Down

0 comments on commit e02fe39

Please sign in to comment.