-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderMiddleware.go
73 lines (63 loc) · 1.97 KB
/
renderMiddleware.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"fmt"
"log"
"net/http"
"path/filepath"
"reflect"
"runtime"
"strings"
"github.com/gin-contrib/multitemplate"
"github.com/gin-gonic/gin"
)
func AutoTemplate(c *gin.Context, controller interface{}, handlerFunc interface{}) {
fullFuncName := runtime.FuncForPC(reflect.ValueOf(handlerFunc).Pointer()).Name()
parts := strings.Split(fullFuncName, ".")
if len(parts) < 3 {
log.Printf("[ERROR] Invalid function name: %s", fullFuncName)
c.String(http.StatusInternalServerError, "Invalid function name format")
return
}
// Sestavení cesty k šabloně
templatePath, err := buildTemplatePath(c.Param("lang"), parts)
if err != nil {
log.Printf("[ERROR] %v", err)
c.String(http.StatusInternalServerError, "Failed to build template path")
return
}
// Vykreslení šablony
c.HTML(http.StatusOK, templatePath, gin.H{
"Title": "Dynamická šablona",
"H1": "Automatické šablony",
})
}
// Pomocná funkce pro sestavení cesty k šabloně
func buildTemplatePath(lang string, parts []string) (string, error) {
if lang == "" {
lang = "cs" // Výchozí jazyk
}
// Cesta k balíčku a sestavení názvů
packagePath := strings.Join(parts[:len(parts)-2], "/")
controllerName := strings.ToLower(parts[len(parts)-2])
methodName := strings.ToLower(parts[len(parts)-1])
// Sestavení výsledné cesty
templateName := fmt.Sprintf("%s/%s/%s.gohtml", packagePath, controllerName, methodName)
return fmt.Sprintf("%s/%s", lang, templateName), nil
}
func createRenderer() multitemplate.Renderer {
r := multitemplate.NewRenderer()
languages := []string{"cs", "en"}
for _, lang := range languages {
templates, err := filepath.Glob(fmt.Sprintf("templates/%s/**/*.gohtml", lang))
if err != nil {
log.Printf("[ERROR] Failed to load templates: %v", err)
continue
}
// Mapování šablon do rendereru
for _, tmpl := range templates {
name := strings.TrimPrefix(tmpl, fmt.Sprintf("templates/%s/", lang))
r.AddFromFiles(name, tmpl)
}
}
return r
}