aozhiwei a5088a47cc 1
2024-06-05 12:05:24 +08:00

152 lines
3.6 KiB
Go

package app
import (
"crypto/md5"
"encoding/hex"
"f5"
"fmt"
"main/constant"
"main/task"
"math/rand"
"mt"
"sync"
"time"
)
type app struct {
initCb func()
unInitCb func()
sessionLock sync.Mutex
sessionHash map[string]string
accountIdHash map[string]string
}
func (this *app) GetPkgName() string {
return "adminserver"
}
func (this *app) GetHttpListenPort() int32 {
return 3000
}
func (this *app) Run(initCb func(), unInitCb func()) {
this.initCb = initCb
this.unInitCb = unInitCb
f5.Run(this)
}
func (this *app) Init() {
f5.LoadMetaTable(mt.Table)
this.registerDataSources()
this.sessionHash = make(map[string]string)
this.accountIdHash = make(map[string]string)
task.TaskMgr.Init()
this.initCb()
}
func (this *app) UnInit() {
task.TaskMgr.UnInit()
this.unInitCb()
}
func (this *app) Update() {
}
func (this *app) registerDataSources() {
f5.GetJsStyleDb().RegisterDataSource(
constant.GAME_DB,
mt.Table.GameDb.GetById(0).GetHost(),
mt.Table.GameDb.GetById(0).GetPort(),
mt.Table.GameDb.GetById(0).GetUser(),
mt.Table.GameDb.GetById(0).GetPasswd(),
mt.Table.GameDb.GetById(0).GetDatabase(),
1)
f5.GetJsStyleDb().RegisterDataSource(
constant.FRIEND_DB,
mt.Table.FriendDb.GetById(0).GetHost(),
mt.Table.FriendDb.GetById(0).GetPort(),
mt.Table.FriendDb.GetById(0).GetUser(),
mt.Table.FriendDb.GetById(0).GetPasswd(),
mt.Table.FriendDb.GetById(0).GetDatabase(),
1)
f5.GetApp().RegisterOrmDb(
constant.ADMIN_DB,
mt.Table.AdminDb.GetById(0).GetHost(),
mt.Table.AdminDb.GetById(0).GetPort(),
mt.Table.AdminDb.GetById(0).GetUser(),
mt.Table.AdminDb.GetById(0).GetPasswd(),
mt.Table.AdminDb.GetById(0).GetDatabase(),
)
f5.GetApp().RegisterOrmDb(
constant.ACCOUNT_DB,
mt.Table.AccountDb.GetById(0).GetHost(),
mt.Table.AccountDb.GetById(0).GetPort(),
mt.Table.AccountDb.GetById(0).GetUser(),
mt.Table.AccountDb.GetById(0).GetPasswd(),
mt.Table.AccountDb.GetById(0).GetDatabase(),
)
f5.GetApp().RegisterOrmDb(
constant.MAIL_DB,
mt.Table.MailDb.GetById(0).GetHost(),
mt.Table.MailDb.GetById(0).GetPort(),
mt.Table.MailDb.GetById(0).GetUser(),
mt.Table.MailDb.GetById(0).GetPasswd(),
mt.Table.MailDb.GetById(0).GetDatabase(),
)
f5.GetGoStyleDb().RegisterDataSource(
constant.MAIL_DB,
mt.Table.MailDb.GetById(0).GetHost(),
mt.Table.MailDb.GetById(0).GetPort(),
mt.Table.MailDb.GetById(0).GetUser(),
mt.Table.MailDb.GetById(0).GetPasswd(),
mt.Table.MailDb.GetById(0).GetDatabase(),
1,
)
}
func (this *app) AddSession(accountId string) string {
this.sessionLock.Lock()
defer this.sessionLock.Unlock()
uuid := f5.GetApp().NewGlobalUuid()
str := fmt.Sprintf("%s%d%s%d", accountId, uuid, randStringBytes(12), time.Now().Unix())
md5New := md5.New()
strByte := []byte(str)
md5New.Write(strByte)
md5String := hex.EncodeToString(md5New.Sum(nil))
token := accountId + "|" + md5String
this.sessionHash[accountId] = token
return token
}
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
func randStringBytes(n int) string {
b := make([]byte, n)
for i := range b {
b[i] = letterBytes[rand.Intn(len(letterBytes))]
}
return string(b)
}
func (this *app) GetSessionAccountId(accountId string) string {
this.sessionLock.Lock()
defer this.sessionLock.Unlock()
if session, ok := this.sessionHash[accountId]; ok {
return session
} else {
return "nil"
}
}
func (this *app) RemoveSession(accountId string) {
this.sessionLock.Lock()
defer this.sessionLock.Unlock()
if _, ok := this.sessionHash[accountId]; ok {
delete(this.sessionHash, accountId)
}
}
func (this *app) HasTask() bool {
return false
}