summaryrefslogtreecommitdiff
path: root/app/controllers
diff options
context:
space:
mode:
Diffstat (limited to 'app/controllers')
-rw-r--r--app/controllers/health/controller.go43
-rw-r--r--app/controllers/sparkles/controller.go17
2 files changed, 43 insertions, 17 deletions
diff --git a/app/controllers/health/controller.go b/app/controllers/health/controller.go
new file mode 100644
index 0000000..99ff4cd
--- /dev/null
+++ b/app/controllers/health/controller.go
@@ -0,0 +1,43 @@
+package health
+
+import (
+ "context"
+ "net/http"
+ "time"
+
+ "github.com/xlgmokha/x/pkg/serde"
+ "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/db"
+)
+
+type Controller struct {
+ dbConnection *db.Connection
+}
+
+func New(dbConnection *db.Connection) *Controller {
+ return &Controller{
+ dbConnection: dbConnection,
+ }
+}
+
+func (c *Controller) MountTo(mux *http.ServeMux) {
+ mux.Handle("GET /-/health", http.HandlerFunc(c.Health))
+ mux.Handle("GET /-/health/database", http.HandlerFunc(c.Database))
+}
+
+func (c *Controller) Health(w http.ResponseWriter, r *http.Request) {
+ w.WriteHeader(http.StatusOK)
+ serde.ToHTTP(w, r, map[string]string{"status": "ok"})
+}
+
+func (c *Controller) Database(w http.ResponseWriter, r *http.Request) {
+ ctx, cancel := context.WithTimeout(r.Context(), 5*time.Second)
+ defer cancel()
+
+ if c.dbConnection.IsHealthy(ctx) {
+ w.WriteHeader(http.StatusOK)
+ serde.ToHTTP(w, r, map[string]string{"database": "ok"})
+ } else {
+ w.WriteHeader(http.StatusServiceUnavailable)
+ serde.ToHTTP(w, r, map[string]string{"database": "unavailable"})
+ }
+}
diff --git a/app/controllers/sparkles/controller.go b/app/controllers/sparkles/controller.go
index 90767b2..86610e1 100644
--- a/app/controllers/sparkles/controller.go
+++ b/app/controllers/sparkles/controller.go
@@ -3,7 +3,6 @@ package sparkles
import (
"net/http"
- "github.com/xlgmokha/x/pkg/log"
"github.com/xlgmokha/x/pkg/mapper"
"github.com/xlgmokha/x/pkg/serde"
"github.com/xlgmokha/x/pkg/x"
@@ -32,9 +31,6 @@ func (c *Controller) MountTo(mux *http.ServeMux) {
middleware.RequireUser(),
// middleware.RequirePermission("create", c.check),
))
-
- // This is a temporary endpoint to restore a backup
- mux.HandleFunc("POST /sparkles/restore", c.Restore)
}
func (c *Controller) Index(w http.ResponseWriter, r *http.Request) {
@@ -64,16 +60,3 @@ func (c *Controller) Create(w http.ResponseWriter, r *http.Request) {
return
}
}
-
-// This is a temporary endpoint to restore a backup
-// of sparkles and can be deleted once we have an actual database
-func (c *Controller) Restore(w http.ResponseWriter, r *http.Request) {
- sparkles, _ := serde.FromHTTP[[]*domain.Sparkle](r)
- log.WithFields(r.Context(), log.Fields{"sparkles": sparkles})
-
- x.Each(sparkles, func(sparkle *domain.Sparkle) {
- if err := c.db.Save(r.Context(), sparkle); err != nil {
- pls.LogError(r.Context(), err)
- }
- })
-}