file

package
v2.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 4, 2023 License: Apache-2.0 Imports: 4 Imported by: 0

README

配置中心-file文件配置

1.功能
  • 支持本地配置文件或目录
  • 支持配置的读取、修改、热更新
  • 支持读写模式设置
  • 支持json、yaml、toml、xml等多种配置格式
  • 不支持集群内热更新
2.快速开始

1.etc配置项

# 配置中心
[config]
    # 文件配置
    [config.file]
        # 配置文件或配置目录路径
        path = "./config"
        # 读写模式。可选:read-only | read-write,默认为read-only
        mode = "read-write"

2.开始使用

package main

import (
	"context"
	"github.com/cnsync/simple-game/v2/config"
	"github.com/cnsync/simple-game/v2/config/file"
	"github.com/cnsync/simple-game/v2/log"
	"time"
)

func main() {
	ctx := context.Background()
	filepath := "config.toml"

	// 设置全局配置器
	config.SetDefaultConfigurator()

	// 更新配置
	if err := config.Store(ctx, file.Name, filepath, map[string]interface{}{
		"timezone": "Local",
	}); err != nil {
		log.Errorf("store config failed: %v", err)
		return
	}

	time.Sleep(time.Millisecond)

	// 读取配置
	timezone := config.Get("config.timezone", "UTC").String()
	log.Infof("timezone: %s", timezone)

	// 更新配置
	if err := config.Store(ctx, file.Name, filepath, map[string]interface{}{
		"timezone": "UTC",
	}); err != nil {
		log.Errorf("store config failed: %v", err)
		return
	}

	time.Sleep(time.Millisecond)

	// 读取配置
	timezone = config.Get("config.timezone", "UTC").String()
	log.Infof("timezone: %s", timezone)
}

Documentation

Index

Constants

View Source
const Name = core.Name

Variables

This section is empty.

Functions

func NewSource

func NewSource(opts ...Option) configurator.Source

Types

type Option

type Option func(o *options)

func WithMode

func WithMode(mode string) Option

WithMode 设置读写模式

func WithPath

func WithPath(path string) Option

WithPath 设置配置文件或配置目录路径

type Source

type Source struct {
	// contains filtered or unexported fields
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL