Skip to content

Commit

Permalink
添加发送消息逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
carlo committed Jul 1, 2020
1 parent 6b9e998 commit 2ae0c71
Show file tree
Hide file tree
Showing 20 changed files with 522 additions and 215 deletions.
22 changes: 7 additions & 15 deletions cmd/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,16 @@ var authCmd = &cobra.Command{

v1.AuthV1Router(router)

err := router.Run(common.GetConf().Auth.Addr)
if err != nil {
golog.Error("auth service start faild","err",err)
panic(err)
}
go func() {
err := router.Run(common.GetConf().Auth.Addr)
if err != nil {
golog.Error("auth service start faild","err",err)
panic(err)
}
}()
},
}

func init() {
rootCmd.AddCommand(authCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// authCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// authCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
22 changes: 7 additions & 15 deletions cmd/logic.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,16 @@ to quickly create a Cobra application.`,

v1.LogicV1Router(router)

err := router.Run(common.GetConf().Logic.Addr)
if err != nil {
golog.Error("auth service start faild","err",err)
panic(err)
}
go func() {
err := router.Run(common.GetConf().Logic.Addr)
if err != nil {
golog.Error("auth service start faild","err",err)
panic(err)
}
}()
},
}

func init() {
rootCmd.AddCommand(logicCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// logicCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// logicCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
107 changes: 58 additions & 49 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,79 +16,88 @@ limitations under the License.
package cmd

import (
"fmt"
"github.com/spf13/cobra"
"github.com/wudaoluo/sonic/common"
"github.com/wudaoluo/sonic/dao"
"github.com/wudaoluo/sonic/queue"
"os"

"github.com/spf13/viper"
"fmt"
"os"

"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/wudaoluo/golog"
"github.com/wudaoluo/golog/conf"
"github.com/wudaoluo/sonic/common"
"github.com/wudaoluo/sonic/dao"
"github.com/wudaoluo/sonic/queue"
)


var cfgFile string


// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "sonic",
Short: "A brief description of your application",
Long: `A longer description that spans multiple lines and likely contains
Use: "sonic",
Short: "A brief description of your application",
Long: `A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
Run: func(cmd *cobra.Command, args []string) {
authCmd.Run(cmd, args)
logicCmd.Run(cmd, args)
},
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}

func init() {
cobra.OnInitialize(initConfig) //永远在顶层
cobra.OnInitialize(dao.Init)
cobra.OnInitialize(queue.Init)

// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
cobra.OnInitialize(initConfig) //永远在顶层
cobra.OnInitialize(dao.Init)
cobra.OnInitialize(queue.Init)

rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "sonic.toml", "config file (default is $HOME/.sonic.yaml)")
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.

rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "sonic.toml", "config file (default is $HOME/.sonic.yaml)")

// Cobra also supports local flags, which will only run
// when this action is called directly.
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
// Cobra also supports local flags, which will only run
// when this action is called directly.
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}


// initConfig reads in config file and ENV variables if set.
// initConfig rea`ds in config file and ENV variables if set.
func initConfig() {
viper.SetConfigFile(cfgFile)
viper.AutomaticEnv() // read in environment variables that match

// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
}else {
fmt.Println(err)
os.Exit(1)
}

err := viper.Unmarshal(common.GetConf())
if err != nil {
fmt.Println(err)
os.Exit(1)
}
viper.SetConfigFile(cfgFile)
viper.AutomaticEnv() // read in environment variables that match

// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
} else {
fmt.Println(err)
os.Exit(1)
}

err := viper.Unmarshal(common.GetConf())
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}

func initLog() {
golog.SetLogger(golog.ZAPLOG,
conf.WithLogType(conf.LogJsontype),
conf.WithProjectName("sonic"),
conf.WithFilename("sonic.log"),
)
}
13 changes: 6 additions & 7 deletions common/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package common

import (
"encoding/json"

"github.com/wudaoluo/golog"
"github.com/wudaoluo/sonic/model"
)
Expand All @@ -10,7 +11,7 @@ var Cmd = &CommandHandler{CmdMap: make(map[model.CmdType]Command)}

// 命令接口
type Command interface {
Do(args interface{}) (interface{}, error)
Do(args json.RawMessage) (interface{}, error)
}

// 命令管理者
Expand All @@ -25,13 +26,14 @@ func (ch *CommandHandler) Handle(buf []byte) (interface{}, error) {
}

var param model.LogicCommand
err := json.Unmarshal(buf,&param)
err := json.Unmarshal(buf, &param)
if err != nil {
golog.Error("Handle","err",err)
golog.Error("Handle", "err", err)
return nil, err
}

cmd, ok := ch.CmdMap[param.MsgType]
golog.Debug("handle", "MsgCode", param.MsgCode, "msg", string(param.Data))
cmd, ok := ch.CmdMap[param.MsgCode]
if ok {
return cmd.Do(param.Data)
}
Expand All @@ -42,6 +44,3 @@ func (ch *CommandHandler) Handle(buf []byte) (interface{}, error) {
func (ch *CommandHandler) Register(CmdType model.CmdType, cmd Command) {
ch.CmdMap[CmdType] = cmd
}



18 changes: 11 additions & 7 deletions common/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,32 @@ package common

import (
"errors"

"github.com/gin-gonic/gin"
)

var (
//EXPIRED_ERROR = engine.NewResponseError(ERROR_STATUS_UNAUTHORIZED, "token expired")
//AD_USERINFO_ERROR = engine.NewResponseError(ERROR_LOGIQUE_FAILURE, "ad userInfo is empty")
// 参数错误
PARAM_ERROR = errors.New("param is error")
PARAM_ERROR = errors.New("param is error")
NOT_FOUND_ERROR = errors.New("not found")
SERVICE_ERROR = errors.New("服务器错误")
AUTH_ERROR = errors.New("认证失败")
TOKEN_INVALID = errors.New("token is invalid")
SERVICE_ERROR = errors.New("服务器错误")
AUTH_ERROR = errors.New("认证失败")
TOKEN_INVALID = errors.New("token is invalid")
COMMAND_INVALID = errors.New("invalid Command")
COMMAND_NIL = errors.New("command is nil")
COMMAND_NIL = errors.New("command is nil")

DB_NOT_FOUND_ERR = errors.New("db item not found")
DB_INSERT_ERR = errors.New("db insert faild")
//PARAM_ERROR = engine.NewResponseError(ERROR_PARAM_ERROR, "param is error")
//MYSQL_PARAM_ERROR = engine.NewResponseError(ERROR_PARAM_ERROR, "sql param is error")
)


func ErrEXec(err error) error {
if gin.Mode() != gin.ReleaseMode {
return err
}

return SERVICE_ERROR
}
}
1 change: 1 addition & 0 deletions common/msg_type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package common
42 changes: 42 additions & 0 deletions dao/im_msg_contact.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package dao

import (
"time"

"github.com/wudaoluo/sonic/common"

"github.com/go-xorm/xorm"
"github.com/wudaoluo/sonic/model"
)

type ImMsgContactService struct {
}

var DBImMsgContact *ImMsgContactService

func init() {
DBImMsgContact = &ImMsgContactService{}
}

func (t *ImMsgContactService) table() *xorm.Session {
return db.Table("im_msg_contact")
}

func (t *ImMsgContactService) Insert(data *model.ImMsgContact) (int64, error) {
data.CreateTime = time.Now()
_, err := t.table().InsertOne(data)
if err != nil {
return 0, err
}

if data.Mid == 0 {
return 0, common.DB_INSERT_ERR
}

return data.Mid, nil
}

func (t *ImMsgContactService) Update(data *model.ImMsgContact) (int64, error) {
return t.table().Where("owner_uid = ?", data.OwnerUid).
And("other_uid = ?", data.OtherUid).Cols("mid", "type").Update(data)
}
38 changes: 38 additions & 0 deletions dao/im_msg_content.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package dao

import (
"time"

"github.com/wudaoluo/sonic/common"

"github.com/go-xorm/xorm"

"github.com/wudaoluo/sonic/model"
)

type ImMsgContentService struct {
}

var DBImMsgContent *ImMsgContentService

func init() {
DBImMsgContent = &ImMsgContentService{}
}

func (t *ImMsgContentService) table() *xorm.Session {
return db.Table("im_msg_content")
}

func (t *ImMsgContentService) Insert(data *model.ImMsgContent) (int64, error) {
data.CreateTime = time.Now()
_, err := t.table().InsertOne(data)
if err != nil {
return 0, err
}

if data.Mid == 0 {
return 0, common.DB_INSERT_ERR
}

return data.Mid, nil
}
Loading

0 comments on commit 2ae0c71

Please sign in to comment.