zdravko/internal/server/handlers/handlers.go

85 lines
2 KiB
Go
Raw Normal View History

package handlers
import (
"embed"
2024-02-27 11:04:05 +00:00
"log/slog"
"github.com/gorilla/sessions"
2024-02-27 11:04:05 +00:00
"github.com/jmoiron/sqlx"
"github.com/mentos1386/zdravko/database"
"github.com/mentos1386/zdravko/internal/config"
"github.com/mentos1386/zdravko/pkg/script"
"github.com/mentos1386/zdravko/web/templates/components"
"go.temporal.io/sdk/client"
2024-05-25 17:55:25 +00:00
"gopkg.in/yaml.v3"
)
//go:embed examples.yaml
var examplesYaml embed.FS
type examples struct {
Check string `yaml:"check"`
Filter string `yaml:"filter"`
Trigger string `yaml:"trigger"`
Target string `yaml:"target"`
Hook string `yaml:"hook"`
}
var Pages = []*components.Page{
{Path: "/", Title: "Status", Breadcrumb: "Status"},
{Path: "/incidents", Title: "Incidents", Breadcrumb: "Incidents"},
{Path: "/settings", Title: "Settings", Breadcrumb: "Settings"},
}
func GetPageByTitle(pages []*components.Page, title string) *components.Page {
for _, p := range pages {
if p.Title == title {
return p
}
}
return nil
}
type BaseHandler struct {
db *sqlx.DB
kvStore database.KeyValueStore
config *config.ServerConfig
logger *slog.Logger
temporal client.Client
store *sessions.CookieStore
examples examples
}
func NewBaseHandler(db *sqlx.DB, kvStore database.KeyValueStore, temporal client.Client, config *config.ServerConfig, logger *slog.Logger) *BaseHandler {
store := sessions.NewCookieStore([]byte(config.SessionSecret))
examples := examples{}
yamlFile, err := examplesYaml.ReadFile("examples.yaml")
if err != nil {
panic(err)
}
err = yaml.Unmarshal(yamlFile, &examples)
if err != nil {
panic(err)
}
examples.Check = script.EscapeString(examples.Check)
examples.Filter = script.EscapeString(examples.Filter)
examples.Trigger = script.EscapeString(examples.Trigger)
examples.Target = script.EscapeString(examples.Target)
examples.Hook = script.EscapeString(examples.Hook)
2024-02-27 11:04:05 +00:00
return &BaseHandler{
db: db,
kvStore: kvStore,
2024-02-27 11:04:05 +00:00
config: config,
logger: logger,
temporal: temporal,
store: store,
examples: examples,
2024-02-27 11:04:05 +00:00
}
}