This commit is contained in:
zhaocy 2022-06-01 15:55:45 +08:00
commit 932f1ec591
11 changed files with 234 additions and 18 deletions

2
.gitignore vendored
View File

@ -15,5 +15,7 @@ bin/conf
.vscode/
./bin/conf
./bin/log
./bin/gateway
./bin/worker
~$*.xlsx
cmd/luban/

6
linux-build.bat Normal file
View File

@ -0,0 +1,6 @@
set GOOS=linux
set CGO_ENABLED=0
del bin/gateway,bin/worker
go build -o bin/gateway services/gateway/main.go
go build -o bin/worker services/worker/main.go
REM pause

View File

@ -3,6 +3,8 @@ package web
import (
"go_dreamfactory/comm"
"go_dreamfactory/modules"
"go_dreamfactory/sys/configure"
cfg "go_dreamfactory/sys/configure/structs"
"github.com/liwei1dao/lego/core"
)
@ -15,6 +17,7 @@ func NewModule() core.IModule {
type Web struct {
modules.ModuleBase
options *Options
table *cfg.TbItem
user_comp *User_Comp
}
@ -34,6 +37,16 @@ func (this *Web) Init(service core.IService, module core.IModule, options core.I
func (this *Web) Start() (err error) {
err = this.ModuleBase.Start()
var (
data interface{}
)
if err = configure.RegisterConfigure("tbitem.json", cfg.NewTbItem); err != nil {
return
}
if data, err = configure.GetConfigure("tbitem.json"); err != nil {
return
}
this.table = data.(*cfg.TbItem)
return
}

View File

@ -24,5 +24,6 @@ func (this *User_Comp) Login(ctx context.Context, session comm.IUserSession, rsp
session.SendMsg("LogigResp", &pb.UserLoginResp{
Code: 200,
})
return nil
}

View File

@ -2,8 +2,7 @@ package services
import (
"fmt"
"go_dreamfactory/sys/cache"
"go_dreamfactory/sys/db"
"go_dreamfactory/sys/configure"
"github.com/liwei1dao/lego/base/rpcx"
"github.com/liwei1dao/lego/sys/log"
@ -15,14 +14,9 @@ type ServiceBase struct {
func (this *ServiceBase) InitSys() {
this.RPCXService.InitSys()
if err := cache.OnInit(this.GetSettings().Sys["cache"]); err != nil {
panic(fmt.Sprintf("init sys.cache err: %s", err.Error()))
if err := configure.OnInit(this.GetSettings().Sys["configure"]); err != nil {
panic(fmt.Sprintf("init sys.configure err: %s", err.Error()))
} else {
log.Infof("init sys.cache success!")
}
if err := db.OnInit(this.GetSettings().Sys["db"]); err != nil {
panic(fmt.Sprintf("init sys.db err: %s", err.Error()))
} else {
log.Infof("init sys.db success!")
log.Infof("init sys.configure success!")
}
}

View File

@ -4,10 +4,13 @@ import (
"flag"
"go_dreamfactory/modules/login"
"go_dreamfactory/services"
"go_dreamfactory/sys/cache"
"go_dreamfactory/sys/db"
"github.com/liwei1dao/lego"
"github.com/liwei1dao/lego/base/rpcx"
"github.com/liwei1dao/lego/core"
"github.com/liwei1dao/lego/sys/log"
)
var (
@ -42,4 +45,14 @@ type Service struct {
func (this *Service) InitSys() {
this.ServiceBase.InitSys()
if err := cache.OnInit(this.GetSettings().Sys["cache"]); err != nil {
panic(fmt.Sprintf("init sys.cache err: %s", err.Error()))
} else {
log.Infof("init sys.cache success!")
}
if err := db.OnInit(this.GetSettings().Sys["db"]); err != nil {
panic(fmt.Sprintf("init sys.db err: %s", err.Error()))
} else {
log.Infof("init sys.db success!")
}
}

View File

@ -1,19 +1,152 @@
package configure
import "github.com/liwei1dao/lego/sys/redis"
import (
"fmt"
"io/ioutil"
"os"
"path"
"reflect"
"sync"
jsoniter "github.com/json-iterator/go"
)
var typeOfIn = reflect.TypeOf(([]map[string]interface{})(nil)).Elem()
var typeOfError = reflect.TypeOf((*error)(nil)).Elem()
type configurehandle struct {
configureType reflect.Type
fn reflect.Value
}
func newSys(options Options) (sys *Configure, err error) {
sys = &Configure{options: options}
sys = &Configure{
options: options,
configurehandles: map[string]*configurehandle{},
configure: map[string]interface{}{},
}
err = sys.init()
return
}
type Configure struct {
options Options
redis redis.ISys
options Options
hlock sync.RWMutex
configurehandles map[string]*configurehandle
clock sync.RWMutex
configure map[string]interface{}
}
func (this *Configure) init() (err error) {
return
}
//加载配置文件
func (this *Configure) RegisterConfigure(name string, fn interface{}) (err error) {
this.hlock.RLock()
_, ok := this.configurehandles[name]
this.hlock.RUnlock()
if ok {
err = fmt.Errorf("重复 注册配置【%s】", name)
return
}
fnvalue := reflect.ValueOf(fn)
if fnvalue.Type().NumIn() != 1 {
err = fmt.Errorf("LoadConfigure fn 类型错误! 只接受fn( _buf []map[string]interface{})(v,error) 函数参数")
return
}
inType := fnvalue.Type().In(0)
if inType.Elem() != typeOfIn {
err = fmt.Errorf("LoadConfigure fn 类型错误! 只接受fn( _buf []map[string]interface{})(v,error) 函数参数")
return
}
if fnvalue.Type().NumOut() != 2 {
err = fmt.Errorf("LoadConfigure fn 类型错误! 只接受fn( _buf []map[string]interface{})(v,error) 函数参数")
return
}
dataType := fnvalue.Type().Out(0)
if dataType.Kind() != reflect.Ptr {
err = fmt.Errorf("LoadConfigure fn 类型错误! 只接受fn( _buf []map[string]interface{})(v,error) 函数参数")
return
}
if returnType := fnvalue.Type().Out(1); returnType != typeOfError {
err = fmt.Errorf("LoadConfigure fn 类型错误! 只接受fn( _buf []map[string]interface{})(v,error) 函数参数")
return
}
handle := &configurehandle{
configureType: dataType,
fn: fnvalue,
}
if err = this.loaderConfigure(name, handle); err != nil {
return
}
this.hlock.Lock()
this.configurehandles[name] = handle
this.hlock.Unlock()
return
}
//更新配置信息
func (this *Configure) UpdateConfigure(names ...string) (err error) {
for _, v := range names {
this.hlock.RLock()
handle, ok := this.configurehandles[v]
this.hlock.RUnlock()
if !ok {
err = fmt.Errorf("no RegisterConfigure:%s", v)
return
}
if err = this.loaderConfigure(v, handle); err != nil {
err = fmt.Errorf("loaderConfigure:%s err:%v", v, err)
return
}
}
return
}
//读取配置文件
func (this *Configure) GetConfigure(name string) (v interface{}, err error) {
this.clock.RLock()
v, ok := this.configure[name]
this.clock.RUnlock()
if !ok {
err = fmt.Errorf("no LoadConfigure:%s", name)
}
return
}
//加载配置文件
func (this *Configure) loaderConfigure(name string, handle *configurehandle) (err error) {
var (
fliepath string
file *os.File
bytes []byte
data []map[string]interface{}
returnValues []reflect.Value
)
fliepath = path.Join(this.options.ConfigurePath, name)
if file, err = os.Open(fliepath); err != nil {
err = fmt.Errorf("no fond file:%s", fliepath)
return
}
defer file.Close()
if bytes, err = ioutil.ReadAll(file); err != nil {
err = fmt.Errorf("read file:%s err:%v", fliepath, err)
return
}
if err = jsoniter.Unmarshal(bytes, &data); err != nil {
err = fmt.Errorf("read file:%s json.Unmarshal err:%v", fliepath, err)
return
}
returnValues = handle.fn.Call([]reflect.Value{reflect.ValueOf(data)})
errInter := returnValues[1].Interface()
if errInter != nil {
err = fmt.Errorf("read file:%s load.fn err:%v", fliepath, errInter)
return
}
this.clock.Lock()
this.configure[name] = returnValues[0].Interface()
this.clock.Unlock()
return
}

View File

@ -7,6 +7,9 @@ package configure
type (
ISys interface {
RegisterConfigure(name string, fn interface{}) (err error) //注册配置
UpdateConfigure(names ...string) (err error) //更新配置
GetConfigure(name string) (v interface{}, err error) //获取配置
}
)
@ -29,3 +32,15 @@ func NewSys(option ...Option) (sys ISys, err error) {
defsys, err = newSys(options)
return
}
func RegisterConfigure(name string, fn interface{}) (err error) {
return defsys.RegisterConfigure(name, fn)
}
func UpdateConfigure(names ...string) (err error) {
return defsys.UpdateConfigure(names...)
}
func GetConfigure(name string) (v interface{}, err error) {
return defsys.GetConfigure(name)
}

View File

@ -6,6 +6,7 @@ import (
type Option func(*Options)
type Options struct {
ConfigurePath string
}
func newOptions(config map[string]interface{}, opts ...Option) (Options, error) {
@ -16,7 +17,6 @@ func newOptions(config map[string]interface{}, opts ...Option) (Options, error)
for _, o := range opts {
o(&options)
}
return options, nil
}

View File

@ -16,9 +16,12 @@ type DB struct {
}
func (this *DB) init() (err error) {
this.mgo, err = mgo.NewSys(
if this.mgo, err = mgo.NewSys(
mgo.SetMongodbUrl(this.options.MongodbUrl),
mgo.SetMongodbDatabase(this.options.MongodbDatabase),
)
); err != nil {
return
}
err = this.checkUserIdInit()
return
}

View File

@ -1,14 +1,20 @@
package db
import (
"context"
"fmt"
"go_dreamfactory/pb"
"math/rand"
"time"
"github.com/liwei1dao/lego/core"
"github.com/liwei1dao/lego/sys/log"
"go.mongodb.org/mongo-driver/bson"
)
const ( //Redis
DB_UserTable core.SqlTable = "user" //会话列表
DB_UserTable core.SqlTable = "user" //用户表
DB_UserIdTable core.SqlTable = "userid" //用户id表
)
func (this *DB) FindUserByAccount(account string) (*pb.DB_UserData, error) {
@ -36,6 +42,36 @@ func (this *DB) CreateUser(user *pb.DB_UserData) error {
return err
}
//校验数据库初始化工作是否完成
func (this DB) checkUserIdInit() (err error) {
ctx, _ := context.WithTimeout(context.Background(), time.Second*60)
count, err := this.mgo.CountDocuments(DB_UserIdTable, bson.M{})
if err != nil || count == 0 {
//批量插入数据
leng := 1000000
cIds := make([]interface{}, leng)
for i, _ := range cIds {
cIds[i] = 1000000 + i
}
data := make([]interface{}, leng)
r := rand.New(rand.NewSource(time.Now().Unix()))
n := 0
for _, i := range r.Perm(leng) {
data[n] = bson.M{"_id": i}
n++
}
var (
err error
)
begin := time.Now()
if _, err = this.mgo.InsertManyByCtx(DB_UserIdTable, ctx, data); err != nil {
return fmt.Errorf("checkUserIdInit err=%s", err.Error())
}
log.Debugf("checkUserIdInit succ time consuming:%v", time.Now().Sub(begin))
}
return
}
//更新用户数据到DB
func (this *DB) UpdateUser(data *pb.DB_UserData) (err error) {