-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
124 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package bot | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"sync" | ||
"text/template" | ||
|
||
"github.com/capcom6/service-monitor-tgbot/internal/config" | ||
) | ||
|
||
type TemplateName string | ||
|
||
const ( | ||
TemplateOnline TemplateName = "online" | ||
TemplateOffline TemplateName = "offline" | ||
) | ||
|
||
type Messages struct { | ||
Templates config.TelegramMessages | ||
cachedTemplates map[TemplateName]*template.Template | ||
mux sync.Mutex | ||
} | ||
|
||
type OnlineContext struct { | ||
Name string | ||
} | ||
|
||
type OfflineContext struct { | ||
OnlineContext | ||
Error string | ||
} | ||
|
||
func NewMessages(templates config.TelegramMessages) *Messages { | ||
return &Messages{ | ||
Templates: templates, | ||
cachedTemplates: make(map[TemplateName]*template.Template), | ||
} | ||
} | ||
|
||
func (m *Messages) prepare(name TemplateName) (prepared *template.Template, err error) { | ||
m.mux.Lock() | ||
defer m.mux.Unlock() | ||
|
||
if prepared = m.cachedTemplates[name]; prepared != nil { | ||
return prepared, nil | ||
} | ||
|
||
var tmplString string | ||
var ok bool | ||
if tmplString, ok = m.Templates[string(name)]; !ok { | ||
return nil, fmt.Errorf("template %s not found", name) | ||
} | ||
|
||
if m.cachedTemplates[name], err = template.New(string(name)).Parse(tmplString); err != nil { | ||
return nil, fmt.Errorf("can't parse template %s: %w", name, err) | ||
} | ||
|
||
return m.cachedTemplates[name], nil | ||
} | ||
|
||
func (m *Messages) Render(name TemplateName, context any) (string, error) { | ||
tmpl, err := m.prepare(name) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
builder := strings.Builder{} | ||
if err := tmpl.Execute(&builder, context); err != nil { | ||
return "", err | ||
} | ||
return builder.String(), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package config | ||
|
||
var ( | ||
defaultTelegramMessages TelegramMessages = TelegramMessages{ | ||
"online": "✅ {{.Name}} is *online*", | ||
"offline": "❌ {{.Name}} is *offline*: {{.Error}}", | ||
} | ||
|
||
defaultConfig Config = Config{ | ||
Telegram: Telegram{ | ||
Token: "", | ||
ChatID: 0, | ||
WebhookURL: "", | ||
Debug: false, | ||
Messages: defaultTelegramMessages, | ||
}, | ||
Services: []Service{}, | ||
Storage: Storage{}, | ||
} | ||
) |