Skip to content

Commit

Permalink
feat: embed file system stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
dd84ai committed Nov 11, 2024
1 parent 7941b6b commit 0105848
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
go.work*
54 changes: 54 additions & 0 deletions darkcore/core_front/core_front.go
Original file line number Diff line number Diff line change
@@ -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 {

Check failure on line 27 in darkcore/core_front/core_front.go

View workflow job for this annotation

GitHub Actions / tests

undefined: utils_types.GetFilesParams

Check failure on line 27 in darkcore/core_front/core_front.go

View workflow job for this annotation

GitHub Actions / tests

undefined: utils_types.GetFilesParams
files := utils_types.GetFiles(fs, params)

Check failure on line 28 in darkcore/core_front/core_front.go

View workflow job for this annotation

GitHub Actions / tests

undefined: utils_types.GetFiles

Check failure on line 28 in darkcore/core_front/core_front.go

View workflow job for this annotation

GitHub Actions / tests

undefined: utils_types.GetFiles
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
}
13 changes: 13 additions & 0 deletions darkcore/core_front/core_front_test.go
Original file line number Diff line number Diff line change
@@ -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"])
}
4 changes: 0 additions & 4 deletions darkcore/core_front/example.templ
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,12 @@ 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:
<link rel="stylesheet" href={ core_types.GetCtx(ctx).GetStaticRoot() + file.Filename }/>
// @templ.Raw("<style>" + file.Content + "</style>")
case core_types.StaticFileJS:
<script src={ core_types.GetCtx(ctx).GetStaticRoot() + file.Filename }></script>
case core_types.StaticFileIco:
<link rel="icon" type="image/x-icon" href={ core_types.GetCtx(ctx).GetStaticRoot() + file.Filename }/>
default:
UNSUPPORTED STATIC FILE
}
}
5 changes: 0 additions & 5 deletions darkcore/core_front/example_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions darkcore/core_types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 0105848

Please sign in to comment.