/
server.go
43 lines (33 loc) · 1.03 KB
/
server.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
package main
import (
"net/http"
"github.com/cloudfoundry-community/go-cfenv"
"github.com/cloudnativego/cf-tools"
"github.com/codegangsta/negroni"
"github.com/gorilla/mux"
"github.com/unrolled/render"
)
//NewServer configures and returns a Server.
func NewServer(appEnv *cfenv.App) *negroni.Negroni {
formatter := render.New(render.Options{
IndentJSON: true,
})
n := negroni.Classic()
mx := mux.NewRouter()
initRoutes(mx, formatter, appEnv)
n.UseHandler(mx)
return n
}
func initRoutes(mx *mux.Router, formatter *render.Render, appEnv *cfenv.App) {
mx.HandleFunc("/vcap", vcapHandler(formatter, appEnv)).Methods("GET")
}
func vcapHandler(formatter *render.Render, appEnv *cfenv.App) http.HandlerFunc {
val, err := cftools.GetVCAPServiceProperty("beer-service", "target-url", appEnv)
return func(w http.ResponseWriter, req *http.Request) {
if err != nil {
formatter.JSON(w, http.StatusInternalServerError, struct{ Error string }{err.Error()})
return
}
formatter.JSON(w, http.StatusOK, struct{ Foo string }{val})
}
}