Skip to content

Commit

Permalink
feat(theme): convert assets type to slice
Browse files Browse the repository at this point in the history
  • Loading branch information
marjune163 committed Mar 5, 2024
1 parent 6e14ea4 commit cd6175d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 24 deletions.
12 changes: 6 additions & 6 deletions src/tpl/defaultTheme/defaultTheme.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import (
//go:embed frontend/index.html
var defaultTplStr string

//go:embed frontend/favicon.ico
var defaultFavicon []byte

//go:embed frontend/index.css
var defaultCss []byte

//go:embed frontend/index.js
var defaultJs []byte

//go:embed frontend/favicon.ico
var defaultFavicon []byte

var DefaultTheme theme.MemTheme

func init() {
Expand All @@ -29,8 +29,8 @@ func init() {
}

DefaultTheme.Assets = theme.Assets{
"favicon.ico": {"image/x-icon", bytes.NewReader(defaultFavicon)},
"index.css": {"text/css; charset=utf-8", bytes.NewReader(defaultCss)},
"index.js": {"application/javascript; charset=utf-8", bytes.NewReader(defaultJs)},
{"index.css", "text/css; charset=utf-8", bytes.NewReader(defaultCss)},
{"index.js", "application/javascript; charset=utf-8", bytes.NewReader(defaultJs)},
{"favicon.ico", "image/x-icon", bytes.NewReader(defaultFavicon)},
}
}
16 changes: 9 additions & 7 deletions src/tpl/theme/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,25 @@ import (
)

type Asset struct {
Path string
ContentType string
ReadSeeker io.ReadSeeker
}

type Assets map[string]Asset
type Assets []Asset

func (assets Assets) Set(path string, content []byte) error {
func (assets Assets) Append(path string, content []byte) (Assets, error) {
rd := bytes.NewReader(content)
ctype, err := util.GetContentType(path, rd)
if err != nil {
return err
return assets, err
}

asset := Asset{
assets = append(assets, Asset{
Path: path,
ContentType: ctype,
ReadSeeker: rd,
}
assets[path] = asset
return nil
})

return assets, nil
}
26 changes: 15 additions & 11 deletions src/tpl/theme/memTheme.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ type MemTheme struct {
var initTime = time.Now()

func LoadMemTheme(themePath string) (theme MemTheme, err error) {
var currentTheme = MemTheme{
Template: nil,
Assets: make(Assets, 3),
}

// assume to be a zip file
var zipRd *zip.ReadCloser
zipRd, err = zip.OpenReader(themePath)
Expand All @@ -30,6 +25,11 @@ func LoadMemTheme(themePath string) (theme MemTheme, err error) {
}
defer zipRd.Close()

currentTheme := MemTheme{
Template: nil,
Assets: make(Assets, 0, len(zipRd.File)-1), // exclude template file
}

for _, f := range zipRd.File {
var rd io.ReadCloser
rd, err = f.Open()
Expand All @@ -48,7 +48,7 @@ func LoadMemTheme(themePath string) (theme MemTheme, err error) {
return
}
} else {
currentTheme.Assets.Set(f.Name, raw)
currentTheme.Assets, _ = currentTheme.Assets.Append(f.Name, raw)
}
}

Expand All @@ -66,12 +66,16 @@ func (theme MemTheme) RenderPage(w io.Writer, data interface{}) error {
}

func (theme MemTheme) RenderAsset(w http.ResponseWriter, r *http.Request, assetPath string) {
asset, ok := theme.Assets[assetPath]
if !ok {
w.WriteHeader(http.StatusNotFound)
assets := theme.Assets
for i := range assets {
if assets[i].Path != assetPath {
continue
}
w.Header().Set("Content-Type", assets[i].ContentType)
http.ServeContent(w, r, assetPath, initTime, assets[i].ReadSeeker)
return
}

w.Header().Set("Content-Type", asset.ContentType)
http.ServeContent(w, r, assetPath, initTime, asset.ReadSeeker)
w.WriteHeader(http.StatusNotFound)
return
}

0 comments on commit cd6175d

Please sign in to comment.