oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1// Package teamprobe answers GET /api/team, the readiness probe Alta reads
2// before it activates a team Equipo in Home's directory (oficina/alta
3// design/team-bundle, spec §1.2).
4//
5// Equipo has no Home roster mode: its owners are seeded from
6// EQUIPO_OWNERS, so it answers in the seeded mode. It names the Home
7// consumer it was configured as, and whether an active owner exists. It
8// carries no address, name or count, and needs no session. An instance
9// without Home has no consumer and is never ready: 404.
10package teamprobe
11
12import (
13 "encoding/json"
14 "net/http"
15
16 "amadan.net/oficina/home/connect"
17 "gorm.io/gorm"
18)
19
20// Handler is the probe over Equipo's members table.
21func Handler(home *connect.DecodedCredentials, db *gorm.DB) http.HandlerFunc {
22 return func(w http.ResponseWriter, r *http.Request) {
23 if home == nil || home.ConsumerID == "" {
24 http.NotFound(w, r)
25 return
26 }
27 var owners int64
28 if err := db.WithContext(r.Context()).Raw(`SELECT count(*) FROM members WHERE active = 1 AND role = 'owner'`).Scan(&owners).Error; err != nil {
29 http.Error(w, http.StatusText(http.StatusServiceUnavailable), http.StatusServiceUnavailable)
30 return
31 }
32 roster := "owner-pending"
33 if owners > 0 {
34 roster = "ok"
35 }
36 w.Header().Set("Content-Type", "application/json")
37 w.Header().Set("Cache-Control", "no-store")
38 _ = json.NewEncoder(w).Encode(struct {
39 TeamMode bool `json:"team_mode"`
40 Seeded bool `json:"seeded"`
41 Consumer string `json:"consumer"`
42 Roster string `json:"roster"`
43 }{false, true, home.ConsumerID, roster})
44 }
45}
46