From 0105848448d803bf4b31043c5394480a5887f31e Mon Sep 17 00:00:00 2001 From: dd84ai Date: Mon, 11 Nov 2024 08:10:53 +0100 Subject: [PATCH] feat: embed file system stuff --- .gitignore | 1 + darkcore/core_front/core_front.go | 54 ++++++++++++++++++++++++++ darkcore/core_front/core_front_test.go | 13 +++++++ darkcore/core_front/example.templ | 4 -- darkcore/core_front/example_templ.go | 5 --- darkcore/core_types/types.go | 4 +- 6 files changed, 70 insertions(+), 11 deletions(-) create mode 100644 darkcore/core_front/core_front.go create mode 100644 darkcore/core_front/core_front_test.go diff --git a/.gitignore b/.gitignore index e69de29..2052709 100644 --- a/.gitignore +++ b/.gitignore @@ -0,0 +1 @@ +go.work* \ No newline at end of file diff --git a/darkcore/core_front/core_front.go b/darkcore/core_front/core_front.go new file mode 100644 index 0000000..2f24b77 --- /dev/null +++ b/darkcore/core_front/core_front.go @@ -0,0 +1,54 @@ +package core_front + +import ( + "embed" + + "github.com/darklab8/fl-darkcore/darkcore/core_types" + "github.com/darklab8/fl-darkcore/darkcore/settings/logus" + "github.com/darklab8/go-typelog/typelog" + "github.com/darklab8/go-utils/utils/utils_types" +) + +type StaticFilesystem struct { + Files []core_types.StaticFile + relPathToFile map[utils_types.FilePath]core_types.StaticFile +} + +func (fs StaticFilesystem) GetFileByRelPath(rel_path utils_types.FilePath) core_types.StaticFile { + file, ok := fs.relPathToFile[rel_path] + + if !ok { + logus.Log.Panic("expected file found by relpath", typelog.Any("relpath", rel_path)) + } + + return file +} + +func GetFiles(fs embed.FS, params utils_types.GetFilesParams) StaticFilesystem { + files := utils_types.GetFiles(fs, params) + var filesystem StaticFilesystem = StaticFilesystem{ + relPathToFile: make(map[utils_types.FilePath]core_types.StaticFile), + } + + for _, file := range files { + var static_file_kind core_types.StaticFileKind + + switch file.Extension { + case "js": + static_file_kind = core_types.StaticFileJS + case "css": + static_file_kind = core_types.StaticFileCSS + case "ico": + static_file_kind = core_types.StaticFileIco + } + + new_file := core_types.StaticFile{ + Filename: string(file.Relpath), + Kind: static_file_kind, + Content: string(file.Content), + } + filesystem.Files = append(filesystem.Files, new_file) + filesystem.relPathToFile[file.Relpath] = new_file + } + return filesystem +} diff --git a/darkcore/core_front/core_front_test.go b/darkcore/core_front/core_front_test.go new file mode 100644 index 0000000..63f43e2 --- /dev/null +++ b/darkcore/core_front/core_front_test.go @@ -0,0 +1,13 @@ +package core_front + +import ( + "fmt" + "testing" +) + +func TestCheckFilesystem(t *testing.T) { + + var SomeMap map[string]int = make(map[string]int) + + fmt.Println(SomeMap["not_exisitng_key"]) +} diff --git a/darkcore/core_front/example.templ b/darkcore/core_front/example.templ index a2dd463..b086cef 100644 --- a/darkcore/core_front/example.templ +++ b/darkcore/core_front/example.templ @@ -4,8 +4,6 @@ import "github.com/darklab8/fl-darkcore/darkcore/core_types" templ StaticFile(file core_types.StaticFile) { switch file.Kind { - case core_types.StaticFilePicture: - // pass case core_types.StaticFileCSS: // @templ.Raw("") @@ -13,7 +11,5 @@ templ StaticFile(file core_types.StaticFile) { case core_types.StaticFileIco: - default: - UNSUPPORTED STATIC FILE } } diff --git a/darkcore/core_front/example_templ.go b/darkcore/core_front/example_templ.go index d217108..8f8ef72 100644 --- a/darkcore/core_front/example_templ.go +++ b/darkcore/core_front/example_templ.go @@ -83,11 +83,6 @@ func StaticFile(file core_types.StaticFile) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - default: - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("UNSUPPORTED STATIC FILE") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } } return templ_7745c5c3_Err }) diff --git a/darkcore/core_types/types.go b/darkcore/core_types/types.go index c058269..a972f3d 100644 --- a/darkcore/core_types/types.go +++ b/darkcore/core_types/types.go @@ -13,10 +13,10 @@ type Url string type StaticFileKind int64 const ( - StaticFileCSS StaticFileKind = iota + StaticFileUnknown StaticFileKind = iota // default unkonwn StaticFileJS StaticFileIco - StaticFilePicture + StaticFileCSS ) type StaticFile struct {