Skip to content

Commit

Permalink
✨ support local config file watch (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
lwnmengjing authored Mar 14, 2024
1 parent d07495e commit 4824ee1
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 17 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/casbin/gorm-adapter/v3 v3.20.0
github.com/casbin/mongodb-adapter/v3 v3.5.0
github.com/coreos/go-oidc/v3 v3.8.0
github.com/fsnotify/fsnotify v1.4.9
github.com/gin-contrib/pprof v1.4.0
github.com/go-openapi/spec v0.20.9
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ github.com/envoyproxy/protoc-gen-validate v1.0.2 h1:QkIBuU5k+x7/QXPvPPnWXWlCdaBF
github.com/envoyproxy/protoc-gen-validate v1.0.2/go.mod h1:GpiZQP3dDbg4JouG/NNS7QWXpgx6x8QiMKdmN72jogE=
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
github.com/gin-contrib/pprof v1.4.0 h1:XxiBSf5jWZ5i16lNOPbMTVdgHBdhfGRD5PZ1LWazzvg=
Expand Down Expand Up @@ -413,6 +415,7 @@ golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE=
golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down
18 changes: 6 additions & 12 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
)

// Init 初始化配置
func Init(cfg any, options ...source.Option) (err error) {
func Init(cfg source.Entity, options ...source.Option) (err error) {
opts := source.DefaultOptions()
for _, opt := range options {
opt(opts)
Expand All @@ -54,15 +54,6 @@ func Init(cfg any, options ...source.Option) (err error) {
f, err = sourceS3.New(options...)
case source.MGDB:
f, err = mgdb.New(options...)
//if err != nil {
// return err
//}
//var rb []byte
//rb, err = f.ReadFile(opts.Name)
//if err != nil {
// return err
//}
//return yaml.Unmarshal(rb, cfg)
case source.GORM:
f, err = gorm.New(options...)
}
Expand All @@ -82,7 +73,7 @@ func Init(cfg any, options ...source.Option) (err error) {
if err != nil {
return err
}
var unm func([]byte, interface{}) error
var unm func([]byte, any) error
switch f.GetExtend() {
case source.SchemeYaml, source.SchemeYml:
unm = yaml.Unmarshal
Expand All @@ -106,7 +97,10 @@ func Init(cfg any, options ...source.Option) (err error) {
slog.Error(err.Error())
}
}
return nil
if !opts.Watch {
return nil
}
return f.Watch(cfg, unm)
}

func getStage() string {
Expand Down
13 changes: 13 additions & 0 deletions pkg/config/source/entity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package source

/*
* @Author: lwnmengjing<[email protected]>
* @Date: 2024/3/14 23:47:41
* @Last Modified by: lwnmengjing<[email protected]>
* @Last Modified time: 2024/3/14 23:47:41
*/

// Entity 配置实体
type Entity interface {
OnChange()
}
4 changes: 4 additions & 0 deletions pkg/config/source/fs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ func (s *Source) ReadFile(name string) (rb []byte, err error) {
return nil, err
}

func (s *Source) Watch(_ source.Entity, _ func([]byte, any) error) error {
return nil
}

// GetExtend get extend
func (s *Source) GetExtend() source.Scheme {
return s.opt.Extend
Expand Down
4 changes: 4 additions & 0 deletions pkg/config/source/gorm/gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ func (s *Source) ReadFile(name string) ([]byte, error) {
return m.GenerateBytes()
}

func (s *Source) Watch(_ source.Entity, _ func([]byte, any) error) error {
return nil
}

// GetExtend get extend
func (s *Source) GetExtend() source.Scheme {
return s.opt.Extend
Expand Down
62 changes: 60 additions & 2 deletions pkg/config/source/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,19 @@ package local
import (
"fmt"
"io/fs"
"log/slog"
"os"
"path/filepath"

"github.com/fsnotify/fsnotify"

"github.com/mss-boot-io/mss-boot/pkg/config/source"
)

// Source is a local file source
type Source struct {
opt *source.Options
opt *source.Options
path []string
}

// Open a file for reading
Expand All @@ -38,13 +42,66 @@ func (s *Source) ReadFile(name string) (rb []byte, err error) {
rb, err = os.ReadFile(filepath.Join(s.opt.Dir,
fmt.Sprintf("%s.%s", name, source.Extends[i])))
if err == nil {
if s.path == nil {
s.path = make([]string, 0)
}
s.path = append(s.path, path)
s.opt.Extend = source.Extends[i]
return rb, nil
}
}
return nil, err
}

func (s *Source) Watch(c source.Entity, unm func([]byte, any) error) error {
slog.Debug("watch", slog.Any("path", s.path))
watcher, err := fsnotify.NewWatcher()
if err != nil {
return err
}
err = watcher.Add(s.path[0])

for i := range s.path {
err = watcher.Add(s.path[i])
if err != nil {
return err
}
}
go func(sc *Source, cfg source.Entity, w *fsnotify.Watcher, decoder func([]byte, any) error) {
for {
select {
case event, ok := <-w.Events:
if !ok {
break
}
if filepath.Ext(event.Name) != "."+sc.opt.Extend.String() {
continue
}
var rb []byte
for i := range sc.path {
rb, err = os.ReadFile(sc.path[i])
if err != nil {
slog.Error("read file error", slog.Any("error", err))
continue
}
err = decoder(rb, cfg)
if err != nil {
slog.Error("unmarshal error", slog.Any("error", err))
continue
}
}
cfg.OnChange()
case err, ok := <-w.Errors:
if !ok {
break
}
slog.Error("watch error", slog.Any("error", err))
}
}
}(s, c, watcher, unm)
return nil
}

// GetExtend get extend
func (s *Source) GetExtend() source.Scheme {
return s.opt.Extend
Expand All @@ -53,7 +110,8 @@ func (s *Source) GetExtend() source.Scheme {
// New returns a new source
func New(options ...source.Option) (*Source, error) {
s := &Source{
opt: source.DefaultOptions(),
opt: source.DefaultOptions(),
path: make([]string, 0),
}
for _, opt := range options {
opt(s.opt)
Expand Down
4 changes: 4 additions & 0 deletions pkg/config/source/mgdb/mgdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ func (s *Source) ReadFile(name string) ([]byte, error) {
return m.GenerateBytes()
}

func (s *Source) Watch(_ source.Entity, _ func([]byte, any) error) error {
return nil
}

// GetExtend get extend
func (s *Source) GetExtend() source.Scheme {
return s.opt.Extend
Expand Down
8 changes: 8 additions & 0 deletions pkg/config/source/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ var Extends = []Scheme{SchemeYaml, SchemeYml, SchemeJSOM}
type Sourcer interface {
fs.ReadFileFS
GetExtend() Scheme
Watch(e Entity, unm func([]byte, any) error) error
}

// Option option
Expand All @@ -62,6 +63,7 @@ type Options struct {
Datasource string
GORMDriver string
GORMDsn string
Watch bool
}

// DefaultOptions default options
Expand Down Expand Up @@ -185,3 +187,9 @@ func WithDriver(driver Driver) Option {
args.Driver = driver
}
}

func WithWatch(watch bool) Option {
return func(args *Options) {
args.Watch = watch
}
}
4 changes: 4 additions & 0 deletions pkg/config/source/s3/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ func (s *Source) ReadFile(name string) (rb []byte, err error) {
return nil, err
}

func (s *Source) Watch(_ source.Entity, _ func([]byte, any) error) error {
return nil
}

// GetExtend get extend
func (s *Source) GetExtend() source.Scheme {
return s.opt.Extend
Expand Down
6 changes: 3 additions & 3 deletions pkg/config/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ package config
*/

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

0 comments on commit 4824ee1

Please sign in to comment.