diff --git a/database/models/models.go b/database/models/models.go index a4c9db9..3c5b0d8 100644 --- a/database/models/models.go +++ b/database/models/models.go @@ -6,20 +6,6 @@ import ( "time" ) -type OAuth2State struct { - State string `db:"state"` - ExpiresAt Time `db:"expires_at"` -} - -type MonitorStatus string - -const ( - MonitorSuccess MonitorStatus = "SUCCESS" - MonitorFailure MonitorStatus = "FAILURE" - MonitorError MonitorStatus = "ERROR" - MonitorUnknown MonitorStatus = "UNKNOWN" -) - type Time struct { Time time.Time } @@ -54,9 +40,23 @@ func (t *Time) Scan(src any) error { return nil } +type OAuth2State struct { + State string `db:"state"` + ExpiresAt *Time `db:"expires_at"` +} + +type MonitorStatus string + +const ( + MonitorSuccess MonitorStatus = "SUCCESS" + MonitorFailure MonitorStatus = "FAILURE" + MonitorError MonitorStatus = "ERROR" + MonitorUnknown MonitorStatus = "UNKNOWN" +) + type Monitor struct { - CreatedAt Time `db:"created_at"` - UpdatedAt Time `db:"updated_at"` + CreatedAt *Time `db:"created_at"` + UpdatedAt *Time `db:"updated_at"` Id string `db:"id"` Name string `db:"name"` @@ -73,7 +73,7 @@ type MonitorWithWorkerGroups struct { } type MonitorHistory struct { - CreatedAt Time `db:"created_at"` + CreatedAt *Time `db:"created_at"` MonitorId string `db:"monitor_id"` Status MonitorStatus `db:"status"` @@ -84,8 +84,8 @@ type MonitorHistory struct { } type WorkerGroup struct { - CreatedAt Time `db:"created_at"` - UpdatedAt Time `db:"updated_at"` + CreatedAt *Time `db:"created_at"` + UpdatedAt *Time `db:"updated_at"` Id string `db:"id"` Name string `db:"name"` diff --git a/internal/handlers/oauth2.go b/internal/handlers/oauth2.go index e123e7c..c423a20 100644 --- a/internal/handlers/oauth2.go +++ b/internal/handlers/oauth2.go @@ -89,7 +89,7 @@ func (h *BaseHandler) OAuth2LoginGET(c echo.Context) error { state := newRandomState() err := services.CreateOAuth2State(ctx, h.db, &models.OAuth2State{ State: state, - ExpiresAt: models.Time{Time: time.Now().Add(5 * time.Minute)}, + ExpiresAt: &models.Time{Time: time.Now().Add(5 * time.Minute)}, }) if err != nil { return err diff --git a/internal/handlers/settings.go b/internal/handlers/settings.go index 48f2433..359774a 100644 --- a/internal/handlers/settings.go +++ b/internal/handlers/settings.go @@ -36,15 +36,19 @@ var SettingsPages = []*components.Page{ {Path: "/settings/worker-groups", Title: "Worker Groups", Breadcrumb: "Worker Groups"}, {Path: "/settings/worker-groups/create", Title: "Worker Groups Create", Breadcrumb: "Create"}, {Path: "/settings/notifications", Title: "Notifications", Breadcrumb: "Notifications"}, + {Path: "/settings/notifications/create", Title: "Notifications Create", Breadcrumb: "Create"}, + {Path: "/settings/incidents", Title: "Incidents", Breadcrumb: "Incidents"}, + {Path: "/settings/incidents/create", Title: "Incidents Create", Breadcrumb: "Create"}, {Path: "/settings/temporal", Title: "Temporal", Breadcrumb: "Temporal"}, {Path: "/oauth2/logout", Title: "Logout", Breadcrumb: "Logout"}, } var SettingsNavbar = []*components.Page{ GetPageByTitle(SettingsPages, "Overview"), + GetPageByTitle(SettingsPages, "Incidents"), GetPageByTitle(SettingsPages, "Monitors"), - GetPageByTitle(SettingsPages, "Worker Groups"), GetPageByTitle(SettingsPages, "Notifications"), + GetPageByTitle(SettingsPages, "Worker Groups"), GetPageByTitle(SettingsPages, "Temporal"), GetPageByTitle(SettingsPages, "Logout"), } diff --git a/internal/handlers/settings_incidents.go b/internal/handlers/settings_incidents.go new file mode 100644 index 0000000..856b318 --- /dev/null +++ b/internal/handlers/settings_incidents.go @@ -0,0 +1,30 @@ +package handlers + +import ( + "net/http" + + "code.tjo.space/mentos1386/zdravko/web/templates/components" + "github.com/labstack/echo/v4" +) + +type Incident struct{} + +type SettingsIncidents struct { + *Settings + Incidents []*Incident +} + +func (h *BaseHandler) SettingsIncidentsGET(c echo.Context) error { + cc := c.(AuthenticatedContext) + + incidents := make([]*Incident, 0) + + return c.Render(http.StatusOK, "settings_incidents.tmpl", &SettingsIncidents{ + Settings: NewSettings( + cc.Principal.User, + GetPageByTitle(SettingsPages, "Incidents"), + []*components.Page{GetPageByTitle(SettingsPages, "Incidents")}, + ), + Incidents: incidents, + }) +} diff --git a/internal/handlers/settings_notifications.go b/internal/handlers/settings_notifications.go new file mode 100644 index 0000000..2caa45d --- /dev/null +++ b/internal/handlers/settings_notifications.go @@ -0,0 +1,30 @@ +package handlers + +import ( + "net/http" + + "code.tjo.space/mentos1386/zdravko/web/templates/components" + "github.com/labstack/echo/v4" +) + +type Notification struct{} + +type SettingsNotifications struct { + *Settings + Notifications []*Notification +} + +func (h *BaseHandler) SettingsNotificationsGET(c echo.Context) error { + cc := c.(AuthenticatedContext) + + notifications := make([]*Notification, 0) + + return c.Render(http.StatusOK, "settings_notifications.tmpl", &SettingsNotifications{ + Settings: NewSettings( + cc.Principal.User, + GetPageByTitle(SettingsPages, "Notifications"), + []*components.Page{GetPageByTitle(SettingsPages, "Notifications")}, + ), + Notifications: notifications, + }) +} diff --git a/internal/handlers/settingsmonitors.go b/internal/handlers/settingsmonitors.go index 5693a3b..4a50b2c 100644 --- a/internal/handlers/settingsmonitors.go +++ b/internal/handlers/settingsmonitors.go @@ -35,8 +35,7 @@ type MonitorWithWorkerGroupsAndStatus struct { type SettingsMonitors struct { *Settings - Monitors []*MonitorWithWorkerGroupsAndStatus - MonitorsLength int + Monitors []*MonitorWithWorkerGroupsAndStatus } type SettingsMonitor struct { @@ -71,8 +70,7 @@ func (h *BaseHandler) SettingsMonitorsGET(c echo.Context) error { GetPageByTitle(SettingsPages, "Monitors"), []*components.Page{GetPageByTitle(SettingsPages, "Monitors")}, ), - Monitors: monitorsWithStatus, - MonitorsLength: len(monitorsWithStatus), + Monitors: monitorsWithStatus, }) } diff --git a/internal/handlers/settingsworkergroups.go b/internal/handlers/settingsworkergroups.go index 7147483..c7659f9 100644 --- a/internal/handlers/settingsworkergroups.go +++ b/internal/handlers/settingsworkergroups.go @@ -27,8 +27,7 @@ type WorkerGroupWithActiveWorkers struct { type SettingsWorkerGroups struct { *Settings - WorkerGroups []*WorkerGroupWithActiveWorkers - WorkerGroupsLength int + WorkerGroups []*WorkerGroupWithActiveWorkers } type SettingsWorker struct { @@ -62,8 +61,7 @@ func (h *BaseHandler) SettingsWorkerGroupsGET(c echo.Context) error { GetPageByTitle(SettingsPages, "Worker Groups"), []*components.Page{GetPageByTitle(SettingsPages, "Worker Groups")}, ), - WorkerGroups: workerGroupsWithActiveWorkers, - WorkerGroupsLength: len(workerGroupsWithActiveWorkers), + WorkerGroups: workerGroupsWithActiveWorkers, }) } diff --git a/internal/services/oauth2_state.go b/internal/services/oauth2_state.go index 9894685..32e7f0f 100644 --- a/internal/services/oauth2_state.go +++ b/internal/services/oauth2_state.go @@ -16,7 +16,7 @@ func CreateOAuth2State(ctx context.Context, db *sqlx.DB, oauth2State *models.OAu } func DeleteOAuth2State(ctx context.Context, db *sqlx.DB, state string) (deleted bool, err error) { - res, err := db.ExecContext(ctx, "DELETE FROM oauth2_states WHERE state = $1 AND expires_at > datetime('now')", state) + res, err := db.ExecContext(ctx, "DELETE FROM oauth2_states WHERE state = $1 AND expires_at > strftime('%Y-%m-%dT%H:%M:%fZ')", state) if err != nil { return false, err } diff --git a/pkg/server/routes.go b/pkg/server/routes.go index ceea675..80bb311 100644 --- a/pkg/server/routes.go +++ b/pkg/server/routes.go @@ -45,6 +45,7 @@ func Routes( settings := e.Group("/settings") settings.Use(h.Authenticated) settings.GET("", h.SettingsOverviewGET) + settings.GET("/incidents", h.SettingsIncidentsGET) settings.GET("/monitors", h.SettingsMonitorsGET) settings.GET("/monitors/create", h.SettingsMonitorsCreateGET) settings.POST("/monitors/create", h.SettingsMonitorsCreatePOST) @@ -53,6 +54,7 @@ func Routes( settings.GET("/monitors/:id/delete", h.SettingsMonitorsDescribeDELETE) settings.GET("/monitors/:id/disable", h.SettingsMonitorsDisableGET) settings.GET("/monitors/:id/enable", h.SettingsMonitorsEnableGET) + settings.GET("/notifications", h.SettingsNotificationsGET) settings.GET("/worker-groups", h.SettingsWorkerGroupsGET) settings.GET("/worker-groups/create", h.SettingsWorkerGroupsCreateGET) settings.POST("/worker-groups/create", h.SettingsWorkerGroupsCreatePOST) diff --git a/web/templates/pages/settings_incidents.tmpl b/web/templates/pages/settings_incidents.tmpl new file mode 100644 index 0000000..b4a06d8 --- /dev/null +++ b/web/templates/pages/settings_incidents.tmpl @@ -0,0 +1,82 @@ +{{ define "settings" }} + {{ $description := "Incidents can be manually created or based on monitors. They are used to notify about issues." }} + + {{ $length := len .Incidents }} + {{ if eq $length 0 }} +
+ {{ else }} +Name | +Notifications | +Action | +
---|---|---|
+ {{ .Name }} + | ++ {{ range .Notifications }} + + {{ . }} + + {{ end }} + | ++ Details + | +
+ {{ $description }} +
+ +Name | +Type | +Action | +
---|---|---|
+ {{ .Name }} + | ++ + {{ .Type }} + + | ++ Details + | +