From b232685b63175cdde1dee8980b1afc6b1810e0c0 Mon Sep 17 00:00:00 2001 From: Evan Chen Date: Thu, 16 Dec 2021 14:25:57 +0800 Subject: [PATCH 1/6] save --- history/history.go | 58 +++++++++++++++++++++++++++++++++++++++++ history/history_test.go | 10 +++++++ messenger/messenger.go | 5 ++++ system/setup.go | 1 + system/system.go | 2 +- 5 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 history/history.go create mode 100644 history/history_test.go create mode 100644 messenger/messenger.go diff --git a/history/history.go b/history/history.go new file mode 100644 index 0000000..99af2d1 --- /dev/null +++ b/history/history.go @@ -0,0 +1,58 @@ +package history + +import ( + "fmt" + "time" +) + +const ( + ERROR = "ERROR" + INFO = "INFO" +) + +type History struct { + ID uint `gorm:"primaryKey"` + CreatedAt time.Time + + Module string + Type string + + Message string + Body string + Issuer string + Caller string + Trace string +} + +var Tunnel chan *History +var quit chan struct{} + +func init() { + Tunnel = make(chan *History) + quit = make(chan struct{}) +} + +func Start(r Receiver) { + go func() { + select { + case h := <-Tunnel: + r(h) + case <-quit: + return + } + }() +} + +func Stop() { + quit <- struct{}{} +} + +type Receiver func(*History) + +var DBReceiver Receiver = func(h *History) { + +} + +var ConsoleReceiver Receiver = func(h *History) { + fmt.Printf("%+v\n", h) +} diff --git a/history/history_test.go b/history/history_test.go new file mode 100644 index 0000000..e522f6b --- /dev/null +++ b/history/history_test.go @@ -0,0 +1,10 @@ +package history + +import ( + "testing" + "time" +) + +func TestHistory(t *testing.T) { + time.Sleep(time.Second * 30) +} diff --git a/messenger/messenger.go b/messenger/messenger.go new file mode 100644 index 0000000..8393a7d --- /dev/null +++ b/messenger/messenger.go @@ -0,0 +1,5 @@ +package messenger + +func init() { + +} diff --git a/system/setup.go b/system/setup.go index 753f44e..e2d099f 100644 --- a/system/setup.go +++ b/system/setup.go @@ -42,6 +42,7 @@ func init() { viper.SetEnvKeyReplacer(replacer) viper.AutomaticEnv() viper.SetConfigType("json") + setup() } func setup() { diff --git a/system/system.go b/system/system.go index 01fea71..c6302a9 100644 --- a/system/system.go +++ b/system/system.go @@ -98,7 +98,7 @@ func New() *System { } func (sys *System) Start() { - setup() + // setup() sys.order() sys.status = sys_init go func() { From c81b014919474ff7d6b85a7af5189a1c4e2733bc Mon Sep 17 00:00:00 2001 From: Evan Chen Date: Thu, 16 Dec 2021 18:11:34 +0800 Subject: [PATCH 2/6] save --- history/history.go | 18 ++++++++++++------ history/history_test.go | 24 +++++++++++++++++++++++- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/history/history.go b/history/history.go index 99af2d1..95dc4b0 100644 --- a/history/history.go +++ b/history/history.go @@ -29,16 +29,18 @@ var quit chan struct{} func init() { Tunnel = make(chan *History) - quit = make(chan struct{}) + quit = make(chan struct{}, 1) } func Start(r Receiver) { go func() { - select { - case h := <-Tunnel: - r(h) - case <-quit: - return + for { + select { + case h := <-Tunnel: + r(h) + case <-quit: + return + } } }() } @@ -47,6 +49,10 @@ func Stop() { quit <- struct{}{} } +func Send(h *History) { + Tunnel <- h +} + type Receiver func(*History) var DBReceiver Receiver = func(h *History) { diff --git a/history/history_test.go b/history/history_test.go index e522f6b..b9852fa 100644 --- a/history/history_test.go +++ b/history/history_test.go @@ -3,8 +3,30 @@ package history import ( "testing" "time" + + "kumoly.io/kumoly/app/util" ) func TestHistory(t *testing.T) { - time.Sleep(time.Second * 30) + t.Log("start") + Start(func(h *History) { + t.Logf("%+v\n", h) + }) + go func() { + for { + Send(&History{ + Module: "test", + Type: INFO, + Message: "testing", + Body: time.Now().String(), + Caller: util.Caller(2), + }) + time.Sleep(time.Second) + } + }() + + time.Sleep(time.Second * 20) + Stop() + t.Log("stoped") + time.Sleep(time.Second * 2) } From 905e1779a53dfddd66649976c4e486a9eb453b77 Mon Sep 17 00:00:00 2001 From: Evan Chen Date: Thu, 16 Dec 2021 19:11:46 +0800 Subject: [PATCH 3/6] save --- history/history.go | 3 +++ history/service.go | 17 +++++++++++++++++ system/helper.go | 2 +- 3 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 history/service.go diff --git a/history/history.go b/history/history.go index 95dc4b0..e25ce63 100644 --- a/history/history.go +++ b/history/history.go @@ -50,6 +50,9 @@ func Stop() { } func Send(h *History) { + if h.Type == "" { + h.Type = INFO + } Tunnel <- h } diff --git a/history/service.go b/history/service.go new file mode 100644 index 0000000..143e6a8 --- /dev/null +++ b/history/service.go @@ -0,0 +1,17 @@ +package history + +import "kumoly.io/kumoly/app/system" + +type Service struct { + system.BaseService +} + +func (srv Service) GetName() string { return "history.Service" } +func (srv Service) IsService() bool { return true } +func (srv Service) Main() error { + Start(DBReceiver) + return nil +} +func (srv Service) Del() { + Stop() +} diff --git a/system/helper.go b/system/helper.go index 243843a..ac27e9a 100644 --- a/system/helper.go +++ b/system/helper.go @@ -9,7 +9,7 @@ import ( type BaseService struct{} func (srv BaseService) GetName() string { return "base" } -func (srv BaseService) GetDependencies() []string { return []string{""} } +func (srv BaseService) GetDependencies() []string { return []string{} } func (srv BaseService) IsService() bool { return false } func (srv BaseService) Init() error { return nil } func (srv BaseService) Load() error { return nil } From 7e89c0795152bed4aaa18d2c46db803200a743af Mon Sep 17 00:00:00 2001 From: Evan Chen Date: Thu, 16 Dec 2021 19:44:07 +0800 Subject: [PATCH 4/6] save --- Makefile | 2 +- README.md | 8 +++++++- main.go => cmd/test/main.go | 0 history/history.go | 10 ++++++++++ run.sh | 9 +++++++++ system/setup.go | 22 +++++++++++++++++++--- 6 files changed, 46 insertions(+), 5 deletions(-) rename main.go => cmd/test/main.go (100%) create mode 100644 run.sh diff --git a/Makefile b/Makefile index 330dc96..054ab4f 100644 --- a/Makefile +++ b/Makefile @@ -5,4 +5,4 @@ run: APP_LOG_PRETTY=true \ APP_DB_TYPE=sqlite \ APP_DATA=work \ - go run main.go \ No newline at end of file + go run cmd/test/main.go \ No newline at end of file diff --git a/README.md b/README.md index b126c23..dd63f95 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,9 @@ # Kumoly App -combine all needed module into one lib \ No newline at end of file +combine all needed module into one lib + +## Run + +``` + +``` \ No newline at end of file diff --git a/main.go b/cmd/test/main.go similarity index 100% rename from main.go rename to cmd/test/main.go diff --git a/history/history.go b/history/history.go index e25ce63..0819c2e 100644 --- a/history/history.go +++ b/history/history.go @@ -3,6 +3,8 @@ package history import ( "fmt" "time" + + "kumoly.io/kumoly/app/errors" ) const ( @@ -26,19 +28,27 @@ type History struct { var Tunnel chan *History var quit chan struct{} +var started chan struct{} func init() { Tunnel = make(chan *History) quit = make(chan struct{}, 1) + started = make(chan struct{}, 1) } func Start(r Receiver) { + select { + case started <- struct{}{}: + default: + panic(errors.New(500, "history has already started!")) + } go func() { for { select { case h := <-Tunnel: r(h) case <-quit: + <-started return } } diff --git a/run.sh b/run.sh new file mode 100644 index 0000000..e8149a4 --- /dev/null +++ b/run.sh @@ -0,0 +1,9 @@ + +export APP_SERVER_HOST=127.0.0.1 +export APP_SERVER_PORT=8000 +export APP_LOG_LEVEL=-1 +export APP_PROD=false +export APP_LOG_PRETTY=true +export APP_DB_TYPE=sqlite +export APP_DATA=work +go run cmd/test/main.go \ No newline at end of file diff --git a/system/setup.go b/system/setup.go index e2d099f..0aabf29 100644 --- a/system/setup.go +++ b/system/setup.go @@ -1,6 +1,7 @@ package system import ( + "fmt" "io/ioutil" "os" "path/filepath" @@ -61,9 +62,24 @@ func setup() { if cc, ok := i.(string); ok { c = cc } - // if len(c) > 0 { - - // } + if len(c) > 0 { + // shorten caller to mod/file:line + segs := strings.Split(c, ":") + file := segs[len(segs)-2] + short := file + skip := false + for i := len(file) - 1; i > 0; i-- { + if file[i] == '/' { + if !skip { + skip = true + continue + } + short = file[i+1:] + break + } + } + return fmt.Sprintf("%v:%v", short, segs[len(segs)-1]) + } return c }, }) From 716f19133d9bddf7464b8d2194f125aa182d5886 Mon Sep 17 00:00:00 2001 From: Evan Chen Date: Thu, 16 Dec 2021 21:43:56 +0800 Subject: [PATCH 5/6] update --- cmd/test/main.go | 3 ++- history/helper.go | 27 +++++++++++++++++++++++++++ history/history.go | 39 ++++++++++++++++++++++++++++++++------- history/service.go | 19 ++++++++++++++++++- util/logging.go | 28 ++++++++++++++++++++++++++++ util/logging_test.go | 22 ++++++++++++++++++++++ 6 files changed, 129 insertions(+), 9 deletions(-) create mode 100644 history/helper.go create mode 100644 util/logging_test.go diff --git a/cmd/test/main.go b/cmd/test/main.go index 674cc2f..758eed3 100644 --- a/cmd/test/main.go +++ b/cmd/test/main.go @@ -2,6 +2,7 @@ package main import ( "kumoly.io/kumoly/app/auth" + "kumoly.io/kumoly/app/history" "kumoly.io/kumoly/app/server" "kumoly.io/kumoly/app/store" "kumoly.io/kumoly/app/system" @@ -17,7 +18,7 @@ func main() { auth.SetDB(store.DB) sys.Inject(auth.Injector(server.API)) - sys.Append(server, auth.New(server), &task.Service{}) + sys.Append(server, auth.New(server), &task.Service{}, &history.Service{}) sys.Start() } diff --git a/history/helper.go b/history/helper.go new file mode 100644 index 0000000..f1bf463 --- /dev/null +++ b/history/helper.go @@ -0,0 +1,27 @@ +package history + +import "kumoly.io/kumoly/app/util" + +func Error() *History { + h := getBase() + h.Type = ERROR + if !PROD { + h.Trace = util.Stack() + } + return h +} + +func Info() *History { + h := getBase() + h.Type = INFO + return h +} + +func getBase() *History { + mod, file := util.CallerMod(3) + h := &History{ + Module: mod, + Caller: file, + } + return h +} diff --git a/history/history.go b/history/history.go index 0819c2e..0e06433 100644 --- a/history/history.go +++ b/history/history.go @@ -1,12 +1,18 @@ package history import ( + "encoding/json" "fmt" "time" + "github.com/rs/zerolog/log" + "gorm.io/gorm" "kumoly.io/kumoly/app/errors" + "kumoly.io/kumoly/app/store" ) +var PROD = false + const ( ERROR = "ERROR" INFO = "INFO" @@ -19,11 +25,22 @@ type History struct { Module string Type string - Message string - Body string - Issuer string - Caller string - Trace string + Message string + BodyJson string + Body interface{} `gorm:"-"` + Issuer string + Caller string + Trace string +} + +func (h *History) BeforeCreate(tx *gorm.DB) (err error) { + if h.Body != nil { + body, err := json.Marshal(h.Body) + if err != nil { + h.BodyJson = string(body) + } + } + return } var Tunnel chan *History @@ -40,12 +57,15 @@ func Start(r Receiver) { select { case started <- struct{}{}: default: - panic(errors.New(500, "history has already started!")) + panic(errors.New(500, "history reporter has already started!")) } go func() { for { select { case h := <-Tunnel: + if Interceptor != nil { + go Interceptor(h) + } r(h) case <-quit: <-started @@ -68,8 +88,13 @@ func Send(h *History) { type Receiver func(*History) -var DBReceiver Receiver = func(h *History) { +var Interceptor func(*History) = nil +var DBReceiver Receiver = func(h *History) { + err := store.DB.Create(h).Error + if err != nil { + log.Error().Str("mod", "history").Err(err).Msg("DBReceiver error") + } } var ConsoleReceiver Receiver = func(h *History) { diff --git a/history/service.go b/history/service.go index 143e6a8..e561d71 100644 --- a/history/service.go +++ b/history/service.go @@ -1,6 +1,11 @@ package history -import "kumoly.io/kumoly/app/system" +import ( + "github.com/rs/zerolog/log" + "github.com/spf13/viper" + "kumoly.io/kumoly/app/store" + "kumoly.io/kumoly/app/system" +) type Service struct { system.BaseService @@ -8,6 +13,18 @@ type Service struct { func (srv Service) GetName() string { return "history.Service" } func (srv Service) IsService() bool { return true } +func (srv Service) Init() error { + PROD = viper.GetBool("prod") + l := log.With().Str("mod", "history"). + Str("service", "history.Service"). + Logger() + l.Debug().Msg("Migrating database for history.Service ...") + if err := store.Migrate(&History{}); err != nil { + l.Error().Err(err).Msg("Migrating database") + return err + } + return nil +} func (srv Service) Main() error { Start(DBReceiver) return nil diff --git a/util/logging.go b/util/logging.go index 80e92ea..2111582 100644 --- a/util/logging.go +++ b/util/logging.go @@ -7,11 +7,39 @@ import ( "runtime" ) +func Stack() string { + buf := make([]byte, 1024) + for { + n := runtime.Stack(buf, false) + if n < len(buf) { + return string(buf[:n]) + } + buf = make([]byte, 2*len(buf)) + } +} + func CallerFull(depth int) string { _, file, line, _ := runtime.Caller(depth) return fmt.Sprintf("%s:%d", file, line) } +func CallerMod(depth int) (mod, file string) { + _, f, l, _ := runtime.Caller(depth) + ptr := 0 + for i := len(f) - 1; i > 0; i-- { + if f[i] == '/' { + if ptr == 0 { + file = fmt.Sprintf("%v:%v", f[i+1:], l) + ptr = i + } else { + mod = f[i+1 : ptr] + break + } + } + } + return +} + func Caller(depth int) string { _, file, line, _ := runtime.Caller(depth) short := file diff --git a/util/logging_test.go b/util/logging_test.go new file mode 100644 index 0000000..4e456d6 --- /dev/null +++ b/util/logging_test.go @@ -0,0 +1,22 @@ +package util + +import ( + "fmt" + "testing" +) + +func TestStack(t *testing.T) { + fmt.Println(Stack()) +} + +func TestCallerFull(t *testing.T) { + fmt.Println(CallerFull(1)) +} + +func TestCallerMod(t *testing.T) { + fmt.Println(CallerMod(1)) +} + +func TestCaller(t *testing.T) { + fmt.Println(Caller(1)) +} From 4ccf2db1a24922415bd201f2622b449f0ef4d871 Mon Sep 17 00:00:00 2001 From: Evan Chen Date: Thu, 16 Dec 2021 23:45:31 +0800 Subject: [PATCH 6/6] update --- scripts/build.sh => build.sh | 0 messenger/base.gohtml | 476 +++++++++++++++++++++++++++++++++++ 2 files changed, 476 insertions(+) rename scripts/build.sh => build.sh (100%) create mode 100644 messenger/base.gohtml diff --git a/scripts/build.sh b/build.sh similarity index 100% rename from scripts/build.sh rename to build.sh diff --git a/messenger/base.gohtml b/messenger/base.gohtml new file mode 100644 index 0000000..4adfac3 --- /dev/null +++ b/messenger/base.gohtml @@ -0,0 +1,476 @@ + + + + + + + + + + + + + + + \ No newline at end of file