1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-07-19 05:19:39 +02:00
portainer/api/http/handler/templates/utils_fetch_templates.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

51 lines
1.4 KiB
Go
Raw Normal View History

package templates
import (
"net/http"
portainer "github.com/portainer/portainer/api"
libclient "github.com/portainer/portainer/pkg/libhttp/client"
httperror "github.com/portainer/portainer/pkg/libhttp/error"
"github.com/rs/zerolog/log"
"github.com/segmentio/encoding/json"
)
type listResponse struct {
Version string `json:"version"`
Templates []portainer.Template `json:"templates"`
}
func (handler *Handler) fetchTemplates() (*listResponse, *httperror.HandlerError) {
settings, err := handler.DataStore.Settings().Settings()
if err != nil {
return nil, httperror.InternalServerError("Unable to retrieve settings from the database", err)
}
templatesURL := settings.TemplatesURL
if templatesURL == "" {
templatesURL = portainer.DefaultTemplatesURL
}
var body *listResponse
if err := libclient.ExternalRequestDisabled(templatesURL); err != nil {
if templatesURL == portainer.DefaultTemplatesURL {
log.Debug().Err(err).Msg("External request disabled: Default templates")
return body, nil
}
}
resp, err := http.Get(templatesURL)
if err != nil {
return nil, httperror.InternalServerError("Unable to retrieve templates via the network", err)
}
defer resp.Body.Close()
err = json.NewDecoder(resp.Body).Decode(&body)
if err != nil {
return nil, httperror.InternalServerError("Unable to parse template file", err)
}
return body, nil
}