more semantic config upload
This commit is contained in:
@ -11,6 +11,7 @@ import (
|
||||
|
||||
cfsslconfig "github.com/cloudflare/cfssl/config"
|
||||
"github.com/emicklei/go-restful"
|
||||
"gopkg.in/yaml.v2"
|
||||
"m.cluseau.fr/go/httperr"
|
||||
|
||||
"novit.tech/direktil/pkg/localconfig"
|
||||
@ -75,7 +76,7 @@ func registerWS(rest *restful.Container) {
|
||||
|
||||
// - configs API
|
||||
ws.Route(ws.POST("/configs").To(wsUploadConfig).
|
||||
Consumes(mime.YAML).Param(ws.BodyParameter("config", "The new full configuration")).
|
||||
Consumes(mime.YAML, mime.JSON).Param(ws.BodyParameter("config", "The new full configuration")).
|
||||
Produces(mime.JSON).Writes(true).
|
||||
Doc("Upload a new current configuration, archiving the previous one"))
|
||||
|
||||
@ -307,3 +308,26 @@ func wsRender(resp *restful.Response, sslCfg *cfsslconfig.Config, tmplStr string
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
restful.RegisterEntityAccessor(mime.YAML, yamlEntityAccessor{})
|
||||
}
|
||||
|
||||
type yamlEntityAccessor struct{}
|
||||
|
||||
var _ restful.EntityReaderWriter = yamlEntityAccessor{}
|
||||
|
||||
func (_ yamlEntityAccessor) Read(req *restful.Request, v any) error {
|
||||
decoder := yaml.NewDecoder(req.Request.Body)
|
||||
return decoder.Decode(v)
|
||||
}
|
||||
func (_ yamlEntityAccessor) Write(resp *restful.Response, status int, v any) error {
|
||||
if v == nil {
|
||||
resp.WriteHeader(status)
|
||||
// do not write a nil representation
|
||||
return nil
|
||||
}
|
||||
resp.Header().Set("Content-Type", mime.YAML)
|
||||
resp.WriteHeader(status)
|
||||
return yaml.NewEncoder(resp.ResponseWriter).Encode(v)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user