forked from cloudfoundry/cli-plugin-repo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
clipr.go
64 lines (50 loc) · 1.44 KB
/
clipr.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"sort"
"github.com/cloudfoundry-incubator/cli-plugin-repo/web"
"github.com/tedsuo/rata"
"github.com/unrolled/secure"
"gopkg.in/yaml.v2"
)
type CLIPR struct {
Port int `short:"p" long:"port" default:"8080" description:"Port that the plugin repo listens on"`
RepoIndexPath string `short:"f" long:"filepath" default:"repo-index.yml" description:"Path to repo-index file"`
ForceSSL bool `long:"force-ssl" description:"Force SSL on every request"`
}
func (cmd *CLIPR) Execute(args []string) error {
logger := os.Stdout //turn this into a logger soon
var plugins web.PluginsJson
b, err := ioutil.ReadFile(cmd.RepoIndexPath)
if err != nil {
return err
}
err = yaml.Unmarshal(b, &plugins)
if err != nil {
return err
}
sort.Sort(plugins)
handlers := map[string]http.Handler{
Index: http.FileServer(http.Dir("ui")),
List: web.NewListPluginsHandler(plugins, logger),
UI: http.RedirectHandler("/", http.StatusMovedPermanently),
}
router, err := rata.NewRouter(Routes, handlers)
if err != nil {
return err
}
if cmd.ForceSSL {
secureMiddleware := secure.New(secure.Options{
SSLRedirect: true,
SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "https"},
})
router = secureMiddleware.Handler(router)
}
return http.ListenAndServe(cmd.bindAddr(), router)
}
func (cmd *CLIPR) bindAddr() string {
return fmt.Sprintf(":%d", cmd.Port)
}