mirror of
https://gitee.com/dromara/electron-egg.git
synced 2026-05-21 08:08:09 +08:00
go
This commit is contained in:
@@ -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'],
|
||||
},
|
||||
},
|
||||
};
|
||||
@@ -17,7 +17,7 @@ module.exports = (appInfo) => {
|
||||
/**
|
||||
* 应用程序顶部菜单
|
||||
*/
|
||||
config.openAppMenu = 'dev-show';
|
||||
config.openAppMenu = true;
|
||||
|
||||
/**
|
||||
* 主窗口
|
||||
|
||||
17
go/boot/work.go
Normal file
17
go/boot/work.go
Normal file
@@ -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")
|
||||
}
|
||||
15
go/go.mod
Normal file
15
go/go.mod
Normal file
@@ -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
|
||||
)
|
||||
18
go/go.sum
Normal file
18
go/go.sum
Normal file
@@ -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=
|
||||
15
go/helper/helper.go
Normal file
15
go/helper/helper.go
Normal file
@@ -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])
|
||||
}
|
||||
}
|
||||
150
go/log/logger.go
Normal file
150
go/log/logger.go
Normal file
@@ -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)
|
||||
//}
|
||||
96
go/log/options.go
Normal file
96
go/log/options.go
Normal file
@@ -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
|
||||
}
|
||||
}
|
||||
5
go/main.go
Normal file
5
go/main.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
eboot.Run()
|
||||
}
|
||||
@@ -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",
|
||||
|
||||
Reference in New Issue
Block a user