From 2e86d3ea573f2598aa30699363893e24a6849347 Mon Sep 17 00:00:00 2001 From: gaoshuaixing Date: Tue, 19 Sep 2023 17:19:17 +0800 Subject: [PATCH] go --- electron/config/bin.js | 14 ++- electron/config/config.default.js | 2 +- go/boot/work.go | 17 ++++ go/go.mod | 15 +++ go/go.sum | 18 ++++ go/helper/helper.go | 15 +++ go/log/logger.go | 150 ++++++++++++++++++++++++++++++ go/log/options.go | 96 +++++++++++++++++++ go/main.go | 5 + package.json | 3 +- 10 files changed, 332 insertions(+), 3 deletions(-) create mode 100644 go/boot/work.go create mode 100644 go/go.mod create mode 100644 go/go.sum create mode 100644 go/helper/helper.go create mode 100644 go/log/logger.go create mode 100644 go/log/options.go create mode 100644 go/main.go diff --git a/electron/config/bin.js b/electron/config/bin.js index 45c22ca..114028f 100644 --- a/electron/config/bin.js +++ b/electron/config/bin.js @@ -72,5 +72,17 @@ module.exports = { stringArrayEncoding: ['none'], deadCodeInjection: false, } - } + }, + + /** + * 执行自定义命令 + * ee-bin exec + */ + exec: { + go: { + directory: './go', + cmd: 'go', + args: ['run', '-tags=fts5', './main.go', '--wd=../', '--env=dev', '--port=6789'], + }, + }, }; \ No newline at end of file diff --git a/electron/config/config.default.js b/electron/config/config.default.js index 0f2b4eb..c2c229b 100644 --- a/electron/config/config.default.js +++ b/electron/config/config.default.js @@ -17,7 +17,7 @@ module.exports = (appInfo) => { /** * 应用程序顶部菜单 */ - config.openAppMenu = 'dev-show'; + config.openAppMenu = true; /** * 主窗口 diff --git a/go/boot/work.go b/go/boot/work.go new file mode 100644 index 0000000..2838670 --- /dev/null +++ b/go/boot/work.go @@ -0,0 +1,17 @@ +package eboot + +const ( + Version = "0.1.0" +) + +var ( + ENV = "dev" // 'dev' 'prod' + progressBar float64 // 0~100 + progressDesc string // Description + HttpServer = false +) + +func Run() { + elog.NewLogger() + elog.GetLogger().Info("hconf example success tttt") +} diff --git a/go/go.mod b/go/go.mod new file mode 100644 index 0000000..d0c9f75 --- /dev/null +++ b/go/go.mod @@ -0,0 +1,15 @@ +module egg + +go 1.20 + +require ( + github.com/natefinch/lumberjack v2.0.0+incompatible + go.uber.org/zap v1.26.0 +) + +require ( + github.com/BurntSushi/toml v1.3.2 // indirect + go.uber.org/multierr v1.10.0 // indirect + gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect +) diff --git a/go/go.sum b/go/go.sum new file mode 100644 index 0000000..707f678 --- /dev/null +++ b/go/go.sum @@ -0,0 +1,18 @@ +github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8= +github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/natefinch/lumberjack v2.0.0+incompatible h1:4QJd3OLAMgj7ph+yZTuX13Ld4UpgHp07nNdFX7mqFfM= +github.com/natefinch/lumberjack v2.0.0+incompatible/go.mod h1:Wi9p2TTF5DG5oU+6YfsmYQpsTIOm0B1VNzQg9Mw6nPk= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= +go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk= +go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ= +go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= +go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo= +go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc= +gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/go/helper/helper.go b/go/helper/helper.go new file mode 100644 index 0000000..2864c18 --- /dev/null +++ b/go/helper/helper.go @@ -0,0 +1,15 @@ +package helper + +import ( + "fmt" + "os" + "strings" +) + +func GetEnv() { + envs := os.Environ() + for _, env := range envs { + cache := strings.Split(env, "=") + fmt.Printf("%v = %v\n", cache[0], cache[1]) + } +} diff --git a/go/log/logger.go b/go/log/logger.go new file mode 100644 index 0000000..b2399e1 --- /dev/null +++ b/go/log/logger.go @@ -0,0 +1,150 @@ +package elog + +import ( + "context" + "fmt" + "os" + "strings" + "sync" + + "github.com/natefinch/lumberjack" + "go.uber.org/zap" + "go.uber.org/zap/zapcore" +) + +var ( + l *Logger + outWrite zapcore.WriteSyncer // IO输出 + debugConsole = zapcore.Lock(os.Stdout) // 控制台标准输出 + once sync.Once +) + +type Logger struct { + *zap.Logger + opts *Options + zapConfig zap.Config +} + +func NewLogger(opts ...HLogOptions) { + once.Do(func() { + l = &Logger{ + opts: newOptions(opts...), + } + l.loadCfg() + l.initHLog() + l.Info("[initLogger] zap plugin initializing completed") + }) +} + +// GetLogger returns logger +func GetLogger() *Logger { + if l == nil { + fmt.Println("Please initialize the hlog service first") + return nil + } + return l +} + +func (l *Logger) GetCtx(ctx context.Context) *zap.Logger { + log, ok := ctx.Value(l.opts.CtxKey).(*zap.Logger) + if ok { + return log + } + return l.Logger +} + +func (l *Logger) WithContext(ctx context.Context) *zap.Logger { + log, ok := ctx.Value(l.opts.CtxKey).(*zap.Logger) + if ok { + return log + } + return l.Logger +} + +func (l *Logger) AddCtx(ctx context.Context, field ...zap.Field) (context.Context, *zap.Logger) { + log := l.With(field...) + ctx = context.WithValue(ctx, l.opts.CtxKey, log) + return ctx, log +} + +func (l *Logger) initHLog() { + l.setSyncers() + var err error + l.Logger, err = l.zapConfig.Build(l.cores()) + if err != nil { + panic(err) + } + defer l.Logger.Sync() +} +func (l *Logger) GetLevel() (level zapcore.Level) { + switch strings.ToLower(l.opts.Level) { + case "debug": + return zapcore.DebugLevel + case "info": + return zapcore.InfoLevel + case "warn": + return zapcore.WarnLevel + case "error": + return zapcore.ErrorLevel + case "dpanic": + return zapcore.DPanicLevel + case "panic": + return zapcore.PanicLevel + case "fatal": + return zapcore.FatalLevel + default: + return zapcore.DebugLevel //默认为调试模式 + } +} + +func (l *Logger) loadCfg() { + if l.opts.Development { + l.zapConfig = zap.NewDevelopmentConfig() + //l.zapConfig.EncoderConfig.EncodeTime = timeEncoder + } else { + l.zapConfig = zap.NewProductionConfig() + //l.zapConfig.EncoderConfig.EncodeTime = timeUnixNano + } +} + +func (l *Logger) setSyncers() { + outWrite = zapcore.AddSync(&lumberjack.Logger{ + Filename: l.opts.LogFileDir + "/" + l.opts.AppName + ".log", + MaxSize: l.opts.MaxSize, + MaxBackups: l.opts.MaxBackups, + MaxAge: l.opts.MaxAge, + Compress: true, + LocalTime: true, + }) + return +} + +func (l *Logger) cores() zap.Option { + encoder := zapcore.NewJSONEncoder(l.zapConfig.EncoderConfig) + priority := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool { + return lvl >= l.GetLevel() + }) + var cores []zapcore.Core + if l.opts.WriteFile { + cores = append(cores, []zapcore.Core{ + zapcore.NewCore(encoder, outWrite, priority), + }...) + } + if l.opts.WriteConsole { + cores = append(cores, []zapcore.Core{ + zapcore.NewCore(encoder, debugConsole, priority), + }...) + } + return zap.WrapCore(func(c zapcore.Core) zapcore.Core { + return zapcore.NewTee(cores...) + }) +} + +// 可自定义时间 +//func timeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) { +// enc.AppendString(t.Format("2006-01-02 15:04:05")) +//} +// +//func timeUnixNano(t time.Time, enc zapcore.PrimitiveArrayEncoder) { +// enc.AppendInt64(t.UnixNano() / 1e6) +//} diff --git a/go/log/options.go b/go/log/options.go new file mode 100644 index 0000000..637b57e --- /dev/null +++ b/go/log/options.go @@ -0,0 +1,96 @@ +package elog + +import "path/filepath" + +type Options struct { + Development bool + LogFileDir string + AppName string + MaxSize int //文件多大开始切分 + MaxBackups int //保留文件个数 + MaxAge int //文件保留最大实际 + Level string + CtxKey string //通过 ctx 传递 hlog 对象 + WriteFile bool + WriteConsole bool +} + +type HLogOptions func(*Options) + +func newOptions(opts ...HLogOptions) *Options { + opt := &Options{ + Development: true, + AppName: "hlog-app", + MaxSize: 100, + MaxBackups: 60, + MaxAge: 30, + Level: "debug", + CtxKey: "hlog-ctx", + WriteFile: false, + WriteConsole: true, + } + opt.LogFileDir, _ = filepath.Abs(filepath.Dir(filepath.Join("."))) + opt.LogFileDir += "/logs/" + for _, o := range opts { + o(opt) + } + return opt +} + +func SetDevelopment(development bool) HLogOptions { + return func(options *Options) { + options.Development = development + } +} + +func SetLogFileDir(logFileDir string) HLogOptions { + return func(options *Options) { + options.LogFileDir = logFileDir + } +} + +func SetAppName(appName string) HLogOptions { + return func(options *Options) { + options.AppName = appName + } +} + +func SetMaxSize(maxSize int) HLogOptions { + return func(options *Options) { + options.MaxSize = maxSize + } +} +func SetMaxBackups(maxBackups int) HLogOptions { + return func(options *Options) { + options.MaxBackups = maxBackups + } +} +func SetMaxAge(maxAge int) HLogOptions { + return func(options *Options) { + options.MaxAge = maxAge + } +} + +func SetLevel(level string) HLogOptions { + return func(options *Options) { + options.Level = level + } +} + +func SetCtxKey(ctxKey string) HLogOptions { + return func(options *Options) { + options.CtxKey = ctxKey + } +} + +func SetWriteFile(writeFile bool) HLogOptions { + return func(options *Options) { + options.WriteFile = writeFile + } +} + +func SetWriteConsole(writeConsole bool) HLogOptions { + return func(options *Options) { + options.WriteConsole = writeConsole + } +} diff --git a/go/main.go b/go/main.go new file mode 100644 index 0000000..c7eb722 --- /dev/null +++ b/go/main.go @@ -0,0 +1,5 @@ +package main + +func main() { + eboot.Run() +} diff --git a/package.json b/package.json index 8505473..fbe34e9 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,13 @@ { "name": "ee", - "version": "3.7.0", + "version": "3.8.0", "description": "A fast, desktop software development framework", "main": "main.js", "scripts": { "dev": "ee-bin dev", "dev-frontend": "ee-bin dev --serve=frontend", "dev-electron": "ee-bin dev --serve=electron", + "dev-go": "ee-bin exec --command=go", "build-frontend": "ee-bin build", "start": "ee-bin start", "rd": "ee-bin rd",