This commit is contained in:
jiangyong27 2023-08-04 11:13:41 +08:00
commit 37f3ae7a26
15 changed files with 373 additions and 0 deletions

13
.gitignore vendored Normal file
View File

@ -0,0 +1,13 @@
*.iml
.idea/
output/
dockerfiles/
log
.DS_Store
web/node_modules
doc
cmd/test
go.sum
pkg
test.go
enterprise

21
Dockerfile Normal file
View File

@ -0,0 +1,21 @@
FROM registry.cn-shanghai.aliyuncs.com/devcon/godev:v1.0.14
ADD . /app/src
WORKDIR /app/src
RUN mkdir -p /app/bin /app/log
RUN export GOROOT=/usr/local/go1.18.2 && \
export GOPROXY=https://goproxy.cn && \
export PATH=$PATH:$GOROOT/bin && \
go mod tidy && \
go build cmd/enterprise.go && \
mv enterprise /app/bin && \
mv doc /app && \
cp -r conf /app && \
cp start.sh /app && \
chmod +x /app/start.sh
WORKDIR /app
EXPOSE 9281
CMD ["/app/start.sh"]

20
cmd/enterprise.go Normal file
View File

@ -0,0 +1,20 @@
package main
import (
"enterprise/common/config"
"enterprise/common/global"
"enterprise/server"
"enterprise/worker"
)
func main() {
config.LoadServerConfig()
global.InitGlobal()
if err := worker.Init(); err != nil {
panic(err)
}
if err := server.Start(); err != nil {
panic(err)
}
}

88
common/config/config.go Normal file
View File

@ -0,0 +1,88 @@
package config
import (
"github.com/mitchellh/mapstructure"
log "github.com/sirupsen/logrus"
"github.com/smbrave/goutil"
"github.com/spf13/viper"
"os"
"strings"
)
var (
config *Config
configEnv string
BuildTime string
CommitId string
)
type Mysql struct {
Host string `toml:"host"`
Port int `toml:"port"`
User string `toml:"user"`
Pass string `toml:"pass"`
Db string `toml:"db"`
}
type Server struct {
Address string `toml:"address"`
LogLevel int `toml:"log_level"`
}
type Redis struct {
Addr string `toml:"addr"`
Db int `toml:"db"`
Password string `toml:"password"`
}
type Config struct {
Server *Server `toml:"server"`
Mysql *Mysql `toml:"mysql"`
Redis *Redis `toml:"redis"`
}
func GetEnv() string {
return configEnv
}
func IsProdEnv() bool {
return configEnv == "prod"
}
func IsDevEnv() bool {
return configEnv == "dev"
}
func IsTestEnv() bool {
return configEnv == "test"
}
func GetConfig() *Config {
return config
}
func LoadServerConfig() {
configEnv = os.Getenv("CONFIG_ENV")
if configEnv == "" {
configEnv = "prod"
}
var envConfig Config
viper.SetConfigFile("conf/server.conf." + configEnv)
viper.SetConfigType("toml")
viper.AutomaticEnv()
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.SetEnvPrefix("conf")
if err := viper.ReadInConfig(); err != nil {
panic(err)
}
if err := viper.Unmarshal(&envConfig, func(decoderConfig *mapstructure.DecoderConfig) {
decoderConfig.TagName = "toml"
}); err != nil {
panic(err)
}
config = &envConfig
log.Infof("load real config[%s] ", goutil.EncodeJSONIndent(config))
}

83
common/global/db.go Normal file
View File

@ -0,0 +1,83 @@
package global
import (
"context"
"enterprise/common/config"
"fmt"
log "github.com/sirupsen/logrus"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"time"
)
var (
db *gorm.DB = nil
)
func GetDB() *gorm.DB {
return db
}
type DBLogger struct {
level logger.LogLevel
threshold int64
}
func (d *DBLogger) LogMode(level logger.LogLevel) logger.Interface {
d.level = level
return d
}
func (d *DBLogger) Info(context.Context, string, ...interface{}) {
}
func (d *DBLogger) Warn(context.Context, string, ...interface{}) {
}
func (d *DBLogger) Error(context.Context, string, ...interface{}) {
}
func (d *DBLogger) Trace(ctx context.Context, begin time.Time, fc func() (sql string, rowsAffected int64), err error) {
sql, affects := fc()
if err != nil && err != gorm.ErrRecordNotFound {
log.Errorf("[SQL]sql=%s affect=%d cost=%dms error=%v", sql, affects, time.Since(begin).Milliseconds(), err)
} else {
if time.Since(begin).Milliseconds() > d.threshold {
log.Errorf("[SQL]sql=%s affect=%d cost=%dms", sql, affects, time.Since(begin).Milliseconds())
} else {
log.Debugf("[SQL]sql=%s affect=%d cost=%dms", sql, affects, time.Since(begin).Milliseconds())
}
}
}
func initDB() error {
cfg := config.GetConfig()
dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", cfg.Mysql.User,
cfg.Mysql.Pass, cfg.Mysql.Host, cfg.Mysql.Port, cfg.Mysql.Db)
var err error
db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
if err != nil {
log.Errorf("open dsn[%s] error[%s]", dsn, err)
return err
}
db.Logger = &DBLogger{threshold: int64(2000)}
return nil
}
func DBPing() error {
d, err := db.DB()
if err != nil {
return err
}
if err := d.Ping(); err != nil {
return err
}
return nil
}

11
common/global/global.go Normal file
View File

@ -0,0 +1,11 @@
package global
func InitGlobal() {
if err := initDB(); err != nil {
panic(err)
}
if err := initLog(); err != nil {
panic(err)
}
}

43
common/global/log.go Normal file
View File

@ -0,0 +1,43 @@
package global
import (
"enterprise/common/config"
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
"github.com/rifflock/lfshook"
log "github.com/sirupsen/logrus"
"github.com/smbrave/goutil"
"os"
"time"
)
func initLog() error {
cfg := config.GetConfig()
logfile := "log/server.log"
writer, err := rotatelogs.New(
logfile+".%Y%m%d",
rotatelogs.WithLinkName(logfile),
rotatelogs.WithMaxAge(time.Duration(86400*7)*time.Second),
rotatelogs.WithRotationTime(time.Duration(86400)*time.Second),
)
if err != nil {
return err
}
pathMap := lfshook.WriterMap{
log.TraceLevel: writer,
log.DebugLevel: writer,
log.InfoLevel: writer,
log.WarnLevel: writer,
log.ErrorLevel: writer,
log.FatalLevel: writer,
log.PanicLevel: writer,
}
log.AddHook(lfshook.NewHook(pathMap, new(goutil.LogFile)))
log.SetOutput(os.Stdout)
log.SetReportCaller(true)
log.SetFormatter(new(goutil.LogFile))
log.SetLevel(log.Level(cfg.Server.LogLevel))
return nil
}

1
common/model/model.go Normal file
View File

@ -0,0 +1 @@
package model

0
conf/server.conf.dev Normal file
View File

16
conf/server.conf.prod Normal file
View File

@ -0,0 +1,16 @@
[server]
address = "0.0.0.0:9283"
#0:PAINC 1:FATAL 2:ERROR 3:WARNING 4:INFO 5:DEBUG 6:TRACE
log_level = 6
[mysql]
host = "127.0.0.1"
port = 3306
user = "unify2"
pass = "MDE2LCJIYXNoSWQiOjY"
db = "unify2"
[redis]
addr="127.0.0.1:6379"
db=0
password=""

49
go.mod Normal file
View File

@ -0,0 +1,49 @@
module enterprise
go 1.18
require (
github.com/ArtisanCloud/PowerWeChat/v3 v3.0.55
github.com/gogap/errors v0.0.0-20210818113853-edfbba0ddea9
github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible
github.com/mitchellh/mapstructure v1.5.0
github.com/rifflock/lfshook v0.0.0-20180920164130-b9218ef580f5
github.com/sirupsen/logrus v1.9.3
github.com/smbrave/goutil v0.0.0-20230602040814-2643c72c2849
github.com/spf13/cast v1.5.1
github.com/spf13/viper v1.16.0
gorm.io/driver/mysql v1.5.1
gorm.io/gorm v1.25.2
)
require (
github.com/ArtisanCloud/PowerLibs/v3 v3.0.12 // indirect
github.com/ArtisanCloud/PowerSocialite/v3 v3.0.6 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/facebookgo/stack v0.0.0-20160209184415-751773369052 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-sql-driver/mysql v1.7.0 // indirect
github.com/gogap/stack v0.0.0-20150131034635-fef68dddd4f8 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/jonboulle/clockwork v0.4.0 // indirect
github.com/lestrrat-go/strftime v1.0.6 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/redis/go-redis/v9 v9.0.3 // indirect
github.com/spf13/afero v1.9.5 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
go.uber.org/zap v1.21.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

6
server/server.go Normal file
View File

@ -0,0 +1,6 @@
package server
func Start() error {
select {}
return nil
}

16
start.sh Normal file
View File

@ -0,0 +1,16 @@
#!/bin/bash
set -x
ROOT=$(cd `dirname $0`; pwd)
cd $ROOT
if [ "$BINARY" = "" ];then
BINARY="enterprise"
fi
while [ true ];do
$ROOT/bin/$BINARY $@ >> log/run.log 2>&1
/usr/local/bin/sendproxy weixin jiangyong "$CONFIG_ENV $BINARY 服务崩溃[`date +"%Y-%m-%d %H:%M:%S"`]"
sleep 60
done

1
worker/checkin.go Normal file
View File

@ -0,0 +1 @@
package worker

5
worker/worker.go Normal file
View File

@ -0,0 +1,5 @@
package worker
func Init() error {
return nil
}