refactor(services): Move staticservice to struct and interface

This commit is contained in:
2024-12-12 17:11:59 +01:00
parent 2a4f126377
commit b9e550d6f5
7 changed files with 23 additions and 12 deletions

View File

@@ -7,7 +7,16 @@ import (
"os"
)
func GetTemplate(locale, templateName string) (string, error) {
type StaticService interface {
GetTemplate(locale, templateName string) (string, error)
ListFilesInStaticSubFolder(folderName string) ([]string, error)
GetImage(imageName string) string
}
type DefaultStaticService struct {
}
func (s *DefaultStaticService) GetTemplate(locale, templateName string) (string, error) {
content, err := os.ReadFile(fmt.Sprintf("static/templates/%s/%s.html", templateName, locale))
if content == nil || err != nil {
log.Printf("error reading template %s with locale %s: %v", templateName, locale, err)
@@ -16,7 +25,7 @@ func GetTemplate(locale, templateName string) (string, error) {
return string(content), nil
}
func ListFilesInStaticSubFolder(folderName string) ([]string, error) {
func (s *DefaultStaticService) ListFilesInStaticSubFolder(folderName string) ([]string, error) {
files, err := os.ReadDir(fmt.Sprintf("static/%s", folderName))
if err != nil {
log.Printf("error reading files from folder %s: %v", folderName, err)
@@ -32,7 +41,7 @@ func ListFilesInStaticSubFolder(folderName string) ([]string, error) {
return images, nil
}
func GetImage(imageName string) string {
func (s *DefaultStaticService) GetImage(imageName string) string {
content, err := os.ReadFile("static/images/" + imageName + ".base64")
if content == nil || err != nil {
log.Printf("error reading image %s: %v", imageName, err)