/
database.go
60 lines (47 loc) · 1.6 KB
/
database.go
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
55
56
57
58
59
60
package resources
import (
"fmt"
"net/http"
"strconv"
"github.com/canonical/lxd/lxd/response"
"github.com/canonical/microcluster/internal/state"
"github.com/canonical/microcluster/rest"
)
var databaseCmd = rest.Endpoint{
AllowedBeforeInit: true,
Path: "database",
Post: rest.EndpointAction{Handler: databasePost},
Patch: rest.EndpointAction{Handler: databasePatch},
}
func databasePost(state *state.State, r *http.Request) response.Response {
// Compare the dqlite version of the connecting client with our own.
versionHeader := r.Header.Get("X-Dqlite-Version")
if versionHeader == "" {
// No version header means an old pre dqlite 1.0 client.
versionHeader = "0"
}
_, err := strconv.Atoi(versionHeader)
if err != nil {
return response.BadRequest(fmt.Errorf("Invalid dqlite vesion: %w", err))
}
// Handle leader address requests.
if r.Header.Get("Upgrade") != "dqlite" {
return response.BadRequest(fmt.Errorf("Missing or invalid upgrade header"))
}
return response.EmptySyncResponse
}
func databasePatch(state *state.State, r *http.Request) response.Response {
// Compare the dqlite version of the connecting client with our own.
versionHeader := r.Header.Get("X-Dqlite-Version")
if versionHeader == "" {
// No version header means an old pre dqlite 1.0 client.
versionHeader = "0"
}
_, err := strconv.Atoi(versionHeader)
if err != nil {
return response.BadRequest(fmt.Errorf("Invalid dqlite vesion: %w", err))
}
// Notify this node that a schema upgrade has occured, in case we are waiting on one.
state.Database.NotifyUpgraded()
return response.EmptySyncResponse
}