File: probes.go

package info (click to toggle)
golang-github-zitadel-oidc 3.37.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, sid, trixie
  • size: 1,484 kB
  • sloc: makefile: 5
file content (49 lines) | stat: -rw-r--r-- 958 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package op

import (
	"context"
	"errors"
	"net/http"

	httphelper "github.com/zitadel/oidc/v3/pkg/http"
)

type ProbesFn func(context.Context) error

func healthHandler(w http.ResponseWriter, r *http.Request) {
	ok(w)
}

func readyHandler(probes []ProbesFn) func(w http.ResponseWriter, r *http.Request) {
	return func(w http.ResponseWriter, r *http.Request) {
		Readiness(w, r, probes...)
	}
}

func Readiness(w http.ResponseWriter, r *http.Request, probes ...ProbesFn) {
	ctx := r.Context()
	for _, probe := range probes {
		if err := probe(ctx); err != nil {
			http.Error(w, "not ready", http.StatusInternalServerError)
			return
		}
	}
	ok(w)
}

func ReadyStorage(s Storage) ProbesFn {
	return func(ctx context.Context) error {
		if s == nil {
			return errors.New("no storage")
		}
		return s.Health(ctx)
	}
}

func ok(w http.ResponseWriter) {
	httphelper.MarshalJSON(w, Status{"ok"})
}

type Status struct {
	Status string `json:"status,omitempty"`
}