70 lines
1.5 KiB
Go
70 lines
1.5 KiB
Go
package rep
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"path"
|
|
)
|
|
|
|
const (
|
|
pathGetInfo = "get-info"
|
|
pathSendState = "send-state"
|
|
pathStreamWAL = "stream-wal"
|
|
)
|
|
|
|
func (rep *Replicator) Handle(w http.ResponseWriter, r *http.Request) {
|
|
// We'll handle two types of requests: HTTP GET requests for JSON, or
|
|
// streaming requets for state or wall.
|
|
|
|
base := path.Base(r.URL.Path)
|
|
switch base {
|
|
case pathGetInfo:
|
|
rep.handleGetInfo(w, r)
|
|
case pathSendState:
|
|
rep.handleSendState(w, r)
|
|
case pathStreamWAL:
|
|
rep.handleStreamWAL(w, r)
|
|
default:
|
|
http.Error(w, "not found", http.StatusNotFound)
|
|
}
|
|
}
|
|
|
|
func (rep *Replicator) handleGetInfo(w http.ResponseWriter, r *http.Request) {
|
|
if !rep.checkBasicAuth(w, r) {
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
if err := json.NewEncoder(w).Encode(rep.Info()); err != nil {
|
|
rep.handlerLogf("Failed to send info: %s", err)
|
|
}
|
|
}
|
|
|
|
func (rep *Replicator) handleSendState(w http.ResponseWriter, r *http.Request) {
|
|
conn := rep.acceptConnect(w, r)
|
|
if conn == nil {
|
|
return
|
|
}
|
|
defer conn.Close()
|
|
|
|
if err := rep.sendState(conn); err != nil {
|
|
if !rep.stopped() {
|
|
rep.handlerLogf("Failed to send state: %s", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (rep *Replicator) handleStreamWAL(w http.ResponseWriter, r *http.Request) {
|
|
conn := rep.acceptConnect(w, r)
|
|
if conn == nil {
|
|
return
|
|
}
|
|
defer conn.Close()
|
|
|
|
err := rep.wal.Send(conn, rep.conf.NetTimeout)
|
|
if !rep.stopped() {
|
|
rep.handlerLogf("Failed when streaming WAL: %s", err)
|
|
}
|
|
}
|