diff --git a/core.go b/core.go index ba208d3..d9093e1 100644 --- a/core.go +++ b/core.go @@ -39,7 +39,7 @@ var basePath string var disableEvents bool = false //go:embed all:template -var components_resources embed.FS +var resources embed.FS type configContainer struct { Request RequestConfig @@ -79,10 +79,6 @@ func (app *App) Bootstrap() { NewEventsManager() } -func (app *App) RegisterTemplates(templates_resources embed.FS) { - NewTemplates(components_resources, templates_resources) -} - func (app *App) Run(router *httprouter.Router) { portNumber := os.Getenv("App_HTTP_PORT") if portNumber == "" { @@ -101,6 +97,11 @@ func (app *App) Run(router *httprouter.Router) { // 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/response.go b/response.go index 7c03b15..1b869df 100644 --- a/response.go +++ b/response.go @@ -6,7 +6,6 @@ package core import ( - "bytes" "fmt" "net/http" ) @@ -64,20 +63,6 @@ func (rs *Response) HTML(body string) *Response { return rs } -// TODO add doc -func (rs *Response) Template(name string, data interface{}) *Response { - var buffer bytes.Buffer - if rs.isTerminated == false { - err := tmpl.ExecuteTemplate(&buffer, name, data) - if err != nil { - panic(fmt.Sprintf("error executing template: %v", err)) - } - rs.contentType = CONTENT_TYPE_HTML - buffer.WriteTo(rs.HttpResponseWriter) - } - return rs -} - // TODO add doc func (rs *Response) SetStatusCode(code int) *Response { if rs.isTerminated == false { 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/head.html b/template/components/head.html deleted file mode 100644 index cecc5ed..0000000 --- a/template/components/head.html +++ /dev/null @@ -1,8 +0,0 @@ -{{define "head"}} - - - - {{.}} | Goffee - - -{{end}} \ No newline at end of file diff --git a/template/components/title.go b/template/components/title.go deleted file mode 100644 index 0e68b6a..0000000 --- a/template/components/title.go +++ /dev/null @@ -1,5 +0,0 @@ -package components - -type Title struct { - Label string -} diff --git a/template/components/title.html b/template/components/title.html deleted file mode 100644 index eef5bf3..0000000 --- a/template/components/title.html +++ /dev/null @@ -1,5 +0,0 @@ -{{define "title"}} -
-

{{.Label}}

-
-{{end}} \ No newline at end of file diff --git a/templates.go b/templates.go deleted file mode 100644 index 38ef4f2..0000000 --- a/templates.go +++ /dev/null @@ -1,36 +0,0 @@ -// 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 ( - "embed" - "html/template" - "io/fs" - "strings" -) - -var tmpl *template.Template = nil - -func NewTemplates(components embed.FS, templates embed.FS) { - // templates - var paths []string - fs.WalkDir(components, ".", func(path string, d fs.DirEntry, err error) error { - if strings.Contains(d.Name(), ".html") { - paths = append(paths, path) - } - return nil - }) - - var pathst []string - fs.WalkDir(templates, ".", func(patht string, d fs.DirEntry, err error) error { - if strings.Contains(d.Name(), ".html") { - pathst = append(pathst, patht) - } - return nil - }) - - tmpla := template.Must(template.ParseFS(components, paths...)) - tmpl = template.Must(tmpla.ParseFS(templates, pathst...)) -}