Skip to content

Commit 26b4cc9

Browse files
authored
Merge pull request #23 from go-admin-team/dev
Dev
2 parents b0d098c + afcbe40 commit 26b4cc9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+2948
-1285
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@
1818
/.idea
1919

2020
/plugins/logger/zap/testdata/*
21+
go.sum

Diff for: README.md

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
# go-admin 项目公共代码库
1+
# go-admin-team 公共代码库
22

33
### 功能
4-
- [x] go-admin log组件
4+
- [x] log组件
55
- [x] 缓存(支持memory、redis)
66
- [x] 队列(支持memory、redis)
77
- [x] 日志写入writer
88
- [x] 日志插件logrus
99
- [x] 日志插件zap
10-
- [ ] 大文件分割写入
10+
- [x] 大文件分割写入
1111
---
12-

Diff for: config/config.go

+17-8
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@ import (
1212

1313
// Config is an interface abstraction for dynamic configuration
1414
type Config interface {
15-
// provide the reader.Values interface
15+
// Values provide the reader.Values interface
1616
reader.Values
1717
// Init the config
1818
Init(opts ...Option) error
1919
// Options in the config
2020
Options() Options
21-
// Stop the config loader/watcher
21+
// Close Stop the config loader/watcher
2222
Close() error
2323
// Load config sources
2424
Load(source ...source.Source) error
25-
// Force a source changeset sync
25+
// Sync Force a source changeset sync
2626
Sync() error
2727
// Watch a value for changes
2828
Watch(path ...string) (Watcher, error)
@@ -34,33 +34,42 @@ type Watcher interface {
3434
Stop() error
3535
}
3636

37+
// Entity 配置实体
38+
type Entity interface {
39+
OnChange()
40+
}
41+
42+
// Options 配置的参数
3743
type Options struct {
3844
Loader loader.Loader
3945
Reader reader.Reader
4046
Source []source.Source
4147

4248
// for alternative data
4349
Context context.Context
50+
51+
Entity Entity
4452
}
4553

54+
// Option 调用类型
4655
type Option func(o *Options)
4756

4857
var (
49-
// Default Config Manager
50-
DefaultConfig, _ = NewConfig()
58+
// DefaultConfig Default Config Manager
59+
DefaultConfig Config
5160
)
5261

5362
// NewConfig returns new config
5463
func NewConfig(opts ...Option) (Config, error) {
5564
return newConfig(opts...)
5665
}
5766

58-
// Return config as raw json
67+
// Bytes Return config as raw json
5968
func Bytes() []byte {
6069
return DefaultConfig.Bytes()
6170
}
6271

63-
// Return config as a map
72+
// Map Return config as a map
6473
func Map() map[string]interface{} {
6574
return DefaultConfig.Map()
6675
}
@@ -70,7 +79,7 @@ func Scan(v interface{}) error {
7079
return DefaultConfig.Scan(v)
7180
}
7281

73-
// Force a source changeset sync
82+
// Sync Force a source changeset sync
7483
func Sync() error {
7584
return DefaultConfig.Sync()
7685
}

Diff for: config/default.go

+13-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ type watcher struct {
3333
func newConfig(opts ...Option) (Config, error) {
3434
var c config
3535

36-
c.Init(opts...)
36+
err := c.Init(opts...)
37+
if err != nil {
38+
return nil, err
39+
}
3740
go c.run()
3841

3942
return &c, nil
@@ -67,6 +70,9 @@ func (c *config) Init(opts ...Option) error {
6770
if err != nil {
6871
return err
6972
}
73+
if c.opts.Entity != nil {
74+
_ = c.vals.Scan(c.opts.Entity)
75+
}
7076

7177
return nil
7278
}
@@ -96,6 +102,10 @@ func (c *config) run() {
96102

97103
// set values
98104
c.vals, _ = c.opts.Reader.Values(snap.ChangeSet)
105+
if c.opts.Entity != nil {
106+
_ = c.vals.Scan(c.opts.Entity)
107+
c.opts.Entity.OnChange()
108+
}
99109

100110
c.Unlock()
101111
}
@@ -116,7 +126,7 @@ func (c *config) run() {
116126
case <-done:
117127
case <-c.exit:
118128
}
119-
w.Stop()
129+
_ = w.Stop()
120130
}()
121131

122132
// block watch
@@ -149,7 +159,7 @@ func (c *config) Scan(v interface{}) error {
149159
return c.vals.Scan(v)
150160
}
151161

152-
// sync loads all the sources, calls the parser and updates the config
162+
// Sync sync loads all the sources, calls the parser and updates the config
153163
func (c *config) Sync() error {
154164
if err := c.opts.Loader.Sync(); err != nil {
155165
return err

Diff for: config/loader/loader.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,23 @@ import (
1010

1111
// Loader manages loading sources
1212
type Loader interface {
13-
// Stop the loader
13+
// Close Stop the loader
1414
Close() error
1515
// Load the sources
1616
Load(...source.Source) error
17-
// A Snapshot of loaded config
17+
// Snapshot A Snapshot of loaded config
1818
Snapshot() (*Snapshot, error)
19-
// Force sync of sources
19+
// Sync Force sync of sources
2020
Sync() error
2121
// Watch for changes
2222
Watch(...string) (Watcher, error)
23-
// Name of loader
23+
// String Name of loader
2424
String() string
2525
}
2626

2727
// Watcher lets you watch sources and returns a merged ChangeSet
2828
type Watcher interface {
29-
// First call to next may return the current Snapshot
29+
// Next First call to next may return the current Snapshot
3030
// If you are watching a path then only the data from
3131
// that path is returned.
3232
Next() (*Snapshot, error)
@@ -38,7 +38,7 @@ type Watcher interface {
3838
type Snapshot struct {
3939
// The merged ChangeSet
4040
ChangeSet *source.ChangeSet
41-
// Deterministic and comparable version of the snapshot
41+
// Version Deterministic and comparable version of the snapshot
4242
Version string
4343
}
4444

Diff for: config/loader/memory/memory.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func (m *memory) watch(idx int, s source.Source) {
9797
case <-done:
9898
case <-m.exit:
9999
}
100-
w.Stop()
100+
_ = w.Stop()
101101
}()
102102

103103
// block watch

Diff for: config/options.go

+7
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,10 @@ func WithReader(r reader.Reader) Option {
2626
o.Reader = r
2727
}
2828
}
29+
30+
// WithEntity sets the config Entity
31+
func WithEntity(e Entity) Option {
32+
return func(o *Options) {
33+
o.Entity = e
34+
}
35+
}

Diff for: config/reader/json/json.go

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"github.com/imdario/mergo"
1212
)
1313

14+
const readerTyp = "json"
15+
1416
type jsonReader struct {
1517
opts reader.Options
1618
json encoder.Encoder

Diff for: config/source/file/watcher.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ func newWatcher(f *file) (source.Watcher, error) {
2222
return nil, err
2323
}
2424

25-
fw.Add(f.path)
25+
err = fw.Add(f.path)
26+
if err != nil {
27+
return nil, err
28+
}
2629

2730
return &watcher{
2831
f: f,
@@ -46,7 +49,10 @@ func (w *watcher) Next() (*source.ChangeSet, error) {
4649
// check existence of file, and add watch again
4750
_, err := os.Stat(event.Name)
4851
if err == nil || os.IsExist(err) {
49-
w.fw.Add(event.Name)
52+
err := w.fw.Add(event.Name)
53+
if err != nil {
54+
return nil, err
55+
}
5056
}
5157
}
5258

Diff for: config/source/flag/flag.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func (fs *flagsrc) Read() (*source.ChangeSet, error) {
3535
tmp = map[string]interface{}{k: tmp}
3636
}
3737

38-
mergo.Map(&changes, tmp) // need to sort error handling
38+
_ = mergo.Map(&changes, tmp) // need to sort error handling
3939
return
4040
}
4141

Diff for: debug/log/log.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import (
88
)
99

1010
var (
11-
// Default buffer size if any
11+
// DefaultSize Default buffer size if any
1212
DefaultSize = 256
13-
// Default formatter
13+
// DefaultFormat Default formatter
1414
DefaultFormat = TextFormat
1515
)
1616

@@ -40,7 +40,7 @@ type Stream interface {
4040
Stop() error
4141
}
4242

43-
// Format is a function which formats the output
43+
// FormatFunc is a function which formats the output
4444
type FormatFunc func(Record) string
4545

4646
// TextFormat returns text format

0 commit comments

Comments
 (0)