-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
carlo
committed
Jun 27, 2020
1 parent
fba9bb7
commit 00f00c3
Showing
11 changed files
with
261 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package common | ||
|
||
|
||
type CmdType int | ||
|
||
func (c CmdType) String() string { | ||
var value string | ||
switch c { | ||
case MsgReceive: | ||
value = "msg_receive" | ||
case MsgSend: | ||
value = "msg_send" | ||
default: | ||
value = "not found" | ||
} | ||
return value | ||
} | ||
|
||
const ( | ||
MsgReceive CmdType = iota + 1 //消息接受 | ||
MsgSend //消息发送 | ||
ContactPersonList //联系人列表 | ||
MsgTotalUnRead //总未读书 | ||
MsgUnRead //消息未读数 | ||
|
||
) | ||
|
||
|
||
var Cmd = &CommandHandler{CmdMap: make(map[CmdType]Command)} | ||
|
||
// 命令接口 | ||
type Command interface { | ||
Do(args interface{}) (interface{}, error) | ||
} | ||
|
||
// 上下文 | ||
type CmdContext struct { | ||
CmdType CmdType | ||
Args interface{} | ||
} | ||
|
||
// 命令管理者 | ||
type CommandHandler struct { | ||
CmdMap map[CmdType]Command | ||
} | ||
|
||
// 处理命令 | ||
func (ch *CommandHandler) Handle(ctx *CmdContext) (interface{}, error) { | ||
if ctx == nil { | ||
return nil, COMMAND_NIL | ||
} | ||
cmd, ok := ch.CmdMap[ctx.CmdType] | ||
if ok { | ||
return cmd.Do(ctx.Args) | ||
} | ||
return nil, COMMAND_INVALID | ||
} | ||
|
||
// 注册命令 | ||
func (ch *CommandHandler) Register(cmdType CmdType, cmd Command) { | ||
ch.CmdMap[cmdType] = cmd | ||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ import ( | |
var db *sql.DB | ||
|
||
|
||
func InitDB() { | ||
func Init() { | ||
var buf bytes.Buffer | ||
var err error | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package v1 | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"github.com/wudaoluo/sonic/common" | ||
"github.com/wudaoluo/sonic/middleware" | ||
"github.com/wudaoluo/sonic/model" | ||
"github.com/wudaoluo/sonic/service" | ||
) | ||
|
||
func LogicV1Router(parentRoute gin.IRouter) { | ||
router := parentRoute.Group("/auth") | ||
end := NewAuth() | ||
router.POST("/login",middleware.Jwt(),end.Login) | ||
router.POST("/logout",end.Logout) | ||
router.POST("/token/refresh",end.TokenRefresh) | ||
} | ||
|
||
type logic struct { | ||
service *service.AuthService | ||
} | ||
|
||
func NewLogic() *auth { | ||
return &auth{ | ||
service.NewAuthService(), | ||
} | ||
} | ||
|
||
func (l logic) Command(c *gin.Context) { | ||
var req model.AuthLogin | ||
if err := c.Bind(&req); err != nil { | ||
common.GinJsonRespErr(c,common.PARAM_ERROR) | ||
return | ||
} | ||
|
||
ret,err := common.Cmd.Handle(&common.CmdContext{CmdType: common.MsgReceive, Args: " Post"}) | ||
if err != nil { | ||
common.GinJsonRespErr(c,err) | ||
return | ||
} | ||
common.GinJsonResp(c,ret) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package queue | ||
|
||
import( | ||
"github.com/wudaoluo/golog" | ||
"github.com/wudaoluo/sonic/common" | ||
"context" | ||
"github.com/wudaoluo/sonic/queue/queue_default" | ||
) | ||
|
||
type Queue interface { | ||
//Start() | ||
Processor | ||
Consumeor | ||
} | ||
|
||
type Processor interface { | ||
Producer(ctx context.Context,buf []byte) error | ||
ProducerClose() error | ||
} | ||
|
||
type Consumeor interface { | ||
Consumer(ctx context.Context,fn func(buf []byte)) | ||
ConsumerClose() error | ||
} | ||
|
||
|
||
var queue Queue | ||
|
||
func Producer(ctx context.Context,buf []byte) error { | ||
return queue.Producer(ctx,buf) | ||
} | ||
|
||
|
||
func ProducerClose() error { | ||
return queue.ProducerClose() | ||
} | ||
|
||
func Consumer(ctx context.Context,fn func(buf []byte)) { | ||
queue.Consumer(ctx,fn) | ||
} | ||
|
||
func ConsumerClose() error { | ||
return queue.ConsumerClose() | ||
} | ||
|
||
func Init() { | ||
conf := &common.GetConf().Queue | ||
golog.Info("queue.init","type",conf.Type) | ||
switch conf.Type { | ||
case "kafka": | ||
//queue = queue_kafka.New(conf) | ||
case "default": | ||
queue = queue_default.New(conf) | ||
default: | ||
golog.Error("queue.init","err",common.NOT_FOUND_ERROR) | ||
panic(common.NOT_FOUND_ERROR) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package queue_default | ||
|
||
import ( | ||
"github.com/wudaoluo/sonic/model" | ||
"sync/atomic" | ||
"context" | ||
) | ||
|
||
type QueueDefault struct { | ||
queue chan []byte | ||
status int32 | ||
} | ||
|
||
func New(conf *model.ConfigQueue) *QueueDefault { | ||
return &QueueDefault{ | ||
queue:make(chan []byte, 100), | ||
status: 1, | ||
} | ||
} | ||
|
||
func (q *QueueDefault) Consumer(ctx context.Context,fn func(buf []byte)) { | ||
go func() { | ||
for msg := range q.queue { | ||
fn(msg) | ||
} | ||
}() | ||
|
||
} | ||
|
||
func (q *QueueDefault) ProducerClose() error { | ||
if atomic.LoadInt32(&q.status) == 1 { | ||
atomic.StoreInt32(&q.status,0) | ||
close(q.queue) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (q *QueueDefault) ConsumerClose() error { | ||
if atomic.LoadInt32(&q.status) == 1 { | ||
atomic.StoreInt32(&q.status,0) | ||
close(q.queue) | ||
} | ||
|
||
|
||
return nil | ||
} | ||
|
||
func (q *QueueDefault) Producer(ctx context.Context,buf []byte) error { | ||
if atomic.LoadInt32(&q.status) == 1 { | ||
q.queue <- buf | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package service | ||
|
||
import ( | ||
"fmt" | ||
"github.com/wudaoluo/sonic/common" | ||
) | ||
|
||
|
||
|
||
func init() { | ||
common.Cmd.Register(common.MsgReceive, &MsgReceive{}) | ||
} | ||
|
||
|
||
type MsgReceive struct { | ||
|
||
} | ||
|
||
func (m *MsgReceive) Do(args interface{}) (interface{}, error) { | ||
fmt.Println("PostCommand") | ||
return args, nil | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters