2017-05-23 18:56:10 +00:00
|
|
|
package handler
|
2016-12-18 05:21:29 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2016-12-25 20:34:02 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2017-06-01 08:14:55 +00:00
|
|
|
"github.com/portainer/portainer"
|
2017-05-23 18:56:10 +00:00
|
|
|
httperror "github.com/portainer/portainer/http/error"
|
|
|
|
"github.com/portainer/portainer/http/security"
|
2016-12-18 05:21:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// TemplatesHandler represents an HTTP API handler for managing templates.
|
|
|
|
type TemplatesHandler struct {
|
|
|
|
*mux.Router
|
2017-06-01 08:14:55 +00:00
|
|
|
Logger *log.Logger
|
|
|
|
SettingsService portainer.SettingsService
|
2016-12-18 05:21:29 +00:00
|
|
|
}
|
|
|
|
|
2017-04-05 08:13:32 +00:00
|
|
|
const (
|
2017-08-01 09:24:44 +00:00
|
|
|
containerTemplatesURLLinuxServerIo = "https://tools.linuxserver.io/portainer.json"
|
2017-04-05 08:13:32 +00:00
|
|
|
)
|
|
|
|
|
2016-12-18 05:21:29 +00:00
|
|
|
// NewTemplatesHandler returns a new instance of TemplatesHandler.
|
2017-06-01 08:14:55 +00:00
|
|
|
func NewTemplatesHandler(bouncer *security.RequestBouncer) *TemplatesHandler {
|
2016-12-18 05:21:29 +00:00
|
|
|
h := &TemplatesHandler{
|
2017-06-01 08:14:55 +00:00
|
|
|
Router: mux.NewRouter(),
|
|
|
|
Logger: log.New(os.Stderr, "", log.LstdFlags),
|
2016-12-18 05:21:29 +00:00
|
|
|
}
|
2017-03-12 16:24:15 +00:00
|
|
|
h.Handle("/templates",
|
2017-08-13 14:45:55 +00:00
|
|
|
bouncer.AuthenticatedAccess(http.HandlerFunc(h.handleGetTemplates))).Methods(http.MethodGet)
|
2016-12-18 05:21:29 +00:00
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
2017-04-05 08:13:32 +00:00
|
|
|
// handleGetTemplates handles GET requests on /templates?key=<key>
|
2016-12-18 05:21:29 +00:00
|
|
|
func (handler *TemplatesHandler) handleGetTemplates(w http.ResponseWriter, r *http.Request) {
|
2017-04-05 08:13:32 +00:00
|
|
|
key := r.FormValue("key")
|
|
|
|
if key == "" {
|
2017-05-23 18:56:10 +00:00
|
|
|
httperror.WriteErrorResponse(w, ErrInvalidQueryFormat, http.StatusBadRequest, handler.Logger)
|
2017-04-05 08:13:32 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var templatesURL string
|
|
|
|
if key == "containers" {
|
2017-06-01 08:14:55 +00:00
|
|
|
settings, err := handler.SettingsService.Settings()
|
|
|
|
if err != nil {
|
|
|
|
httperror.WriteErrorResponse(w, err, http.StatusInternalServerError, handler.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
templatesURL = settings.TemplatesURL
|
2017-04-05 08:13:32 +00:00
|
|
|
} else if key == "linuxserver.io" {
|
|
|
|
templatesURL = containerTemplatesURLLinuxServerIo
|
|
|
|
} else {
|
2017-05-23 18:56:10 +00:00
|
|
|
httperror.WriteErrorResponse(w, ErrInvalidQueryFormat, http.StatusBadRequest, handler.Logger)
|
2017-04-05 08:13:32 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := http.Get(templatesURL)
|
2016-12-18 05:21:29 +00:00
|
|
|
if err != nil {
|
2017-05-23 18:56:10 +00:00
|
|
|
httperror.WriteErrorResponse(w, err, http.StatusInternalServerError, handler.Logger)
|
2016-12-18 05:21:29 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2017-05-23 18:56:10 +00:00
|
|
|
httperror.WriteErrorResponse(w, err, http.StatusInternalServerError, handler.Logger)
|
2016-12-18 05:21:29 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Write(body)
|
|
|
|
}
|