Commit 0a702c99 authored by vicotor's avatar vicotor

update

parent 6070eaa2
......@@ -197,7 +197,7 @@ func (c *CaddyAPI) CreateWebsite(domain string, resource string) error {
// send put request to the caddy api.
path := fmt.Sprintf("%s/config/apps/http/servers/srv0/routes/%d", c.Url, 0)
log.WithField("payload", payload).Debug("sending payload to caddy")
if err := c.put(path, []byte(payload)); err != nil {
if err := c.post(path, []byte(payload)); err != nil {
return err
}
success = true
......@@ -224,13 +224,45 @@ func (c *CaddyAPI) ForwardWebsite(param types.ForwardWebsite) error {
// send put request to the caddy api.
path := fmt.Sprintf("%s/config/apps/http/servers/srv0/routes/%d", c.Url, 0)
log.WithField("payload", payload).Debug("sending payload to caddy")
if err := c.put(path, []byte(payload)); err != nil {
if err := c.post(path, []byte(payload)); err != nil {
return err
}
return nil
}
func (c *CaddyAPI) post(path string, data []byte) error {
// send a get request to the Caddy API.
req, err := http.NewRequest("POST", path, bytes.NewBuffer(data))
if err != nil {
log.WithError(err).Error("failed to create request")
return err
}
req.Header.Set("Content-Type", "application/json")
resp, err := c.client.Do(req)
if err != nil {
log.WithError(err).Error("failed to send request")
return err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.WithError(err).Error("failed to read response body")
return err
}
if resp.StatusCode != http.StatusOK {
log.WithFields(log.Fields{
"status": resp.StatusCode,
"message": string(body),
}).Error("failed to post data")
return fmt.Errorf("failed to post data")
}
return nil
}
func (c *CaddyAPI) put(path string, data []byte) error {
// send a get request to the Caddy API.
req, err := http.NewRequest("PUT", path, bytes.NewBuffer(data))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment