Apply .gitignore rules
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
package webServer
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"common/resultStatus"
|
||||
)
|
||||
|
||||
// 处理函数
|
||||
type HandleFunc func(context *ApiContext) *ResponseObject
|
||||
|
||||
// ApiHandler
|
||||
//
|
||||
// @description: API处理结构
|
||||
type ApiHandler struct {
|
||||
// API完整路径名称
|
||||
apiFullName string
|
||||
|
||||
// 方法定义
|
||||
handleFun HandleFunc
|
||||
|
||||
// 方法参数名称集合
|
||||
funcParamNames []string
|
||||
}
|
||||
|
||||
// ApiFullName
|
||||
//
|
||||
// @description: API完整路径名称
|
||||
//
|
||||
// parameter:
|
||||
//
|
||||
// @receiver this: this
|
||||
//
|
||||
// return:
|
||||
//
|
||||
// @string:
|
||||
func (this *ApiHandler) ApiFullName() string {
|
||||
return this.apiFullName
|
||||
}
|
||||
|
||||
// HandleFun
|
||||
//
|
||||
// @description: 方法定义
|
||||
//
|
||||
// parameter:
|
||||
//
|
||||
// @receiver this: this
|
||||
//
|
||||
// return:
|
||||
//
|
||||
// @HandleFunc: 方法
|
||||
func (this *ApiHandler) HandleFun() HandleFunc {
|
||||
return this.handleFun
|
||||
}
|
||||
|
||||
// FuncParamNames
|
||||
//
|
||||
// @description: 方法参数名称集合
|
||||
//
|
||||
// parameter:
|
||||
//
|
||||
// @receiver this: this
|
||||
//
|
||||
// return:
|
||||
//
|
||||
// @[]string: 方法参数名称集合
|
||||
func (this *ApiHandler) FuncParamNames() []string {
|
||||
return this.funcParamNames
|
||||
}
|
||||
|
||||
// CheckParam
|
||||
//
|
||||
// @description: 检测参数数量
|
||||
//
|
||||
// parameter:
|
||||
//
|
||||
// @receiver this: this
|
||||
// @r:
|
||||
//
|
||||
// return:
|
||||
//
|
||||
// @resultStatus.ResultStatus: 状态码数据
|
||||
func (this *ApiHandler) CheckParam(r *http.Request) resultStatus.ResultStatus {
|
||||
for _, name := range this.funcParamNames {
|
||||
if r.Form[name] == nil || len(r.Form[name]) == 0 {
|
||||
return resultStatus.APIParamError
|
||||
}
|
||||
}
|
||||
|
||||
return resultStatus.Success
|
||||
}
|
||||
|
||||
// newApiHandler
|
||||
//
|
||||
// @description: 创建新的请求方法对象
|
||||
//
|
||||
// parameter:
|
||||
//
|
||||
// @_apiFullName: API完整路径名称
|
||||
// @_funcDefinition: 方法定义
|
||||
// @_funcParamNames: 方法参数名称集合
|
||||
//
|
||||
// return:
|
||||
//
|
||||
// @*ApiHandler: 请求方法对象
|
||||
func newApiHandler(_apiFullName string, _funcDefinition HandleFunc, _funcParamNames ...string) *ApiHandler {
|
||||
return &ApiHandler{
|
||||
apiFullName: _apiFullName,
|
||||
handleFun: _funcDefinition,
|
||||
funcParamNames: _funcParamNames,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
module Framework
|
||||
|
||||
go 1.22.2
|
||||
|
||||
replace goutil => ../goutil
|
||||
|
||||
require (
|
||||
github.com/Shopify/sarama v1.29.1
|
||||
github.com/go-sql-driver/mysql v1.5.0
|
||||
github.com/gorilla/websocket v1.4.2
|
||||
github.com/jinzhu/gorm v1.9.12
|
||||
github.com/rabbitmq/amqp091-go v1.8.1
|
||||
github.com/samuel/go-zookeeper v0.0.0-20201211165307-7117e9ea2414
|
||||
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.230
|
||||
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vms v1.0.230
|
||||
goutil v0.0.0-00010101000000-000000000000
|
||||
)
|
||||
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/goutil.iml" filepath="$PROJECT_DIR$/.idea/goutil.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
@@ -0,0 +1,130 @@
|
||||
package notify_util
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"Framework/goroutineMgr"
|
||||
"goutil/logUtil"
|
||||
"goutil/syncUtil"
|
||||
)
|
||||
|
||||
const (
|
||||
deathLockTime = 0
|
||||
)
|
||||
|
||||
// notifyCenter
|
||||
// @description: 玩家信息务逻辑类
|
||||
type notifyCenter struct {
|
||||
// key:初始化成功的标志名称 val:占位符
|
||||
registerMap map[string]func()
|
||||
mutex *syncUtil.RWLocker
|
||||
}
|
||||
|
||||
// newNotifyCenter
|
||||
// @description: 构造对象
|
||||
// parameter:
|
||||
// return:
|
||||
// @*notifyCenter:
|
||||
func newNotifyCenter() *notifyCenter {
|
||||
return ¬ifyCenter{
|
||||
registerMap: make(map[string]func()),
|
||||
mutex: syncUtil.NewRWLocker(),
|
||||
}
|
||||
}
|
||||
|
||||
// register
|
||||
// @description: 注册需要被通知的对象
|
||||
// parameter:
|
||||
// @receiver nc:
|
||||
// @chanName:唯一标识
|
||||
// @cf:回调方法
|
||||
// return:
|
||||
func (nc *notifyCenter) register(chanName string, cf func()) {
|
||||
if isOk, prevStack, currStack := nc.mutex.Lock(deathLockTime); isOk == false {
|
||||
//记日志
|
||||
errMsg := fmt.Sprintf("Lock timeout! \n上一个堆栈:\n%s \n当前堆栈:\n%s", prevStack, currStack)
|
||||
panic(errMsg)
|
||||
}
|
||||
defer nc.mutex.Unlock()
|
||||
if _, exists := nc.registerMap[chanName]; exists {
|
||||
panic(fmt.Errorf("registerMap.Register-%s已经存在,请检查", chanName))
|
||||
}
|
||||
|
||||
nc.registerMap[chanName] = cf
|
||||
}
|
||||
|
||||
// @description: 取消启动成功通知注册
|
||||
// parameter:
|
||||
// @receiver nc:
|
||||
// @name:唯一标识
|
||||
// return:
|
||||
func (nc *notifyCenter) unregister(name string) {
|
||||
if isOk, prevStack, currStack := nc.mutex.Lock(deathLockTime); isOk == false {
|
||||
//记日志
|
||||
errMsg := fmt.Sprintf("Lock timeout! \n上一个堆栈:\n%s \n当前堆栈:\n%s", prevStack, currStack)
|
||||
panic(errMsg)
|
||||
}
|
||||
defer nc.mutex.Unlock()
|
||||
|
||||
delete(nc.registerMap, name)
|
||||
}
|
||||
|
||||
// notify
|
||||
// @description: 通知所有已注册的对象
|
||||
// parameter:
|
||||
// @receiver nc:
|
||||
// return:
|
||||
func (nc *notifyCenter) notify() {
|
||||
// 处理goroutine数量
|
||||
goroutineName := "notifyCenter.Notify"
|
||||
goroutineMgr.MonitorZero(goroutineName)
|
||||
defer goroutineMgr.ReleaseMonitor(goroutineName)
|
||||
|
||||
if isOk, prevStack, currStack := nc.mutex.RLock(deathLockTime); isOk == false {
|
||||
//记日志
|
||||
errMsg := fmt.Sprintf("Lock timeout! \n上一个堆栈:\n%s \n当前堆栈:\n%s", prevStack, currStack)
|
||||
panic(errMsg)
|
||||
}
|
||||
defer nc.mutex.RUnlock()
|
||||
|
||||
for name, cf := range nc.registerMap {
|
||||
cf()
|
||||
msg := fmt.Sprintf("通知:%s初始化成功", name)
|
||||
logUtil.DebugLog(msg)
|
||||
}
|
||||
}
|
||||
|
||||
// notify2
|
||||
// @description: 通知所有已注册的对象,该方法会在捕获第一个err的时候停止后续的通知。多用于系统启动的判定
|
||||
// parameter:
|
||||
// @receiver nc:
|
||||
// return:
|
||||
// @error:
|
||||
func (nc *notifyCenter) notify2() (err error) {
|
||||
// 处理goroutine数量
|
||||
goroutineName := "notifyCenter.Notify"
|
||||
goroutineMgr.MonitorZero(goroutineName)
|
||||
defer goroutineMgr.ReleaseMonitor(goroutineName)
|
||||
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
err = fmt.Errorf("notify2 err:%s", r)
|
||||
logUtil.ErrorLog(err.Error())
|
||||
}
|
||||
}()
|
||||
|
||||
if isOk, prevStack, currStack := nc.mutex.RLock(deathLockTime); isOk == false {
|
||||
//记日志
|
||||
errMsg := fmt.Sprintf("Lock timeout! \n上一个堆栈:\n%s \n当前堆栈:\n%s", prevStack, currStack)
|
||||
panic(errMsg)
|
||||
}
|
||||
defer nc.mutex.RUnlock()
|
||||
|
||||
for name, cf := range nc.registerMap {
|
||||
cf()
|
||||
msg := fmt.Sprintf("通知:%s初始化成功", name)
|
||||
logUtil.DebugLog(msg)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package timeUtil
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestConverToStandardFormat(t *testing.T) {
|
||||
str := "2018-10-10T10:10:10"
|
||||
expected := time.Date(2018, 10, 10, 10, 10, 10, 0, time.Local)
|
||||
|
||||
got, err := ConverToStandardFormat(str)
|
||||
if err != nil {
|
||||
t.Errorf("发生错误,错误信息为:%s", err)
|
||||
}
|
||||
|
||||
if got != expected {
|
||||
t.Errorf("转换不正确,期待:%s, 实际:%s", expected, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConvertToInt(t *testing.T) {
|
||||
date := time.Date(2018, 10, 10, 10, 10, 10, 0, time.Local)
|
||||
finalInt := ConvertToInt(date)
|
||||
expecteInt := 20181010
|
||||
|
||||
if finalInt != expecteInt {
|
||||
t.Errorf("转换不正确,期待:%d, 实际:%d", expecteInt, finalInt)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSubDay(t *testing.T) {
|
||||
time1 := time.Now().AddDate(0, 0, 5)
|
||||
time2 := time.Now()
|
||||
expected := 5
|
||||
|
||||
got := SubDay(time1, time2)
|
||||
if got != expected {
|
||||
t.Errorf("Expected %d, but now got %d.", expected, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseTimeString(t *testing.T) {
|
||||
val := "12:13:14"
|
||||
expectedHour := 12
|
||||
expectedMinute := 13
|
||||
expectedSecond := 14
|
||||
|
||||
err, hour, miniute, second := ParseTimeString(val)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if expectedHour != hour || expectedMinute != miniute || expectedSecond != second {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user