Beispiel #1
0
// NewInjector creates a TestInjector
func NewInjector(method string, body string) *Injector {
	var i Injector
	i.r, _ = http.NewRequest(method, "http://localhost/v1/", strings.NewReader(body))

	w := httptest.NewRecorder()

	enc := negotiator.JsonEncoder{PrettyPrint: false}
	cn := negotiator.NewContentNegotiator(enc, w)
	cn.AddEncoder(negotiator.MimeJSON, enc)

	i.Injector = inject.New()
	i.Injector.Map(i.r)
	i.Injector.MapTo(w, (*http.ResponseWriter)(nil))
	i.Injector.MapTo(cn, (*negotiator.Negotiator)(nil))

	return &i
}
Beispiel #2
0
func main() {
	consulAddress := mustGetEnvVar("CONSUL_HTTP_ADDR")
	environment := mustGetEnvVar("ENVIRONMENT")
	serviceName := mustGetEnvVar("SERVICE_NAME")

	conf, _ := setupConfig(serviceName, environment, consulAddress)
	//pull config and pass it around
	conf.MustGetDuration(configKeyBalancerTTL)

	// use loadbalancer to lookup services and db locations if necessary
	// Each request should repeat the lookup to make sure that this app
	// follows any services that move
	// s, err := loadbalancer.FindService("foo")

	//setup martini
	m := martini.New()
	// Setup middleware
	m.Use(martini.Recovery())
	m.Use(martini.Logger())

	// Setup routes
	router := martini.NewRouter()
	router.Group("/v1", func(v1router martini.Router) {
		//Setup v1 routes
		v1router.Group("/test", func(r martini.Router) {
			r.Get("/ping", test.Ping())
		})
	})

	// Add the router action
	m.Action(router.Handle)

	// Inject dependencies
	m.Use(func(c martini.Context, w http.ResponseWriter) {
		enc := negotiator.JsonEncoder{PrettyPrint: false}
		cn := negotiator.NewContentNegotiator(enc, w)
		cn.AddEncoder(negotiator.MimeJSON, enc)
		c.MapTo(cn, (*negotiator.Negotiator)(nil))
	})

	// Start up the server
	m.RunOnAddr(":8080")
}