get body request, validator fix #3
7 changed files with 74 additions and 51 deletions
11
core.go
11
core.go
|
@ -39,7 +39,7 @@ var basePath string
|
|||
var disableEvents bool = false
|
||||
|
||||
//go:embed all:template
|
||||
var resources embed.FS
|
||||
var components_resources embed.FS
|
||||
|
||||
type configContainer struct {
|
||||
Request RequestConfig
|
||||
|
@ -79,6 +79,10 @@ 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 == "" {
|
||||
|
@ -97,11 +101,6 @@ 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")
|
||||
|
|
15
response.go
15
response.go
|
@ -6,6 +6,7 @@
|
|||
package core
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
@ -63,6 +64,20 @@ 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 {
|
||||
|
|
45
template.go
45
template.go
|
@ -1,45 +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 (
|
||||
"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)
|
||||
}
|
8
template/components/head.html
Normal file
8
template/components/head.html
Normal file
|
@ -0,0 +1,8 @@
|
|||
{{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}}
|
5
template/components/title.go
Normal file
5
template/components/title.go
Normal file
|
@ -0,0 +1,5 @@
|
|||
package components
|
||||
|
||||
type Title struct {
|
||||
Label string
|
||||
}
|
5
template/components/title.html
Normal file
5
template/components/title.html
Normal file
|
@ -0,0 +1,5 @@
|
|||
{{define "title"}}
|
||||
<div class="title">
|
||||
<h1>{{.Label}}</h1>
|
||||
</div>
|
||||
{{end}}
|
36
templates.go
Normal file
36
templates.go
Normal file
|
@ -0,0 +1,36 @@
|
|||
// 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...))
|
||||
}
|
Loading…
Reference in a new issue