53 lines
1.2 KiB
Go
53 lines
1.2 KiB
Go
|
package http
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"net/http"
|
||
|
|
||
|
"github.com/hashicorp/vault/vault"
|
||
|
)
|
||
|
|
||
|
// Handler returns an http.Handler for the API. This can be used on
|
||
|
// its own to mount the Vault API within another web server.
|
||
|
func Handler(core *vault.Core) http.Handler {
|
||
|
mux := http.NewServeMux()
|
||
|
mux.Handle("/sys/seal-status", handleSysSealStatus(core))
|
||
|
mux.Handle("/sys/seal", handleSysSeal(core))
|
||
|
mux.Handle("/sys/unseal", handleSysUnseal(core))
|
||
|
return mux
|
||
|
}
|
||
|
|
||
|
func parseRequest(r *http.Request, out interface{}) error {
|
||
|
dec := json.NewDecoder(r.Body)
|
||
|
return dec.Decode(out)
|
||
|
}
|
||
|
|
||
|
func respondError(w http.ResponseWriter, status int, err error) {
|
||
|
w.Header().Add("Content-Type", "application/json")
|
||
|
w.WriteHeader(status)
|
||
|
|
||
|
resp := &ErrorResponse{Errors: make([]string, 0, 1)}
|
||
|
if err != nil {
|
||
|
resp.Errors = append(resp.Errors, err.Error())
|
||
|
}
|
||
|
|
||
|
enc := json.NewEncoder(w)
|
||
|
enc.Encode(resp)
|
||
|
}
|
||
|
|
||
|
func respondOk(w http.ResponseWriter, body interface{}) {
|
||
|
w.Header().Add("Content-Type", "application/json")
|
||
|
|
||
|
if body == nil {
|
||
|
w.WriteHeader(http.StatusNoContent)
|
||
|
} else {
|
||
|
w.WriteHeader(http.StatusOK)
|
||
|
enc := json.NewEncoder(w)
|
||
|
enc.Encode(body)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
type ErrorResponse struct {
|
||
|
Errors []string `json:"errors"`
|
||
|
}
|