{"modulePath":"github.com/DoNewsCode/core","version":"v0.12.3","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package core\n\nPackage core is a service container that elegantly bootstrap and coordinate twelve-factor apps in Go.\n\nCheckout the README.md for an overview of this package.\n\n## Constants\n\n```go\nconst (\n\t// OnHTTPServerStart is an event triggered when the http server is ready to serve\n\t// traffic. At this point the module is already wired up. This event is useful to\n\t// register service to service discovery.\n\tOnHTTPServerStart event = \"onHTTPServerStart\"\n\n\t// OnHTTPServerShutdown is an event triggered when the http server is shutting down.\n\t// traffic. At this point The traffic can no longer reach the server, but the\n\t// database and other infrastructures are not closed yet. This event is useful\n\t// to unregister service to service discovery.\n\tOnHTTPServerShutdown event = \"onHTTPServerShutdown\"\n\n\t// OnGRPCServerStart is an event triggered when the grpc server is ready to serve\n\t// traffic. At this point the module is already wired up. This event is useful to\n\t// register service to service discovery.\n\tOnGRPCServerStart event = \"onGRPCServerStart\"\n\n\t// OnGRPCServerShutdown is an event triggered when the http server is shutting down.\n\t// traffic. At this point The traffic can no longer reach the server, but the\n\t// database and other infrastructures are not closed yet. This event is useful\n\t// to unregister service to service discovery.\n\tOnGRPCServerShutdown event = \"onGRPCServerShutdown\"\n)\n```\n\n## Functions\n\n```go\nfunc NewServeModule(in serveIn) serveModule\n```\n\n```go\nfunc ProvideAppName(conf contract.ConfigUnmarshaler) contract.AppName\n```\nProvideAppName is the default AppNameProvider for package Core.\n\n```go\nfunc ProvideConfig(configStack []config.ProviderSet, configWatcher contract.ConfigWatcher) contract.ConfigUnmarshaler\n```\nProvideConfig is the default ConfigProvider for package Core.\n\n```go\nfunc ProvideDi(conf contract.ConfigUnmarshaler) *dig.Container\n```\nProvideDi is the default DiProvider for package Core.\n\n```go\nfunc ProvideEnv(conf contract.ConfigUnmarshaler) contract.Env\n```\nProvideEnv is the default EnvProvider for package Core.\n\n```go\nfunc ProvideEventDispatcher(conf contract.ConfigUnmarshaler) contract.Dispatcher\n```\nProvideEventDispatcher is the default EventDispatcherProvider for package Core.\n\n```go\nfunc ProvideLogger(conf contract.ConfigUnmarshaler, appName contract.AppName, env contract.Env) log.Logger\n```\nProvideLogger is the default LoggerProvider for package Core.\n\n```go\nfunc WithYamlFile(path string) (CoreOption, CoreOption)\n```\nWithYamlFile is a two-in-one coreOption. It uses the configuration file as the source of configuration, and watches the change of that file for hot reloading.\n\n## Types\n\n```go\ntype AppNameProvider func(conf contract.ConfigUnmarshaler) contract.AppName\n```\nAppNameProvider provides the contract.AppName to the core.\n\n```go\ntype C struct {\n\tAppName contract.AppName\n\tEnv     contract.Env\n\tcontract.ConfigAccessor\n\tlogging.LevelLogger\n\t*container.Container\n\tcontract.Dispatcher\n\t// contains filtered or unexported fields\n}\n```\nC stands for the core of the application. It contains service definitions and dependencies. C means to be used in the boostrap phase of the application. Do not pass C into services and use it as a service locator.\n\n```go\nfunc Default(opts ...CoreOption) *C\n```\nDefault creates a core.C under its default state. Core dependencies are already provided, and the config module and serve module are bundled.\n\n```go\nfunc New(opts ...CoreOption) *C\n```\nNew creates a new bare-bones C.\n\n```go\nfunc (c *C) AddModule(module interface{})\n```\nAddModule adds a module to the core.\n\nA Module is a group of functionality. It must provide some runnable stuff: http handlers, grpc handlers, cron jobs, one-time command, etc.\n\nOptionally if the module embeds di.In, its fields will be injected by DI container. The semantics of injection follows the same rule of dig.Invoke. Note that the module added in this way will not retain any original field values, i.e. the module will only contain fields populated by DI container.\n\n```go\nfunc (c *C) AddModuleFunc(constructor interface{})\n```\nAddModuleFunc add the module after Invoking its constructor. Clean up functions and errors are handled automatically.\n\n```go\nfunc (c *C) ApplyRootCommand(command *cobra.Command)\n```\nApplyRootCommand iterates through every CommandProvider registered in the container, and introduce the root \\*cobra.Command to everyone.\n\n```go\nfunc (c *C) Invoke(function interface{})\n```\nInvoke runs the given function after instantiating its dependencies. Any arguments that the function has are treated as its dependencies. The dependencies are instantiated in an unspecified order along with any dependencies that they might have. The function may return an error to indicate failure. The error will be returned to the caller as-is.\n\nIt internally calls uber's dig library. Consult dig's documentation for details. ([https://pkg.go.dev/go.uber.org/dig](https://pkg.go.dev/go.uber.org/dig))\n\n```go\nfunc (c *C) Provide(deps di.Deps)\n```\nProvide adds dependencies provider to the core. Note the dependency provider must be a function in the form of:\n\n\tfunc(foo Foo) Bar\n\nwhere foo is the upstream dependency and Bar is the provided type. The order for providers doesn't matter. They are only executed lazily when the Invoke is called.\n\nThis method internally calls uber's dig library. Consult dig's documentation for details. ([https://pkg.go.dev/go.uber.org/dig](https://pkg.go.dev/go.uber.org/dig))\n\nThe difference is, core.Provide has been made to accommodate the convention from google/wire ([https://github.com/google/wire](https://github.com/google/wire)). All \"func()\" returned by constructor are treated as clean up functions. It also examines if the dependency implements the modular interface. If so, this dependency will be added the module collection.\n\n```go\nfunc (c *C) ProvideEssentials()\n```\nProvideEssentials adds the default core dependencies to the core.\n\n```go\nfunc (c *C) Serve(ctx context.Context) error\n```\nServe runs the serve command bundled in the core. For larger projects, consider use full-featured ServeModule instead of calling serve directly.\n\n```go\nfunc (c *C) Shutdown()\n```\nShutdown iterates through every CloserProvider registered in the container, and calls them in the reversed order of registration.\n\n```go\ntype CloserProvider interface {\n\tProvideCloser()\n}\n```\nCloserProvider provides a shutdown function that will be called when service exits.\n\n```go\ntype CommandProvider interface {\n\tProvideCommand(command *cobra.Command)\n}\n```\nCommandProvider provides cobra.Command.\n\n```go\ntype ConfParser interface {\n\tUnmarshal([]byte) (map[string]interface{}, error)\n\tMarshal(map[string]interface{}) ([]byte, error)\n}\n```\nConfParser models a parser for configuration. For example, yaml.Parser.\n\n```go\ntype ConfProvider interface {\n\tReadBytes() ([]byte, error)\n\tRead() (map[string]interface{}, error)\n}\n```\nConfProvider models a configuration provider. For example, file.Provider.\n\n```go\ntype ConfigProvider func(configStack []config.ProviderSet, configWatcher contract.ConfigWatcher) contract.ConfigUnmarshaler\n```\nConfigProvider provides contract.ConfigAccessor to the core.\n\n```go\ntype CoreOption func(*coreValues)\n```\nCoreOption is the option to modify core attribute.\n\n```go\nfunc SetAppNameProvider(provider AppNameProvider) CoreOption\n```\nSetAppNameProvider is a CoreOption to replaces the default AppNameProvider.\n\n```go\nfunc SetConfigProvider(provider ConfigProvider) CoreOption\n```\nSetConfigProvider is a CoreOption to replaces the default ConfigProvider.\n\n```go\nfunc SetDiProvider(provider DiProvider) CoreOption\n```\nSetDiProvider is a CoreOption to replaces the default DiContainer.\n\n```go\nfunc SetEnvProvider(provider EnvProvider) CoreOption\n```\nSetEnvProvider is a CoreOption to replaces the default EnvProvider.\n\n```go\nfunc SetEventDispatcherProvider(provider EventDispatcherProvider) CoreOption\n```\nSetEventDispatcherProvider is a CoreOption to replaces the default EventDispatcherProvider.\n\n```go\nfunc SetLoggerProvider(provider LoggerProvider) CoreOption\n```\nSetLoggerProvider is a CoreOption to replaces the default LoggerProvider.\n\n```go\nfunc WithConfigStack(provider ConfProvider, parser ConfParser) CoreOption\n```\nWithConfigStack is a CoreOption that defines a configuration layer. See package config for details.\n\n```go\nfunc WithConfigWatcher(watcher contract.ConfigWatcher) CoreOption\n```\nWithConfigWatcher is a CoreOption that adds a config watcher to the core (for hot reloading configs).\n\n```go\nfunc WithInline(key string, entry interface{}) CoreOption\n```\nWithInline is a CoreOption that creates a inline config in the configuration stack.\n\n```go\ntype CronProvider interface {\n\tProvideCron(cron *cron2.Cron)\n}\n```\nCronProvider provides cron jobs.\n\n```go\ntype DeprecatedCronProvider interface {\n\tProvideCron(crontab *cron.Cron)\n}\n```\nDeprecatedCronProvider provides cron jobs. Deprecated: CronProvider is deprecated. Use CronProvider instead\n\n```go\ntype DiProvider func(conf contract.ConfigUnmarshaler) *dig.Container\n```\nDiProvider provides the \\*dig.Container to the core.\n\n```go\ntype EnvProvider func(conf contract.ConfigUnmarshaler) contract.Env\n```\nEnvProvider provides the contract.Env to the core.\n\n```go\ntype EventDispatcherProvider func(conf contract.ConfigUnmarshaler) contract.Dispatcher\n```\nEventDispatcherProvider provides contract.Dispatcher to the core.\n\n```go\ntype GRPCProvider interface {\n\tProvideGRPC(server *grpc.Server)\n}\n```\nGRPCProvider provides gRPC services.\n\n```go\ntype HTTPProvider interface {\n\tProvideHTTP(router *mux.Router)\n}\n```\nHTTPProvider provides http services.\n\n```go\ntype HttpFunc func(router *mux.Router)\n```\nHttpFunc converts a function to a module provides http.\n\n```go\nfunc (h HttpFunc) ProvideHTTP(router *mux.Router)\n```\nProvideHTTP implements container.HTTPProvider\n\n```go\ntype LoggerProvider func(conf contract.ConfigUnmarshaler, appName contract.AppName, env contract.Env) log.Logger\n```\nLoggerProvider provides the log.Logger to the core.\n\n```go\ntype OnGRPCServerShutdownPayload struct {\n\tGRPCServer *grpc.Server\n\tListener   net.Listener\n}\n```\nOnGRPCServerShutdownPayload is the payload of OnGRPCServerShutdown\n\n```go\ntype OnGRPCServerStartPayload struct {\n\tGRPCServer *grpc.Server\n\tListener   net.Listener\n}\n```\nOnGRPCServerStartPayload is the payload of OnGRPCServerStart\n\n```go\ntype OnHTTPServerShutdownPayload struct {\n\tHTTPServer *http.Server\n\tListener   net.Listener\n}\n```\nOnHTTPServerShutdownPayload is the payload of OnHTTPServerShutdown\n\n```go\ntype OnHTTPServerStartPayload struct {\n\tHTTPServer *http.Server\n\tListener   net.Listener\n}\n```\nOnHTTPServerStartPayload is the payload of OnHTTPServerStart\n\n```go\ntype RunProvider interface {\n\tProvideRunGroup(group *run.Group)\n}\n```\nRunProvider provides a runnable actor. Use it to register any server-like actions. For example, kafka consumer can be started here.\n\n","path":"github.com/DoNewsCode/core","name":"core","synopsis":"Package core is a service container that elegantly bootstrap and coordinate twelve-factor apps in Go.","isRedistributable":true}
