Compare commits

..

No commits in common. "21319dab48e77a27199ac621392706d040f099e3" and "1172e23c161d133eb5087af5fc24bd552d141b10" have entirely different histories.

7 changed files with 51 additions and 74 deletions

11
core.go
View file

@ -39,7 +39,7 @@ var basePath string
var disableEvents bool = false var disableEvents bool = false
//go:embed all:template //go:embed all:template
var components_resources embed.FS var resources embed.FS
type configContainer struct { type configContainer struct {
Request RequestConfig Request RequestConfig
@ -79,10 +79,6 @@ func (app *App) Bootstrap() {
NewEventsManager() NewEventsManager()
} }
func (app *App) RegisterTemplates(templates_resources embed.FS) {
NewTemplates(components_resources, templates_resources)
}
func (app *App) Run(router *httprouter.Router) { func (app *App) Run(router *httprouter.Router) {
portNumber := os.Getenv("App_HTTP_PORT") portNumber := os.Getenv("App_HTTP_PORT")
if portNumber == "" { if portNumber == "" {
@ -101,6 +97,11 @@ func (app *App) Run(router *httprouter.Router) {
// add public path // add public path
publicPath := os.Getenv("TEMPLATE_PUBLIC") publicPath := os.Getenv("TEMPLATE_PUBLIC")
router.ServeFiles("/public/*filepath", http.Dir(publicPath)) router.ServeFiles("/public/*filepath", http.Dir(publicPath))
// templates
var template = new(Template)
template.NewTemplates(resources)
} }
useHttpsStr := os.Getenv("App_USE_HTTPS") useHttpsStr := os.Getenv("App_USE_HTTPS")

View file

@ -6,7 +6,6 @@
package core package core
import ( import (
"bytes"
"fmt" "fmt"
"net/http" "net/http"
) )
@ -64,20 +63,6 @@ func (rs *Response) HTML(body string) *Response {
return rs 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 // TODO add doc
func (rs *Response) SetStatusCode(code int) *Response { func (rs *Response) SetStatusCode(code int) *Response {
if rs.isTerminated == false { if rs.isTerminated == false {

45
template.go Normal file
View file

@ -0,0 +1,45 @@
// Copyright (c) 2024 Zeni Kim <zenik@smarteching.com>
// 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)
}

View file

@ -1,8 +0,0 @@
{{define "head"}}
<head>
<meta charset="UTF-8">
<meta name="viewport" content="initial-scale=1, viewport-fit=cover, width=device-width, maximum-scale=1.0, minimum-scale=1.0">
<title>{{.}} | Goffee</title>
<link href="/public/style.css" rel="stylesheet">
</head>
{{end}}

View file

@ -1,5 +0,0 @@
package components
type Title struct {
Label string
}

View file

@ -1,5 +0,0 @@
{{define "title"}}
<div class="title">
<h1>{{.Label}}</h1>
</div>
{{end}}

View file

@ -1,36 +0,0 @@
// Copyright (c) 2024 Zeni Kim <zenik@smarteching.com>
// 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...))
}