portainer/api/http/templates_handler.go

51 lines
1.2 KiB
Go
Raw Normal View History

2016-12-18 05:21:29 +00:00
package http
import (
"io/ioutil"
"log"
"net/http"
"os"
"github.com/gorilla/mux"
2016-12-18 05:21:29 +00:00
)
// TemplatesHandler represents an HTTP API handler for managing templates.
type TemplatesHandler struct {
*mux.Router
Logger *log.Logger
templatesURL string
2016-12-18 05:21:29 +00:00
}
// NewTemplatesHandler returns a new instance of TemplatesHandler.
func NewTemplatesHandler(mw *middleWareService) *TemplatesHandler {
2016-12-18 05:21:29 +00:00
h := &TemplatesHandler{
Router: mux.NewRouter(),
Logger: log.New(os.Stderr, "", log.LstdFlags),
2016-12-18 05:21:29 +00:00
}
h.Handle("/templates",
mw.authenticated(http.HandlerFunc(h.handleGetTemplates)))
2016-12-18 05:21:29 +00:00
return h
}
// handleGetTemplates handles GET requests on /templates
func (handler *TemplatesHandler) handleGetTemplates(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
handleNotAllowed(w, []string{http.MethodGet})
2016-12-18 05:21:29 +00:00
return
}
resp, err := http.Get(handler.templatesURL)
if err != nil {
Error(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 {
Error(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)
}