Ver Fonte

配置、redis实现合入

zwl há 5 anos atrás
pai
commit
d31007278b
7 ficheiros alterados com 346 adições e 10 exclusões
  1. 10 4
      account/account.go
  2. 222 0
      config/config.go
  3. 25 0
      config/config.xml
  4. 11 2
      db/db.go
  5. 1 1
      goods/goods.go
  6. 24 3
      main.go
  7. 53 0
      rediscli/rediscli.go

+ 10 - 4
account/account.go

@@ -2,6 +2,7 @@ package account
 
 import (
 	"mtp2_if/db"
+	"mtp2_if/rediscli"
 	"net/http"
 
 	"github.com/gin-gonic/gin"
@@ -21,10 +22,10 @@ type AccountQueryRsp struct {
 }
 
 type Taaccount struct {
-	Accountid  int `json:"accountid" xorm:"'ACCOUNTID'"`
-	Userid     int `json:"userid" xorm:"'USERID'"`
-	Currencyid int `json:"currencyid" xorm:"'CURRENCYID'"`
-	Changeflag int `json:"changeflag" xorm:"'CHANGEFLAG'"`
+	Accountid  int `json:"accountid" xorm:"'ACCOUNTID'" binding:"required"`
+	Userid     int `json:"userid" xorm:"'USERID'"  binding:"required"`
+	Currencyid int `json:"currencyid" xorm:"'CURRENCYID'"  binding:"required"`
+	Changeflag int `json:"changeflag" xorm:"'CHANGEFLAG'"  binding:"required"`
 }
 
 func (Taaccount) TableName() string {
@@ -38,6 +39,11 @@ func GetAccount(c *gin.Context) {
 		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
 	}
 
+	err = rediscli.CheckToken(account.AccountID, account.Token)
+	if err != nil {
+		c.JSON(http.StatusBadRequest, gin.H{"token check failed": err.Error()})
+	}
+
 	engine := db.GetEngine()
 
 	accountinfo := new(Taaccount)

+ 222 - 0
config/config.go

@@ -0,0 +1,222 @@
+package config
+
+import (
+	"errors"
+	"fmt"
+	"strconv"
+
+	"github.com/beevik/etree"
+)
+
+var SerCfg *ServiceConfig
+
+type WebConfig struct {
+	ListenPort int
+}
+
+type DbConfig struct {
+	DbDriver  string
+	DbAddress string
+	DbName    string
+	DbPort    string
+	DbUser    string
+	DbPwd     string
+}
+
+type RedisConfig struct {
+	Address string
+	Port    int
+	TimeOut int
+	ConnNum int
+	DbNum   int
+	Pwd     string
+}
+
+type ServiceConfig struct {
+	WebCfg   WebConfig
+	DbCfg    DbConfig
+	RedisCfg RedisConfig
+}
+
+func (c *ServiceConfig) Init(path string) error {
+	SerCfg = &ServiceConfig{}
+
+	cfgfile := etree.NewDocument()
+	err := cfgfile.ReadFromFile(path)
+	if err != nil {
+		fmt.Println("Read config file failed")
+		return err
+	}
+
+	root := cfgfile.SelectElement("Configuration")
+	if root == nil {
+		return errors.New("read config file failed")
+	}
+
+	// web配置
+	websettings := root.SelectElements("WebSetting")
+	for _, setting := range websettings {
+		web_listenport := setting.SelectElement("ListenPort")
+		if web_listenport == nil {
+			return errors.New("read web listen port failed")
+		}
+		ret, err := strconv.ParseUint(web_listenport.SelectAttrValue("value", "8080"), 10, 32)
+		if err != nil {
+			return errors.New("read web listen port is invalid:")
+		}
+		SerCfg.WebCfg.ListenPort = int(ret)
+	}
+
+	// 数据库配置
+	dbsettings := root.SelectElements("DbSetting")
+	for _, setting := range dbsettings {
+		// 数据库驱动
+		db_driver := setting.SelectElement("DbDriver")
+		if db_driver == nil {
+			return errors.New("read db driver failed")
+		}
+		SerCfg.DbCfg.DbDriver = db_driver.SelectAttrValue("value", "oci8")
+
+		// 数据库地址
+		db_address := setting.SelectElement("DbAddress")
+		if db_address == nil {
+			return errors.New("read db address failed")
+		}
+		SerCfg.DbCfg.DbAddress = db_address.SelectAttrValue("value", "0.0.0.0")
+
+		// 数据库名
+		db_name := setting.SelectElement("DbName")
+		if db_name == nil {
+			return errors.New("read db name failed")
+		}
+		SerCfg.DbCfg.DbName = db_name.SelectAttrValue("value", "orcl")
+
+		// 数据库端口
+		db_port := setting.SelectElement("DbPort")
+		if db_port == nil {
+			return errors.New("read db port failed")
+		}
+		SerCfg.DbCfg.DbPort = db_port.SelectAttrValue("value", "1521")
+
+		// 数据库名
+		db_user := setting.SelectElement("DbUser")
+		if db_user == nil {
+			return errors.New("read db user failed")
+		}
+		SerCfg.DbCfg.DbUser = db_user.SelectAttrValue("value", "mtp2_test01")
+
+		// 数据库密码
+		db_pwd := setting.SelectElement("DbPwd")
+		if db_pwd == nil {
+			return errors.New("read db password failed")
+		}
+		SerCfg.DbCfg.DbPwd = db_pwd.SelectAttrValue("value", "muchinfo")
+	}
+
+	// Redis配置
+	redissettings := root.SelectElements("RedisSetting")
+	for _, setting := range redissettings {
+		// 地址
+		redis_address := setting.SelectElement("Address")
+		if redis_address == nil {
+			return errors.New("read redis address failed")
+		}
+		SerCfg.RedisCfg.Address = redis_address.SelectAttrValue("value", "127.0.0.1")
+
+		// 端口
+		redis_port := setting.SelectElement("Port")
+		if redis_port == nil {
+			return errors.New("read redis port failed")
+		}
+		ret, err := strconv.ParseUint(redis_port.SelectAttrValue("value", "5007"), 10, 32)
+		if err != nil {
+			return errors.New("read redis port is invalid:")
+		}
+		SerCfg.RedisCfg.Port = int(ret)
+
+		// 超时时长
+		redis_timeout := setting.SelectElement("Timeout")
+		if redis_timeout == nil {
+			return errors.New("read redis timeout failed")
+		}
+		ret, err = strconv.ParseUint(redis_timeout.SelectAttrValue("value", "3"), 10, 32)
+		if err != nil {
+			return errors.New("read redis timeout is invalid:")
+		}
+		SerCfg.RedisCfg.TimeOut = int(ret)
+
+		// 连接数
+		redis_connnum := setting.SelectElement("ConnNum")
+		if redis_connnum == nil {
+			return errors.New("read redis connnet num failed")
+		}
+		ret, err = strconv.ParseUint(redis_connnum.SelectAttrValue("value", "1"), 10, 32)
+		if err != nil {
+			return errors.New("read redis connnet num is invalid:")
+		}
+		SerCfg.RedisCfg.ConnNum = int(ret)
+
+		// 库编号
+		redis_dbnum := setting.SelectElement("DbNum")
+		if redis_dbnum == nil {
+			return errors.New("read redis db num failed")
+		}
+		ret, err = strconv.ParseUint(redis_dbnum.SelectAttrValue("value", "0"), 10, 32)
+		if err != nil {
+			return errors.New("read redis db num is invalid:")
+		}
+		SerCfg.RedisCfg.DbNum = int(ret)
+
+		// 密码
+		redis_pwd := setting.SelectElement("Pwd")
+		if redis_pwd == nil {
+			return errors.New("read redis password failed")
+		}
+		SerCfg.RedisCfg.Pwd = redis_pwd.SelectAttrValue("value", "")
+	}
+
+	return nil
+}
+
+func (c *ServiceConfig) GetWebListenPort() int {
+	return SerCfg.WebCfg.ListenPort
+}
+
+func (c *ServiceConfig) GetDbDriver() string {
+	return SerCfg.DbCfg.DbDriver
+}
+
+func (c *ServiceConfig) GetDbSource() string {
+	dbsource := fmt.Sprintf("%s/%s@%s:%s/%s",
+		SerCfg.DbCfg.DbUser,
+		SerCfg.DbCfg.DbPwd,
+		SerCfg.DbCfg.DbAddress,
+		SerCfg.DbCfg.DbPort,
+		SerCfg.DbCfg.DbName)
+
+	return dbsource
+}
+
+func (c *ServiceConfig) GetRedisAddress() string {
+	redisaccress := fmt.Sprintf("%s:%d",
+		SerCfg.RedisCfg.Address,
+		SerCfg.RedisCfg.Port)
+
+	return redisaccress
+}
+
+func (c *ServiceConfig) GetRedisPwd() string {
+	return SerCfg.RedisCfg.Pwd
+}
+
+func (c *ServiceConfig) GetRedisDbNum() int {
+	return SerCfg.RedisCfg.DbNum
+}
+
+func (c *ServiceConfig) GetRedisConnNum() int {
+	return SerCfg.RedisCfg.ConnNum
+}
+
+func (c *ServiceConfig) GetRedisTimeout() int {
+	return SerCfg.RedisCfg.TimeOut
+}

+ 25 - 0
config/config.xml

@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="utf-8"?>
+
+<Configuration xmlns:http="http://www.w3.org/2001/XMLSchema-instance">
+    <WebSetting>
+        <ListenPort value="8080"/>
+    </WebSetting>
+
+    <DbSetting>
+        <DbDriver  value="oci8"/>
+        <DbAddress value="192.168.31.117"/>
+        <DbName    value="orcl"/>
+        <DbPort    value="1521"/>
+        <DbUser    value="mtp2_test18"/>
+        <DbPwd     value="muchinfo"/>
+    </DbSetting>
+
+    <RedisSetting>
+        <Address   value="192.168.31.136"/>
+        <Port      value="5007"/>
+        <Timeout   value="3"/>
+        <ConnNum   value="1"/>
+        <DbNum     value="0"/>
+        <Pwd       value=""/>
+    </RedisSetting>
+</Configuration>

+ 11 - 2
db/db.go

@@ -3,16 +3,21 @@ package db
 import (
 	"fmt"
 
+	"mtp2_if/config"
+
 	"github.com/xormplus/core"
 	"github.com/xormplus/xorm"
 )
 
-var driverName = "oci8"
-var dataSourceName = "mtp2_test18/muchinfo@192.168.31.117:1521/orcl"
+//var driverName = "oci8"
+//var dataSourceName = "mtp2_test18/muchinfo@192.168.31.117:1521/orcl"
 var Engine *xorm.Engine
 
 // NewDbDriver 创建数据库驱动
 func InitDbEngine() error {
+	driverName := config.SerCfg.GetDbDriver()
+	dataSourceName := config.SerCfg.GetDbSource()
+
 	var err error
 	Engine, err = xorm.NewOracle(driverName, dataSourceName)
 	if err != nil {
@@ -31,3 +36,7 @@ func InitDbEngine() error {
 func GetEngine() *xorm.Engine {
 	return Engine
 }
+
+func Close() {
+	Engine.Close()
+}

+ 1 - 1
goods/goods.go

@@ -83,7 +83,7 @@ func GetGoods(c *gin.Context) {
 	}
 
 	if !has {
-		c.JSON(http.StatusBadRequest, gin.H{"error": "not find"})
+		c.JSON(http.StatusBadRequest, gin.H{"error": "not find record"})
 		return
 	}
 

+ 24 - 3
main.go

@@ -1,20 +1,40 @@
 package main
 
 import (
+	"fmt"
 	"mtp2_if/account"
+	"mtp2_if/config"
 	"mtp2_if/db"
 	"mtp2_if/goods"
+	"mtp2_if/rediscli"
 
 	"github.com/gin-gonic/gin"
 	_ "github.com/mattn/go-oci8"
 )
 
 func main() {
-	err := db.InitDbEngine()
+	err := config.SerCfg.Init("config/config.xml")
 	if err != nil {
-		println("err", err)
+		println("read config file failed:", err)
+		return
 	}
 
+	// 初始化数据库引擎
+	err = db.InitDbEngine()
+	if err != nil {
+		println("init db engine failed:", err)
+		return
+	}
+	defer db.Close()
+
+	// 初始化Redis客户端
+	err = rediscli.InitRedisCli()
+	if err != nil {
+		println("init redis client failed:", err)
+		return
+	}
+	defer rediscli.CloseReidsClient()
+
 	r := gin.Default()
 
 	// 路由分组
@@ -30,5 +50,6 @@ func main() {
 		aropt.POST("/GetAccount", account.GetAccount)
 	}
 
-	r.Run(":8080")
+	port := fmt.Sprintf(":%d", config.SerCfg.GetWebListenPort())
+	r.Run(port)
 }

+ 53 - 0
rediscli/rediscli.go

@@ -0,0 +1,53 @@
+package rediscli
+
+import (
+	"errors"
+	"fmt"
+
+	"mtp2_if/config"
+
+	"github.com/go-redis/redis"
+)
+
+var RedisCli *redis.Client
+
+func InitRedisCli() error {
+	RedisCli = redis.NewClient(&redis.Options{
+		Addr:     config.SerCfg.GetRedisAddress(),
+		Password: config.SerCfg.GetRedisPwd(),
+		DB:       config.SerCfg.GetRedisDbNum(),
+		PoolSize: config.SerCfg.GetRedisConnNum(),
+	})
+
+	pong, err := RedisCli.Ping().Result()
+	if err != nil {
+		fmt.Println(pong, err)
+		return err
+	}
+
+	return nil
+}
+
+func GetRedisClient() *redis.Client {
+	return RedisCli
+}
+
+func CloseReidsClient() {
+	RedisCli.Close()
+}
+
+func CheckToken(accountid int, token string) error {
+	//key := fmt.Sprintf("monitor:online_loginid::%d", accountid)
+	key := "monitor:online_loginid::25001000000002"
+	field := "Token"
+
+	realToken, err := RedisCli.HGet(key, field).Result()
+	if err != nil {
+		return err
+	}
+
+	if realToken != token {
+		return errors.New("token is invalid")
+	}
+	return nil
+}