-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathview.go
93 lines (83 loc) · 2.53 KB
/
view.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package mmoa
// Monolithic Message-Oriented Application (MMOA)
// View
// Copyright © 2016 Eduard Sesigin. All rights reserved. Contacts: <claygod@yandex.ru>
import (
"bytes"
"html/template"
"io/ioutil"
"strings"
"github.com/claygod/mmoa/service"
"github.com/claygod/mmoa/tools"
)
// NewView - create a new View
func NewView() *View {
v := &View{
the: tools.NewThemes(),
templates: make(map[tools.TypeSERVICE]map[tools.TypeTHEME]*template.Template),
}
return v
}
// View structure
type View struct {
the *tools.Themes
tpl *template.Template
templates map[tools.TypeSERVICE]map[tools.TypeTHEME]*template.Template
statusCodeOf tools.TypeTHEME
contentType string
}
// ContentType - for output
func (v *View) ContentType(ct string) {
v.contentType = ct
}
// StatusCodeOf - the answer to what theme will form status
func (v *View) StatusCodeOf(theme tools.TypeTHEME) {
v.statusCodeOf = theme
}
// TemplatePage - Loading of the page template
func (v *View) TemplatePage(path string) {
t, _ := ioutil.ReadFile(path)
tpl, _ := template.New(path).Parse(string(t))
v.tpl = tpl
}
// TemplateService - Loading of the service template
func (v *View) TemplateService(service tools.TypeSERVICE, theme tools.TypeTHEME, path string) {
t, _ := ioutil.ReadFile(path)
keyStr := string(theme)
tpl, _ := template.New(keyStr).Parse(string(t))
if _, ok := v.templates[service]; !ok {
v.templates[service] = make(map[tools.TypeTHEME]*template.Template)
}
v.templates[service][theme] = tpl
}
// ProcessingAggregate - processing the resulting aggregate, messages are displayed on templates
func (v *View) ProcessingAggregate(messages map[string]*tools.Message, statusCode int) (map[string]template.HTML, int) {
var sCode int = tools.StatusNotFound
var title bytes.Buffer
a := &service.Aggregate{}
arr := make(map[string]template.HTML)
for service, vw := range v.templates {
for theme, tpl := range vw {
keyStr := a.GenKey(service, theme)
if msg, ok := messages[keyStr]; ok && msg != nil {
if theme == v.statusCodeOf {
sCode = msg.MsgStatusCode
if ttl, ok := msg.MsgCtx[v.the.Attach.Title]; ok {
title.WriteString(ttl.(string))
title.WriteString(" ")
}
}
var doc bytes.Buffer
tpl.Execute(&doc, msg.MsgCtx)
arr[tpl.Name()] = template.HTML(doc.String())
} else {
if theme == v.statusCodeOf {
sCode = statusCode
title.WriteString(string(v.the.Trash.Timeout))
}
}
}
}
arr[v.the.Attach.Title] = template.HTML(strings.TrimSpace(title.String()))
return arr, sCode
}