summaryrefslogtreecommitdiff
path: root/internal/appmeta/appmeta.go
blob: 72c8afc9839557e32603b3db8afed4d954d7e664 (plain)
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
50
51
52
53
54
// Package appmeta stores the small amount of per-app networking config
// Skiff itself needs (IP, port, health check, domains) that isn't already
// implied by jail.conf/rc.conf - a flat file living with the app's own
// data, not a separate control-plane database.
package appmeta

import (
	"encoding/json"
	"os"
)

// Domain is one hostname routed to the app; every non-Primary one redirects
// to whichever is Primary.
type Domain struct {
	Hostname string `json:"hostname"`
	Primary  bool   `json:"primary"`
}

type Meta struct {
	IP         string   `json:"ip"`
	Port       int      `json:"port"`
	HealthPath string   `json:"health_path"`
	Domains    []Domain `json:"domains,omitempty"`
	// Hostnames the app's cert actually covers as of the last sync - may
	// lag Domains after an add (not yet issued) or a remove (not pruned).
	TLSDomains []string `json:"tls_domains,omitempty"`
}

func (m Meta) PrimaryDomain() (Domain, bool) {
	for _, d := range m.Domains {
		if d.Primary {
			return d, true
		}
	}
	return Domain{}, false
}

func Write(path string, m Meta) error {
	b, err := json.MarshalIndent(m, "", "  ")
	if err != nil {
		return err
	}
	return os.WriteFile(path, b, 0644)
}

func Read(path string) (Meta, error) {
	var m Meta
	b, err := os.ReadFile(path)
	if err != nil {
		return m, err
	}
	err = json.Unmarshal(b, &m)
	return m, err
}