2016-12-18 05:21:29 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2016-12-25 20:34:02 +00:00
|
|
|
|
|
|
|
"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
|
2017-03-12 16:24:15 +00:00
|
|
|
Logger *log.Logger
|
|
|
|
templatesURL string
|
2016-12-18 05:21:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewTemplatesHandler returns a new instance of TemplatesHandler.
|
2017-03-12 16:24:15 +00:00
|
|
|
func NewTemplatesHandler(mw *middleWareService) *TemplatesHandler {
|
2016-12-18 05:21:29 +00:00
|
|
|
h := &TemplatesHandler{
|
2017-03-12 16:24:15 +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",
|
|
|
|
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) {
|
2016-12-25 20:34:02 +00:00
|
|
|
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 {
|
2017-01-24 03:35:48 +00:00
|
|
|
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 {
|
2017-01-24 03:35:48 +00:00
|
|
|
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)
|
|
|
|
}
|