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