func main() { flagSet := flag.CommandLine flags := parseFlags(flagSet) log.Printf("Flags: %+v", flags) oinker := &Oinker{} if *flags.cassandraAddr != "" { oinker.CQLHosts = []string{*flags.cassandraAddr} } else if *flags.cassandraSRV != "" && *flags.cassandraDNS == "" || *flags.cassandraSRV == "" && *flags.cassandraDNS != "" { log.Fatalf("Invalid input: cassandra-srv and cassandra-dns must both be specified to enable cassandra usage") } else if *flags.cassandraSRV != "" && *flags.cassandraDNS != "" { hosts, err := lookupCassandraHosts(*flags.cassandraSRV, *flags.cassandraDNS) if err != nil { log.Fatalf("Error looking up Cassandra SRV records: %s", err) } oinker.CQLHosts = hosts } graph := oinker.NewGraph() var oinkRepo model.OinkRepo inject.ExtractAssignable(graph, &oinkRepo) err := oinkRepo.Init() if err != nil { log.Fatalf("Error Initializing Repo: %s", err) } var mux controller.MuxServer inject.ExtractAssignable(graph, &mux) var controllers []controller.Controller inject.FindAssignable(graph, &controllers) for _, c := range controllers { log.Println("Registering controller:", c.Name()) c.RegisterHandlers(mux) } // serve and listen for shutdown signals gracehttp.Serve( &http.Server{Addr: *flags.address, Handler: mux}, ) }
func main() { flagSet := flag.CommandLine flags := parseFlags(flagSet) log.Infof("Flags: %+v", flags) oinker := &Oinker{} if *flags.cassandraAddr != "" { oinker.CQLHosts = []string{*flags.cassandraAddr} oinker.CQLReplicationFactor = *flags.cassandraRepl } graph := oinker.NewGraph() defer graph.Finalize() // initialize cassandra (connection, keyspace, tables) var oinkRepo service.OinkRepo inject.ExtractAssignable(graph, &oinkRepo) svc, ok := oinkRepo.(inject.Initializable) if ok { svc.Initialize() } var mux controller.MuxServer inject.ExtractAssignable(graph, &mux) var controllers []controller.Controller inject.FindAssignable(graph, &controllers) for _, c := range controllers { log.Infof("Registering controller:", c.Name()) c.RegisterHandlers(mux) } // serve and listen for shutdown signals gracehttp.Serve( &http.Server{Addr: *flags.address, Handler: mux}, ) }
func TestExtractAssignableNoMatch(t *testing.T) { RegisterTestingT(t) graph := inject.NewGraph() var ( g1 *gamma ) graph.Define(&g1, inject.NewProvider(func() *gamma { return &gamma{name: "g1"} })) var o omega defer ExpectPanic("no defined pointer is assignable to the specified type") inject.ExtractAssignable(graph, &o) }
func TestExtractAssignable(t *testing.T) { RegisterTestingT(t) graph := inject.NewGraph() var ( a1 *alpha g1 *gamma ) graph.Define(&a1, inject.NewProvider(func() *alpha { return &alpha{name: "a1"} })) graph.Define(&g1, inject.NewProvider(func() *gamma { return &gamma{name: "g1"} })) var o omega inject.ExtractAssignable(graph, &o) Expect(o).To(Equal(&alpha{name: "a1"})) }
func TestExtractAssignableMultiMatch(t *testing.T) { RegisterTestingT(t) graph := inject.NewGraph() var ( a1 *alpha b1 *beta g1 *gamma ) graph.Define(&a1, inject.NewProvider(func() *alpha { return &alpha{name: "a1"} })) graph.Define(&b1, inject.NewProvider(func() *beta { return &beta{name: "b1"} })) graph.Define(&g1, inject.NewProvider(func() *gamma { return &gamma{name: "g1"} })) var o omega defer ExpectPanic("more than one defined pointer is assignable to the specified type") inject.ExtractAssignable(graph, &o) }