Skip to content

Commit

Permalink
Merge pull request #23 from go-admin-team/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
wenjianzhang authored Jul 4, 2021
2 parents b0d098c + afcbe40 commit 26b4cc9
Show file tree
Hide file tree
Showing 72 changed files with 2,948 additions and 1,285 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@
/.idea

/plugins/logger/zap/testdata/*
go.sum
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
# go-admin 项目公共代码库
# go-admin-team 公共代码库

### 功能
- [x] go-admin log组件
- [x] log组件
- [x] 缓存(支持memory、redis)
- [x] 队列(支持memory、redis)
- [x] 日志写入writer
- [x] 日志插件logrus
- [x] 日志插件zap
- [ ] 大文件分割写入
- [x] 大文件分割写入
---

25 changes: 17 additions & 8 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ import (

// Config is an interface abstraction for dynamic configuration
type Config interface {
// provide the reader.Values interface
// Values provide the reader.Values interface
reader.Values
// Init the config
Init(opts ...Option) error
// Options in the config
Options() Options
// Stop the config loader/watcher
// Close Stop the config loader/watcher
Close() error
// Load config sources
Load(source ...source.Source) error
// Force a source changeset sync
// Sync Force a source changeset sync
Sync() error
// Watch a value for changes
Watch(path ...string) (Watcher, error)
Expand All @@ -34,33 +34,42 @@ type Watcher interface {
Stop() error
}

// Entity 配置实体
type Entity interface {
OnChange()
}

// Options 配置的参数
type Options struct {
Loader loader.Loader
Reader reader.Reader
Source []source.Source

// for alternative data
Context context.Context

Entity Entity
}

// Option 调用类型
type Option func(o *Options)

var (
// Default Config Manager
DefaultConfig, _ = NewConfig()
// DefaultConfig Default Config Manager
DefaultConfig Config
)

// NewConfig returns new config
func NewConfig(opts ...Option) (Config, error) {
return newConfig(opts...)
}

// Return config as raw json
// Bytes Return config as raw json
func Bytes() []byte {
return DefaultConfig.Bytes()
}

// Return config as a map
// Map Return config as a map
func Map() map[string]interface{} {
return DefaultConfig.Map()
}
Expand All @@ -70,7 +79,7 @@ func Scan(v interface{}) error {
return DefaultConfig.Scan(v)
}

// Force a source changeset sync
// Sync Force a source changeset sync
func Sync() error {
return DefaultConfig.Sync()
}
Expand Down
16 changes: 13 additions & 3 deletions config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ type watcher struct {
func newConfig(opts ...Option) (Config, error) {
var c config

c.Init(opts...)
err := c.Init(opts...)
if err != nil {
return nil, err
}
go c.run()

return &c, nil
Expand Down Expand Up @@ -67,6 +70,9 @@ func (c *config) Init(opts ...Option) error {
if err != nil {
return err
}
if c.opts.Entity != nil {
_ = c.vals.Scan(c.opts.Entity)
}

return nil
}
Expand Down Expand Up @@ -96,6 +102,10 @@ func (c *config) run() {

// set values
c.vals, _ = c.opts.Reader.Values(snap.ChangeSet)
if c.opts.Entity != nil {
_ = c.vals.Scan(c.opts.Entity)
c.opts.Entity.OnChange()
}

c.Unlock()
}
Expand All @@ -116,7 +126,7 @@ func (c *config) run() {
case <-done:
case <-c.exit:
}
w.Stop()
_ = w.Stop()
}()

// block watch
Expand Down Expand Up @@ -149,7 +159,7 @@ func (c *config) Scan(v interface{}) error {
return c.vals.Scan(v)
}

// sync loads all the sources, calls the parser and updates the config
// Sync sync loads all the sources, calls the parser and updates the config
func (c *config) Sync() error {
if err := c.opts.Loader.Sync(); err != nil {
return err
Expand Down
12 changes: 6 additions & 6 deletions config/loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ import (

// Loader manages loading sources
type Loader interface {
// Stop the loader
// Close Stop the loader
Close() error
// Load the sources
Load(...source.Source) error
// A Snapshot of loaded config
// Snapshot A Snapshot of loaded config
Snapshot() (*Snapshot, error)
// Force sync of sources
// Sync Force sync of sources
Sync() error
// Watch for changes
Watch(...string) (Watcher, error)
// Name of loader
// String Name of loader
String() string
}

// Watcher lets you watch sources and returns a merged ChangeSet
type Watcher interface {
// First call to next may return the current Snapshot
// Next First call to next may return the current Snapshot
// If you are watching a path then only the data from
// that path is returned.
Next() (*Snapshot, error)
Expand All @@ -38,7 +38,7 @@ type Watcher interface {
type Snapshot struct {
// The merged ChangeSet
ChangeSet *source.ChangeSet
// Deterministic and comparable version of the snapshot
// Version Deterministic and comparable version of the snapshot
Version string
}

Expand Down
2 changes: 1 addition & 1 deletion config/loader/memory/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (m *memory) watch(idx int, s source.Source) {
case <-done:
case <-m.exit:
}
w.Stop()
_ = w.Stop()
}()

// block watch
Expand Down
7 changes: 7 additions & 0 deletions config/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,10 @@ func WithReader(r reader.Reader) Option {
o.Reader = r
}
}

// WithEntity sets the config Entity
func WithEntity(e Entity) Option {
return func(o *Options) {
o.Entity = e
}
}
2 changes: 2 additions & 0 deletions config/reader/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"github.com/imdario/mergo"
)

const readerTyp = "json"

type jsonReader struct {
opts reader.Options
json encoder.Encoder
Expand Down
10 changes: 8 additions & 2 deletions config/source/file/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ func newWatcher(f *file) (source.Watcher, error) {
return nil, err
}

fw.Add(f.path)
err = fw.Add(f.path)
if err != nil {
return nil, err
}

return &watcher{
f: f,
Expand All @@ -46,7 +49,10 @@ func (w *watcher) Next() (*source.ChangeSet, error) {
// check existence of file, and add watch again
_, err := os.Stat(event.Name)
if err == nil || os.IsExist(err) {
w.fw.Add(event.Name)
err := w.fw.Add(event.Name)
if err != nil {
return nil, err
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion config/source/flag/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (fs *flagsrc) Read() (*source.ChangeSet, error) {
tmp = map[string]interface{}{k: tmp}
}

mergo.Map(&changes, tmp) // need to sort error handling
_ = mergo.Map(&changes, tmp) // need to sort error handling
return
}

Expand Down
6 changes: 3 additions & 3 deletions debug/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
)

var (
// Default buffer size if any
// DefaultSize Default buffer size if any
DefaultSize = 256
// Default formatter
// DefaultFormat Default formatter
DefaultFormat = TextFormat
)

Expand Down Expand Up @@ -40,7 +40,7 @@ type Stream interface {
Stop() error
}

// Format is a function which formats the output
// FormatFunc is a function which formats the output
type FormatFunc func(Record) string

// TextFormat returns text format
Expand Down
Loading

0 comments on commit 26b4cc9

Please sign in to comment.