Configmanager
No description available
Install / Use
/learn @cloudwego/ConfigmanagerREADME
Config Manager
Introduction
The configmanager is a package that provides a robust and efficient way to manage and
handle configuration data in your application. It's designed to load configurations
periodically from arbitrary sources, such as a file, a database, or a web service. The
module also provides monitoring on configuration changes and allows instant callback of
a registered listener.
Features
- Refreshes configuration data at regular intervals specified
- Provides methods for accessing configuration data
- Supports configuration change listeners for efficient handling of updates
- Includes a default FileProvider for loading configurations from a file
- Allows for customizable options and arbitrary sources
Overview
ConfigManager is designed with a 4-level hierarchy:
- ConfigManager:
- Holds a
Providerfor loading of all configurations - Compares two versions of config and notify registered listeners if there's any difference
- Provides APIs to reload config, retrieve certain config value/item, dump to files, etc.
- Holds a
- Provider:
- Responsible for loading configurations from desired source(s)
- Returns all configs in the form of
map[string]iface.ConfigValueto the calling ConfigManager
- ConfigValue:
- Holding a set of
iface.ConfigValueItem- The builtin
ConfigValueImplis designed with amap[iface.ItemType]iface.ConfigValueItem
- The builtin
- Provides APIs to retrieve certain config item
- Holding a set of
- ConfigValueItem:
- Holds specific configurations according to requirement
Usage
Quick Start
You can go directly to the
example/main.gofor a runnable example of the following steps.
configmanager ships with a FileProvider which loads from a config file in JSON format.
We'll begin with a JSON config file below (save it as config.json):
{
"test1": {
"item-max-retry": 1,
"item-max-limit": 2,
"item-service-name": "service_name"
}
}
Import the package into your application.
import (
"github.com/cloudwego/configmanager"
"github.com/cloudwego/configmanager/configvalue/items"
"github.com/cloudwego/configmanager/fileprovider"
"github.com/cloudwego/configmanager/iface
)
Initialize a FileProvider
// define your own item type for builtin items
const TypeItemMaxRetry iface.ItemType = "item-max-retry"
const TypeItemMaxLimit iface.ItemType = "item-max-limit"
const TypeItemServiceName iface.ItemType = "item-service-name"
itemFactory := fileprovider.NewItemFactory(map[iface.ItemType]iface.ItemInitializer{
TypeItemMaxRetry: items.NewItemInt64,
TypeItemMaxLimit: items.NewItemInt64, // an item can be used with multiple item types
TypeItemServiceName: items.NewItemString,
// you can register your own ConfigValueItem(s) here
})
provider := fileprovider.NewFileProvider(
fileprovider.WithFileProviderItemFactory(itemFactory),
fileprovider.WithFileProviderPath("config.json"),
)
Create a new instance of ConfigManager with the desired options.
manager := configmanager.NewConfigManager([]configmanager.ConfigManagerOption{
configmanager.WithRefreshInterval(10 * time.Second),
configmanager.WithConfigProvider(provider),
)
Register configuration change listeners as needed:
manager.RegisterConfigChangeListener("unique-id-for-each-listener", func(change iface.ConfigChange) {
// update something according to the change
log.Println("Change:", string(util.MustJsonMarshal(change)))
})
Refresh the configuration data manually if necessary.
// send a refresh signal and return immediately:
manager.Refresh()
// or if you need to wait until the loading completes:
manager.RefreshAndWait()
Access and retrieve configuration using the provided methods:
// define your iface.ConfigKey with a ToString() method
configKey := "SomeConfigKey"
// it's recommended to retrieve the ConfigValueItem directly:
maxRetryItem, err := manager.GetConfigItem(configKey, TypeItemMaxRetry)
if err == nil {
maxRetry := maxRetryItem.(*items.ItemInt64).Value()
...
}
// you can also retrieve the ConfigValueImpl for other purposes
configValue, err := manager.GetConfig(configKey)
if err == nil {
maxRetryLimit, err := configValue.GetItem(TypeItemMaxLimit)
// or call GetItemOrDefault() with a default item value
serviceName := configValue.GetItemOrDefault(TypeItemServiceName, items.CopyDefaultItemString())
}
Export the configuration data to a file using the Dump method.
err := manager.Dump("local_dump.json")
Customization
ConfigValueItems
Basic items like ItemBool, ItemInt64, ItemPair, ItemString is shipped with this package. You can define your own ItemType for simple configuration only holding basic go type values with these items.
If you need items more complicated, just create your own items implementing the iface.ConfigValueItem.
It might be easier to copy from items.ItemPair and make modifications.
ConfigValue
The built-in ConfigValueImpl mainly focuses on its extensibility, which allows for extending of new
types of ConfigValueItem without modifying ConfigValueImpl and FileProvider, at the cost of increasing
its complexity (harder to comprehend).
If you wish, you can write your own implementation of iface.ConfigValue. It's not recommended, because
it's not compatible with the builtin FileProvider.
Provider
FileProvider
Available Options are:
- WithFileProviderPath
- The specified file will be used to load the configuration data.
- WithFileProviderItemFactory
- The ItemFactory is responsible for creating new instances of ConfigValueItem when loading data from the file.
- WithFileProviderLogger
- This option sets a custom logger implementation for the FileProvider.
- By default, the logger uses log.Printf.
Provider for other sources
You can implement your own Provider to load config from other sources, as long as it implements the
iface.ConfigProvider.
It's recommended to use ConfigValueImpl as the value in the returned config map, thus you can easily
switch your provider to FileProvider.
ConfigManager
Available Options are:
- WithRefreshInterval
- This option sets the interval for refreshing the configuration data.
- If not set, the interval defaults to 10 seconds.
- WithConfigProvider
- This option sets the given ConfigProvider as the source for configuration data.
- WithErrorLogger
- This option sets a custom error logger for the ConfigManager instance.
- If not set, the logger defaults to log.Printf.
- WithConfigSerializer
- This option sets a custom ConfigSerializer for the ConfigManager.
- If not set, the serializer defaults to util.JSONSerializer.
Related Skills
diffs
341.8kUse the diffs tool to produce real, shareable diffs (viewer URL, file artifact, or both) instead of manual edit summaries.
clearshot
Structured screenshot analysis for UI implementation and critique. Analyzes every UI screenshot with a 5×5 spatial grid, full element inventory, and design system extraction — facts and taste together, every time. Escalates to full implementation blueprint when building. Trigger on any digital interface image file (png, jpg, gif, webp — websites, apps, dashboards, mockups, wireframes) or commands like 'analyse this screenshot,' 'rebuild this,' 'match this design,' 'clone this.' Skip for non-UI images (photos, memes, charts) unless the user explicitly wants to build a UI from them. Does NOT trigger on HTML source code, CSS, SVGs, or any code pasted as text.
openpencil
1.9kThe world's first open-source AI-native vector design tool and the first to feature concurrent Agent Teams. Design-as-Code. Turn prompts into UI directly on the live canvas. A modern alternative to Pencil.
ui-ux-designer
Use this agent when you need to design, implement, or improve user interface components and user experience flows. Examples include: creating new pages or components, improving existing UI layouts, implementing responsive designs, optimizing user interactions, building forms or dashboards, analyzing existing UI through browser snapshots, or when you need to ensure UI components follow design system standards and shadcn/ui best practices.\n\n<example>\nContext: User needs to create a new dashboard page for team management.\nuser: "I need to create a team management dashboard where users can view team members, invite new members, and manage roles"\nassistant: "I'll use the ui-ux-designer agent to design and implement this dashboard with proper UX considerations, using shadcn/ui components and our design system tokens."\n</example>\n\n<example>\nContext: User wants to improve the user experience of an existing form.\nuser: "The signup form feels clunky and users are dropping off. Can you improve it?"\nassistant: "Let me use the ui-ux-designer agent to analyze the current form UX and implement improvements using our design system and shadcn/ui components."\n</example>\n\n<example>\nContext: User wants to evaluate and improve existing UI.\nuser: "Can you take a look at our pricing page and see how we can make it more appealing and user-friendly?"\nassistant: "I'll use the ui-ux-designer agent to take a snapshot of the current pricing page, analyze the UX against Notion-inspired design principles, and implement improvements using our design tokens."\n</example>
