forked from cloudfoundry/cli-plugin-repo
/
handles.go
45 lines (37 loc) · 970 Bytes
/
handles.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package server
import (
"encoding/json"
"io"
"net/http"
"github.com/cloudfoundry-incubator/cli-plugin-repo/models"
"github.com/cloudfoundry-incubator/cli-plugin-repo/parser"
)
type ServerHandles interface {
ListPlugins(w http.ResponseWriter, r *http.Request)
}
type handlers struct {
yamlParser parser.YamlParser
logger io.Writer
}
func NewServerHandles(yamlParser parser.YamlParser, logger io.Writer) ServerHandles {
return &handlers{
yamlParser: yamlParser,
logger: logger,
}
}
func (h *handlers) ListPlugins(w http.ResponseWriter, r *http.Request) {
pluginModel, err := h.yamlParser.Parse()
if err != nil {
h.logger.Write([]byte("Error parsing repo-index.yml: " + err.Error()))
return
}
b, err := json.Marshal(models.PluginsJson{
Plugins: pluginModel,
})
if err != nil {
h.logger.Write([]byte("Error marshalling plugin models: " + err.Error()))
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(b)
}