diff --git a/core.go b/core.go index ee5f9c4..d9093e1 100644 --- a/core.go +++ b/core.go @@ -6,6 +6,7 @@ package core import ( + "embed" "fmt" "log" "net/http" @@ -37,12 +38,15 @@ var mailer *Mailer var basePath string var disableEvents bool = false +//go:embed all:template +var resources embed.FS + type configContainer struct { Request RequestConfig } type App struct { - t int // for trancking hooks + t int // for tracking hooks chain *chain hooks *Hooks Config *configContainer @@ -88,11 +92,16 @@ func (app *App) Run(router *httprouter.Router) { TemplateEnableStr = "false" } TemplateEnable, _ := strconv.ParseBool(TemplateEnableStr) - // if enabled, add public path + // if enabled, if TemplateEnable { - + // add public path publicPath := os.Getenv("TEMPLATE_PUBLIC") router.ServeFiles("/public/*filepath", http.Dir(publicPath)) + + // templates + var template = new(Template) + template.NewTemplates(resources) + } useHttpsStr := os.Getenv("App_USE_HTTPS") diff --git a/template.go b/template.go new file mode 100644 index 0000000..a0724d7 --- /dev/null +++ b/template.go @@ -0,0 +1,45 @@ +// Copyright (c) 2024 Zeni Kim +// Use of this source code is governed by MIT-style +// license that can be found in the LICENSE file. + +package core + +import ( + "bytes" + "embed" + "fmt" + "html/template" + "io/fs" + "net/http" + "strings" +) + +type Template struct { + tmpl *template.Template +} + +func (t *Template) NewTemplates(resources embed.FS) { + var paths []string + fs.WalkDir(resources, ".", func(path string, d fs.DirEntry, err error) error { + if strings.Contains(d.Name(), ".html") { + paths = append(paths, path) + } + return nil + }) + + t.tmpl = template.Must(template.ParseFS(resources, paths...)) +} + +func (t *Template) Render(w http.ResponseWriter, name string, data interface{}) { + var buffer bytes.Buffer + + err := t.tmpl.ExecuteTemplate(&buffer, name, data) + if err != nil { + err = fmt.Errorf("error executing template: %w", err) + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + w.Header().Set("Content-Type", "text/html; charset=UTF-8") + buffer.WriteTo(w) +} diff --git a/template/components/button.go b/template/components/button.go new file mode 100644 index 0000000..0c52a38 --- /dev/null +++ b/template/components/button.go @@ -0,0 +1,10 @@ +package components + +type Button struct { + Text string + Link string + Icon string + IsSubmit bool + IsPrimary bool + IsDisabled bool +} diff --git a/template/components/button.html b/template/components/button.html new file mode 100644 index 0000000..6590905 --- /dev/null +++ b/template/components/button.html @@ -0,0 +1,22 @@ +{{define "button"}} + +{{end}} \ No newline at end of file