示例#1
0
func main() {
	graph := inject.NewGraph()
	var (
		logger    log.Logger
		server    *app.Server
		a         *A
		b         *B
		mux       *http.ServeMux
		dconf     *distconf.Distconf
		authCheck *app.AuthorizationCheck
		tk        timekeeper.TimeKeeper
		auth      app.Authentication
	)

	graph.Define(&logger, inject.NewProvider(func() log.Logger {
		return log.DefaultLogger
	}))
	graph.Define(&tk, inject.NewProvider(func() timekeeper.RealTime {
		return timekeeper.RealTime{}
	}))
	graph.Define(&auth, inject.NewProvider(func() *app.StrictCheck {
		return &app.StrictCheck{
			Username: "******",
			Password: "******",
		}
	}))

	graph.Define(&dconf, inject.NewProvider(distconf.New, &[]distconf.Reader{distconf.Env()}))
	graph.Define(&mux, inject.NewProvider(http.NewServeMux))
	graph.Define(&authCheck, inject.NewAutoProvider(app.NewAuthorizationCheck))
	graph.Define(&a, inject.NewAutoProvider(NewA))
	graph.Define(&b, inject.NewAutoProvider(NewB))
	graph.Define(&server, inject.NewAutoProvider(app.NewServer))

	graph.Resolve(&logger)
	graph.Resolve(&server)
	graph.Resolve(&a)
	fmt.Println(a)
	logger.Log("Setting up")
	if err := server.Setup(); err != nil {
		logger.Log("Cannot setup!")
		panic(err)
	}
	err := server.Start(context.Background())
	fmt.Println(err)
}
示例#2
0
func main() {
	dconf := distconf.New([]distconf.Reader{distconf.Env()})

	var graph inject.Graph
	var server app.Server

	if err := graph.Provide(
		&inject.Object{
			Value: &timekeeper.RealTime{},
		},
		&inject.Object{
			Value: log.DefaultLogger,
		},
		&inject.Object{
			Value: http.NewServeMux(),
		},
		&inject.Object{
			Value: &app.StrictCheck{
				Username: "******",
				Password: "******",
			},
		},
		&inject.Object{
			Value: dconf,
		},
		&inject.Object{
			Value: &server,
		},
	); err != nil {
		panic(err)
	}
	if err := graph.Populate(); err != nil {
		panic(err)
	}

	logger := server.Logger
	logger.Log("Setting up")
	if err := server.Setup(); err != nil {
		logger.Log("Cannot setup!")
		panic(err)
	}
	err := server.Start(context.Background())
	fmt.Println(err)
}