func initializeForStorageProvider(parameter MysqlParameter) error {
	mysqlServerAddr := fmt.Sprintf("%v:%v", parameter.Ip, parameter.Port)
	Logger().Infof("Initialize mysql storage provider (parameter=%v)...", parameter)
	mysqlConnPool = &pool.Pool{Id: "MySQL Connection Pool", Size: int(parameter.PoolSize)}
	initFunc := func() (interface{}, error) {
		conn := autorc.New("tcp", "", mysqlServerAddr, parameter.User, parameter.Password)
		conn.Raw.Register("set names utf8")
		err := conn.Use(parameter.DbName)
		if err != nil {
			errorMsg := fmt.Sprintf("Occur error when mysql connection initialization (parameter=%v): %s\n", parameter, err)
			Logger().Errorln(errorMsg)
			return nil, err
		}
		return conn, nil
	}
	err := mysqlConnPool.Init(initFunc)
	if err != nil {
		errorMsg := fmt.Sprintf("Occur error when mysql connection pool initialization (parameter=%v): %s\n", parameter, err)
		Logger().Errorln(errorMsg)
		return errors.New(errorMsg)
	}
	signMap = make(map[string]*go_lib.Sign)
	iMysqlStorageProvider = &mysqlStorageProvider{parameter.Name}
	return nil
}
Exemple #2
0
// dumps a specific chunk, reading chunk info from the cchunk channel
func dumpChunk(cc string) {
	var out *os.File
	dbcon := autorc.New("tcp", "", host+":"+port, user, password, schema)
	defer func() {
		out.Close()
		if err := recover(); err != nil {
			die(err.(error).Error())
		}
		wg.Done()
	}()

	for {
		cr := <-cchunks
		out, _ = os.Create(path + "/" + schema + "." + table + "." + strconv.Itoa(cr.lower) + "." + strconv.Itoa(cr.upper) + ".csv")
		rows, _, _ := dbcon.Query("select * from " + schema + "." + table + " where " + cc + " between " + strconv.Itoa(cr.lower) + " and " + strconv.Itoa(cr.upper))
		for _, row := range rows {
			line := ""

			for idx, _ := range row {
				comma := ","
				if idx == len(row)-1 {
					comma = ""
				}
				line += row.Str(idx) + comma
			}

			out.WriteString(line + "\n")
		}
		if done {
			return
		}
	}
}
Exemple #3
0
// connects to MySQL
func initMySQL() {
	var err error
	debug("connecting to MySQL")
	debug("Will connect to tcp, " + host + ":" + port + ", " + user + ", " + password + ", " + schema)
	db = autorc.New("tcp", "", host+":"+port, user, password, schema)
	_, _, err = db.Query("select 1")
	if err != nil {
		die("An error occurred while connecting to MySQL: " + err.Error())
	}
}
//func initConn() (*autorc.Conn, error) {
func initConn() (interface{}, error) {
	conn := autorc.New("tcp", "", "localhost:3306", "root", "root", "test")
	// Register initialisation commands
	conn.Register("set names utf8")
	fmt.Println(" --- autorc --- ", conn.Raw, " --- ")
	//rows, res, err := db.Query("Select * From `test` Limit 1")
	//fmt.Println(rows)
	//fmt.Println(res)
	//fmt.Println(err)
	return conn, nil
}
Exemple #5
0
func main() {
	// START OMIT
	conn := autorc.New("tcp", "", "localhost:3306", "user", "passwd", "database")
	stmtSelectFooBar, err := conn.Prepare(`SELECT foo, bar FROM stuff WHERE ID = ?`)
	if err != nil {
		panic(err)
	}
	rows, res, err := stmtSelectFooBar.Exec(42)
	if err != nil {
		panic(err)
	}
	for _, row := range rows {
		foo := row.Str(res.Map("foo"))
		bar := row.Uint64(res.Map("bar"))
		fmt.Println(foo, bar)
	}
	// END OMIT
}
Exemple #6
0
// (读+写)连接数据库+选择数据库
//func InitDB(address, user, pass, name string, logSlowQuery bool, logSlowTime int64) (*DB, error){
func InitDB() (*DB, error) {
	//fmt.Println("InitDB")
	host := conf.GetValue("db", "host")
	port := conf.GetValue("db", "port")
	user := conf.GetValue("db", "user")
	pass := conf.GetValue("db", "pass")
	name := conf.GetValue("db", "name")
	logSlowQuery, _ := strconv.ParseBool(conf.GetValue("db", "log_slow_query"))
	logSlowTime, _ := strconv.ParseInt(conf.GetValue("db", "log_slow_time"), 0, 64)
	address := host + ":" + port

	//db := new(DB)
	db := &DB{logSlowQuery: logSlowQuery, logSlowTime: logSlowTime}
	conn := autorc.New("tcp", "", address, user, pass, name)
	conn.Register("set names utf8")
	db.Conn = conn
	return db, nil
}
Exemple #7
0
// username:password@tcp(address:port)/dbname
func (self *dbExecutor) Open(dsn string, showOperate bool, connCount int) error {
	self.showOperate = showOperate

	self.connChan = make(chan *autorc.Conn, connCount)

	var conn *autorc.Conn

	for i := 0; i < connCount; i++ {

		if usr, passwd, nettype, addr, dbname, err := parseDSN(dsn); err == nil {

			//func New(proto, laddr, raddr, user, passwd string, db ...string) *Conn {
			conn = autorc.New(nettype, "", addr, usr, passwd, dbname)
		} else {
			return err
		}

		self.connChan <- conn
	}

	return nil
}
// test database connection settings
func (g *GuerrillaDBAndRedisBackend) testDbConnections() (err error) {
	db := autorc.New(
		"tcp",
		"",
		g.config.MysqlHost,
		g.config.MysqlUser,
		g.config.MysqlPass,
		g.config.MysqlDB)

	if mysqlErr := db.Raw.Connect(); mysqlErr != nil {
		err = fmt.Errorf("MySql cannot connect, check your settings: %s", mysqlErr)
	} else {
		db.Raw.Close()
	}

	redisClient := &redisClient{}
	if redisErr := redisClient.redisConnection(g.config.RedisInterface); redisErr != nil {
		err = fmt.Errorf("Redis cannot connect, check your settings: %s", redisErr)
	}

	return
}
Exemple #9
0
func New(host string, port string, user string, password string, database string, tdhPort string) *Handa {
	self := &Handa{
		tableCacheVarMutex: new(sync.Mutex),
	}

	// DDL listeners
	self.startTableDDLListener()
	self.columnDDL = make(map[string]chan columnDDLReq)
	self.indexDDL = make(map[string]chan indexDDLReq)

	// init database connection pool
	self.dbname = database
	self.mysqlConnPool = make(chan *autorc.Conn, MysqlConnPoolSize)
	for i := 0; i < MysqlConnPoolSize; i++ {
		conn := autorc.New("tcp", "", host+":"+port, user, password, database)
		conn.Register("set names utf8")
		self.mysqlConnPool <- conn
	}
	self.socketConnPool = make(chan *tdh.Conn, SocketConnPoolSize)
	for i := 0; i < SocketConnPoolSize; i++ {
		socket, err := tdh.New(host+":"+tdhPort, "", "")
		if err != nil {
			fatal("Tdhsocket connect error")
		}
		self.socketConnPool <- socket
	}

	// load table schemas
	schema := make(map[string]*TableInfo)
	row, _, _ := self.mysqlQuery("SHOW TABLES")
	for _, row := range row {
		tableName := row.Str(0)
		schema[tableName] = self.loadTableInfo(tableName)
	}
	self.schema = schema

	return self
}
Exemple #10
0
func InitDatabase(addr, name, password, dbname string) {
	db = mysql.New("tcp", "", addr, name, password, dbname)
}
Exemple #11
0
func init() {
	var err error

	loadConf()

	if len(config.Db.Addr) < 1 ||
		len(config.Db.User) < 1 ||
		len(config.Db.Pass) < 1 ||
		len(config.Db.Name) < 1 {
		fmt.Printf("Missing db config\n")
		os.Exit(1)
	}

	db = autorc.New("tcp", "",
		config.Db.Addr,
		config.Db.User,
		config.Db.Pass,
		config.Db.Name)

	db.Raw.Register("SET NAMES utf8")

	plugin_insert_stmt, err = db.Prepare("INSERT INTO plugin (id, owner) VALUES(?, ?)")
	mysqlErrExit(err)

	version_insert_stmt, err = db.Prepare("INSERT INTO version (plugin_id, version, type, author, showtime_min_version, title, synopsis, description, homepage, pkg_digest, category, icon_digest, status) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
	mysqlErrExit(err)

	version_delete_stmt, err = db.Prepare("DELETE FROM version WHERE plugin_id=? AND version=?")
	mysqlErrExit(err)

	status_set_stmt, err = db.Prepare("UPDATE version SET status=? WHERE plugin_id=? AND version=?")
	mysqlErrExit(err)

	published_set_stmt, err = db.Prepare("UPDATE version SET published=? WHERE plugin_id=? AND version=?")
	mysqlErrExit(err)

	user_query_stmt, err = db.Prepare("SELECT salt, sha1, email, admin, autoapprove FROM users WHERE username = ?")
	mysqlErrExit(err)

	admin_query_stmt, err = db.Prepare("SELECT username, email, autoapprove FROM users WHERE admin = true")
	mysqlErrExit(err)

	user_insert_stmt, err = db.Prepare("INSERT INTO users (username, salt, sha1, email) VALUES(?, ?, ?, ?)")
	mysqlErrExit(err)

	version_dlinc_stmt, err = db.Prepare("UPDATE version SET downloads=downloads+1 WHERE plugin_id = ? AND version = ?")
	mysqlErrExit(err)

	plugin_update_stmt, err = db.Prepare("UPDATE plugin SET betasecret=?, downloadurl=? WHERE id=?")
	mysqlErrExit(err)

	track_update_stmt, err = db.Prepare("INSERT INTO tracking (id, ua, count, ipaddr, cc) VALUE (?, ?, 1, ?, ?) ON DUPLICATE KEY UPDATE count=count + 1, ua=?, updated=now(), ipaddr=?, cc=?")
	mysqlErrExit(err)

	plugin_delete_stmt, err = db.Prepare("DELETE FROM plugin WHERE id=?")
	mysqlErrExit(err)

	rows, _, err := db.Query("SELECT id, owner, betasecret,downloadurl FROM plugin")
	mysqlErrExit(err)

	for _, r := range rows {
		id := r.Str(0)
		plugins[id] = newPlugin(id, r.Str(1), r.Str(2), r.Str(3))
	}

	rows, _, err = db.Query("SELECT plugin_id, version, type, author, downloads," +
		"published, showtime_min_version, title, synopsis, description," +
		"homepage, pkg_digest, comment, category, status, icon_digest FROM version")
	mysqlErrExit(err)

	for _, r := range rows {
		plugin_id := r.Str(0)
		version := r.Str(1)
		v := PluginVersion{
			PluginId:        plugin_id,
			Version:         version,
			Type:            r.Str(2),
			Author:          r.Str(3),
			Downloads:       r.Int(4),
			Published:       r.Bool(5),
			ShowtimeVersion: r.Str(6),
			Title:           r.Str(7),
			Synopsis:        r.Str(8),
			Description:     r.Str(9),
			Homepage:        r.Str(10),
			PkgDigest:       r.Str(11),
			Comment:         r.Str(12),
			Category:        r.Str(13),
			Status:          r.Str(14),
			IconDigest:      r.Str(15),
		}
		plugins[plugin_id].versions[version] = &v
		pkgHashToVersion[v.PkgDigest] = &v

		v.pkg_ver, err = parseVersionString(v.Version)
		if err != nil {
			log.Println(err)
			os.Exit(1)
		}

		v.showtime_ver, err = parseVersionString(v.ShowtimeVersion)
		if err != nil {
			log.Println(err)
			os.Exit(1)
		}

	}
}
func (g *GuerrillaDBAndRedisBackend) saveMail() {
	var to, body string
	var err error

	var redisErr error
	var length int
	redisClient := &redisClient{}
	db := autorc.New(
		"tcp",
		"",
		g.config.MysqlHost,
		g.config.MysqlUser,
		g.config.MysqlPass,
		g.config.MysqlDB)
	db.Register("set names utf8")
	sql := "INSERT INTO " + g.config.MysqlTable + " "
	sql += "(`date`, `to`, `from`, `subject`, `body`, `charset`, `mail`, `spam_score`, `hash`, `content_type`, `recipient`, `has_attach`, `ip_addr`, `return_path`, `is_tls`)"
	sql += " values (NOW(), ?, ?, ?, ? , 'UTF-8' , ?, 0, ?, '', ?, 0, ?, ?, ?)"
	ins, sqlErr := db.Prepare(sql)
	if sqlErr != nil {
		log.WithError(sqlErr).Fatalf("failed while db.Prepare(INSERT...)")
	}
	sql = "UPDATE gm2_setting SET `setting_value` = `setting_value`+1 WHERE `setting_name`='received_emails' LIMIT 1"
	incr, sqlErr := db.Prepare(sql)
	if sqlErr != nil {
		log.WithError(sqlErr).Fatalf("failed while db.Prepare(UPDATE...)")
	}

	//  receives values from the channel repeatedly until it is closed.
	for {
		payload := <-g.saveMailChan
		if payload == nil {
			log.Debug("No more payload")
			g.wg.Done()
			return
		}
		to = payload.recipient.User + "@" + g.config.PrimaryHost
		length = len(payload.mail.Data)
		ts := fmt.Sprintf("%d", time.Now().UnixNano())
		payload.mail.Subject = MimeHeaderDecode(payload.mail.Subject)
		hash := MD5Hex(
			to,
			payload.mail.MailFrom.String(),
			payload.mail.Subject,
			ts)
		// Add extra headers
		var addHead string
		addHead += "Delivered-To: " + to + "\r\n"
		addHead += "Received: from " + payload.mail.Helo + " (" + payload.mail.Helo + "  [" + payload.mail.RemoteAddress + "])\r\n"
		addHead += "	by " + payload.recipient.Host + " with SMTP id " + hash + "@" + payload.recipient.Host + ";\r\n"
		addHead += "	" + time.Now().Format(time.RFC1123Z) + "\r\n"
		// compress to save space
		payload.mail.Data = Compress(addHead, payload.mail.Data)
		body = "gzencode"
		redisErr = redisClient.redisConnection(g.config.RedisInterface)
		if redisErr == nil {
			_, doErr := redisClient.conn.Do("SETEX", hash, g.config.RedisExpireSeconds, payload.mail.Data)
			if doErr == nil {
				payload.mail.Data = ""
				body = "redis"
			}
		} else {
			log.WithError(redisErr).Warn("Error while SETEX on redis")
		}
		// bind data to cursor
		ins.Bind(
			to,
			payload.mail.MailFrom.String(),
			payload.mail.Subject,
			body,
			payload.mail.Data,
			hash,
			to,
			payload.mail.RemoteAddress,
			payload.mail.MailFrom.String(),
			payload.mail.TLS,
		)
		// save, discard result
		_, _, err = ins.Exec()
		if err != nil {
			errMsg := "Database error while inserting"
			log.WithError(err).Warn(errMsg)
			payload.savedNotify <- &saveStatus{errors.New(errMsg), hash}
		} else {
			log.Debugf("Email saved %s (len=%d)", hash, length)
			_, _, err = incr.Exec()
			if err != nil {
				log.WithError(err).Warn("Database error while incr count")
			}
			payload.savedNotify <- &saveStatus{nil, hash}
		}
	}
}
// TODO: Try for at least 3 times before discarding a message (+ What happens if new field is added to MQ json ?)
// DONE: Implement kill channel for the goroutine
func apn_error_processor_status_inactive(config Configuration, conn *amqp.Connection, ApnStatusInactiveQueueName string,
	ch_custom_err chan []byte, logger *log.Logger, killApnStatusInactive, killApnStatusInactiveAck chan int, gq ApnQueue) {
	// Connect to a database
	db := autorc.New("tcp", "", config.Db.DbHost+":"+strconv.Itoa(config.Db.DbPort), config.Db.DbUser, config.Db.DbPassword, config.Db.DbDatabase)

	var upd autorc.Stmt
	err := db.PrepareOnce(&upd, gq.Queries.StatusInactive)
	if err != nil {
		failOnError(err, "Could not create prepared statement")
	}

	// Create new channel for Status Inactive
	ch, err := conn.Channel()
	failOnError(err, "Failed to open a channel")
	defer ch.Close()

	err = ch.Qos(
		1,     // prefetch count
		0,     // prefetch size
		false, // global
	)
	failOnError(err, "Failed to set QoS")

	// Connect to ApnStatusInactiveQueueName channel as consumer
	msgsStatusInactive, err := ch.Consume(
		ApnStatusInactiveQueueName, // queue
		"",    // consumer
		false, // auto-ack
		false, // exclusive
		false, // no-local
		false, // no-wait
		nil,   // args
	)
	failOnError(err, "Failed to register a consumer")

	i := 0
	payloads := make([]ApnStatusInactiveMsg, config.Db.TransactionMinCount.StatusInactive)
	for {
		select {
		case d, ok := <-msgsStatusInactive:
			if !ok {
				time.Sleep(100 * time.Millisecond)
				continue
			}
			olog(fmt.Sprintf("APN Status Inactive Received a message: %s", d.Body), config.DebugMode)

			payload := ApnStatusInactiveMsg{}
			err := json.Unmarshal(d.Body, &payload)

			if err != nil {
				logger.Printf("Unmarshal error for APN status Inactive MQ message data = %s", d.Body)
				olog(fmt.Sprintf("Unmarshal error for APN Token Update MQ message data = %s", d.Body), config.DebugMode)
			} else {
				payloads[i] = payload
				i++

				if i == config.Db.TransactionMinCount.StatusInactive {
					i = 0
					err := db.Begin(func(tr mysql.Transaction, args ...interface{}) error {
						for _, v := range payloads {
							if v.Token != "" {
								_, err := tr.Do(upd.Raw).Run(v.Token)
								if err != nil {
									return err
								}
							}
						}
						return tr.Commit()
					})
					t := time.Now()
					ts := t.Format(time.RFC3339)

					if err != nil {
						// ERROR WHILE UPDATING DB

						olog("Database Transaction Error StatusErrStatusInactiveTransaction", config.DebugMode)

						errInfo := make(map[string]interface{})
						errInfo["error"] = err.Error()
						errInfo["payloads"] = payloads
						errLog := DbLog{TimeStamp: ts, Type: StatusErrStatusInactiveTransaction, Data: errInfo}

						errLogByte, err := json.Marshal(errLog)
						if err == nil {
							ch_custom_err <- errLogByte
						} else {
							logger.Printf("Marshal error for ErrStatusInactiveTransaction")
						}
					} else {
						// SUCCESSFULLY UPDATED
						olog("Database Transaction Success StatusSuccessStatusInactiveTransaction", config.DebugMode)
						errLog := DbLog{TimeStamp: ts, Type: StatusSuccessStatusInactiveTransaction, Data: payloads}

						errLogByte, err := json.Marshal(errLog)
						if err == nil {
							ch_custom_err <- errLogByte
						} else {
							logger.Printf("Marshal error for StatusErrStatusInactiveTransaction")
						}
					}
					// For for specified time before running next query
					time.Sleep(time.Duration(config.Db.WaitTimeMs.StatusInactive) * time.Millisecond)
				}
			}
			// Acknowledge to MQ that work has been processed successfully
			d.Ack(false)
		case ack := <-killApnStatusInactive:
			olog("Killing APN Status Inactive goroutine", config.DebugMode)
			// Write to database and exit from goroutine
			if i > 0 {
				i = 0
				err := db.Begin(func(tr mysql.Transaction, args ...interface{}) error {
					for _, v := range payloads {
						if v.Token != "" {
							_, err := tr.Do(upd.Raw).Run(v.Token)
							if err != nil {
								return err
							}
						}
					}
					return tr.Commit()
				})
				if err != nil {
					olog("Database Transaction Error while exiting + StatusErrStatusInactiveTransaction", config.DebugMode)
					t := time.Now()
					ts := t.Format(time.RFC3339)
					errInfo := make(map[string]interface{})
					errInfo["error"] = err.Error()
					errInfo["payloads"] = payloads
					errLog := DbLog{TimeStamp: ts, Type: StatusErrStatusInactiveTransaction, Data: errInfo}
					errLogByte, err := json.Marshal(errLog)
					if err == nil {
						ch_custom_err <- errLogByte
					} else {
						logger.Printf("Marshal error for ErrStatusInactiveTransaction while quiting")
					}
				}
			}
			if ack == NeedAck {
				killApnStatusInactiveAck <- 1
			}

			olog("APN status inactive goroutine killed", config.DebugMode)
			// Exit from goroutine
			return
		}
	}
}
Exemple #14
0
package database

import (
	"github.com/ziutek/mymysql/autorc"
	"log"
	"os"
)

var (
	// MySQL Connection Handler
	CurtDevDb = autorc.New(db_proto, "", db_addr, db_user, db_pass, CurtDevdb_name)
	AdminDb   = autorc.New(db_proto, "", db_addr, db_user, db_pass, Admindb_name)
)

func MysqlError(err error) (ret bool) {
	ret = (err != nil)
	if ret {
		log.Println("MySQL error: ", err)
	}
	return
}

func MysqlErrExit(err error) {
	if MysqlError(err) {
		os.Exit(1)
	}
}
func saveMail() {
	var to string
	var err error
	var body string
	var redis_err error
	var length int
	redis := &redisClient{}
	db := autorc.New("tcp", "", gConfig["MYSQL_HOST"], gConfig["MYSQL_USER"], gConfig["MYSQL_PASS"], gConfig["MYSQL_DB"])
	db.Register("set names utf8")
	sql := "INSERT INTO " + gConfig["GM_MAIL_TABLE"] + " "
	sql += "(`date`, `to`, `from`, `subject`, `body`, `charset`, `mail`, `spam_score`, `hash`, `content_type`, `recipient`, `has_attach`, `ip_addr`)"
	sql += " values (NOW(), ?, ?, ?, ? , 'UTF-8' , ?, 0, ?, '', ?, 0, ?)"
	ins, sql_err := db.Prepare(sql)
	if sql_err != nil {
		logln(2, fmt.Sprintf("Sql statement incorrect: %s", sql_err))
	}
	sql = "UPDATE gm2_setting SET `setting_value` = `setting_value`+1 WHERE `setting_name`='received_emails' LIMIT 1"
	incr, sql_err := db.Prepare(sql)
	if sql_err != nil {
		logln(2, fmt.Sprintf("Sql statement incorrect: %s", sql_err))
	}

	//  receives values from the channel repeatedly until it is closed.
	for {
		client := <-SaveMailChan
		if user, _, addr_err := validateEmailData(client); addr_err != nil { // user, host, addr_err
			logln(1, fmt.Sprintln("mail_from didnt validate: %v", addr_err)+" client.mail_from:"+client.mail_from)
			// notify client that a save completed, -1 = error
			client.savedNotify <- -1
			continue
		} else {
			to = user + "@" + gConfig["GM_PRIMARY_MAIL_HOST"]
		}
		length = len(client.data)
		client.subject = mimeHeaderDecode(client.subject)
		client.hash = md5hex(to + client.mail_from + client.subject + strconv.FormatInt(time.Now().UnixNano(), 10))
		// Add extra headers
		add_head := ""
		add_head += "Delivered-To: " + to + "\r\n"
		add_head += "Received: from " + client.helo + " (" + client.helo + "  [" + client.address + "])\r\n"
		add_head += "	by " + gConfig["GSMTP_HOST_NAME"] + " with SMTP id " + client.hash + "@" +
			gConfig["GSMTP_HOST_NAME"] + ";\r\n"
		add_head += "	" + time.Now().Format(time.RFC1123Z) + "\r\n"
		// compress to save space
		client.data = compress(add_head + client.data)
		body = "gzencode"
		redis_err = redis.redisConnection()
		if redis_err == nil {
			_, do_err := redis.conn.Do("SETEX", client.hash, 3600, client.data)
			if do_err == nil {
				client.data = ""
				body = "redis"
			}
		} else {
			logln(1, fmt.Sprintf("redis: %v", redis_err))
		}
		// bind data to cursor
		ins.Bind(
			to,
			client.mail_from,
			client.subject,
			body,
			client.data,
			client.hash,
			to,
			client.address)
		// save, discard result
		_, _, err = ins.Exec()
		if err != nil {
			logln(1, fmt.Sprintf("Database error, %v %v", err))
			client.savedNotify <- -1
		} else {
			logln(1, "Email saved "+client.hash+" len:"+strconv.Itoa(length))
			_, _, err = incr.Exec()
			if err != nil {
				logln(1, fmt.Sprintf("Failed to incr count:", err))
			}
			client.savedNotify <- 1
		}
	}
}
Exemple #16
0
	"log"
	"os"
)

// Put your data connection
const (
	db_proto = "tcp"
	db_addr  = "192.168.X.XX:3306"
	db_user  = "******"
	db_pass  = "******"
	db_name  = "jail"
)

var (
	// MySQL connection handler
	db = autorc.New(db_proto, "", db_addr, db_user, db_pass, db_name)
)

// Check Error from db connection
func mysqlError(err error) (ret bool) {
	ret = (err != nil)
	if ret {
		log.Println("MySQL error:", err)
	}
	return
}

// Close connection
func mysqlErrExit(err error) {
	if mysqlError(err) {
		os.Exit(1)
Exemple #17
0
func main() {
	flag.Parse()
	config.SetConfig("config", *flag.String("config", "config.xml", "config xml file for start"))
	config.SetConfig("logfilename", *flag.String("logfilename", "/log/logfilename.log", "log file name"))
	config.SetConfig("deamon", *flag.String("deamon", "false", "need run as demo"))
	config.SetConfig("port", *flag.String("port", "8000", "http port "))
	config.SetConfig("log", *flag.String("log", "debug", "logger level "))
	config.LoadFromFile(config.GetConfigStr("config"), "global")
	if err := config.LoadFromFile(config.GetConfigStr("config"), "SdoOnlineServer"); err != nil {
		fmt.Println(err)
		return
	}
	logger, err := logging.NewTimeRotationHandler(config.GetConfigStr("logfilename"), "060102-15")
	if err != nil {
		fmt.Println(err)
		return
	}
	logger.SetLevel(logging.DEBUG)
	logging.AddHandler("SDO", logger)
	mysqlurl := config.GetConfigStr("mysql")
	if ok, err := regexp.MatchString("^mysql://.*:.*@.*/.*$", mysqlurl); ok == false || err != nil {
		logging.Error("mysql config syntax err:%s", mysqlurl)
		return
	}
	logging.Info("server starting...")
	mysqlurl = strings.Replace(mysqlurl, "mysql://", "", 1)
	mysqlurl = strings.Replace(mysqlurl, "@", ":", 1)
	mysqlurl = strings.Replace(mysqlurl, "/", ":", 1)
	mysqlurls := strings.Split(mysqlurl, ":")
	config.SetConfig("dbname", mysqlurls[4])
	db_login = mysql.New("tcp", "", mysqlurls[2]+":"+mysqlurls[3], mysqlurls[0], mysqlurls[1], mysqlurls[4])
	if err != nil {
		logging.Error("db connect error:%s", err.Error())
		return
	}
	mysqlurl = config.GetConfigStr("mysql_monitor")
	if ok, err := regexp.MatchString("^mysql://.*:.*@.*/.*$", mysqlurl); ok == false || err != nil {
		logging.Error("mysql config syntax err:%s", mysqlurl)
		return
	}
	mysqlurl = strings.Replace(mysqlurl, "mysql://", "", 1)
	mysqlurl = strings.Replace(mysqlurl, "@", ":", 1)
	mysqlurl = strings.Replace(mysqlurl, "/", ":", 1)
	mysqlurls = strings.Split(mysqlurl, ":")
	config.SetConfig("dbname", mysqlurls[4])
	db_monitor = mysql.New("tcp", "", mysqlurls[2]+":"+mysqlurls[3], mysqlurls[0], mysqlurls[1], mysqlurls[4])
	if err != nil {
		logging.Error("db connect error:%s", err.Error())
		return
	}
	mysqlurl = config.GetConfigStr("mysql_account")
	if ok, err := regexp.MatchString("^mysql://.*:.*@.*/.*$", mysqlurl); ok == false || err != nil {
		logging.Error("mysql config syntax err:%s", mysqlurl)
		return
	}
	mysqlurl = strings.Replace(mysqlurl, "mysql://", "", 1)
	mysqlurl = strings.Replace(mysqlurl, "@", ":", 1)
	mysqlurl = strings.Replace(mysqlurl, "/", ":", 1)
	mysqlurls = strings.Split(mysqlurl, ":")
	config.SetConfig("dbname", mysqlurls[4])
	db_account = mysql.New("tcp", "", mysqlurls[2]+":"+mysqlurls[3], mysqlurls[0], mysqlurls[1], mysqlurls[4])
	if err != nil {
		logging.Error("db connect error:%s", err.Error())
		return
	}
	http.HandleFunc("/online", OnlineServer)
	http.HandleFunc("/online/country", OnlineCountryServer)
	http.HandleFunc("/gjia/maxlevel", HandleMaxlevel)
	http.HandleFunc("/gjia/isprecreate", HandleIsPrecreate)
	http.HandleFunc("/gjia/isonline", HandleIsOnline)
	http.HandleFunc("/card/gonghuiuser", HandleGonghuiUserCard)
	http.HandleFunc("/card/gonghuikey", HandleGonghuiKeyCard)
	err = http.ListenAndServe(":"+config.GetConfigStr("port"), nil)
	if err != nil {
		logging.Error("ListenAndServe:%s", err.Error())
	}
	logging.Info("server stop...")
}
Exemple #18
0
func InitCheckNameDatabase(addr, name, password, dbname string) {
	db_checkname = mysql.New("tcp", "", addr, name, password, dbname)
}
func main() {
	user := "******"
	passwd := "TestPasswd9"
	dbname := "test"
	//conn := []string{"unix", "", "/var/run/mysqld/mysqld.sock"}
	conn := []string{"tcp", "", "127.0.0.1:3306"}

	c := autorc.New(conn[0], conn[1], conn[2], user, passwd)

	// Register initialisation commands
	c.Raw.Register("set names utf8")

	// my is in unconnected state
	checkErr(c.Use(dbname))

	// Now we ar connected - disconnect
	c.Raw.Close()

	// Drop test table if exists
	_, _, err := c.Query("drop table R")

	fmt.Println("You may restart MySQL sererr or down the network interface.")
	sec := 9
	fmt.Printf("Waiting %ds...", sec)
	for sec--; sec >= 0; sec-- {
		time.Sleep(1e9)
		fmt.Printf("\b\b\b\b\b%ds...", sec)
	}
	fmt.Println()

	// Create table
	_, _, err = c.Query(
		"create table R (id int primary key, name varchar(20))",
	)
	checkErr(err)

	// Kill the connection
	_, _, err = c.Query("kill %d", c.Raw.ThreadId())
	checkErr(err)

	// Prepare insert statement
	ins, err := c.Prepare("insert R values (?,  ?)")
	checkErr(err)

	// Kill the connection
	_, _, err = c.Query("kill %d", c.Raw.ThreadId())
	checkErr(err)

	// Bind insert parameters
	ins.Raw.Bind(1, "jeden")
	// Insert into table
	_, _, err = ins.Exec()
	checkErr(err)

	// Kill the connection
	_, _, err = c.Query("kill %d", c.Raw.ThreadId())
	checkErr(err)

	// Bind insert parameters
	ins.Raw.Bind(2, "dwa")
	// Insert into table
	_, _, err = ins.Exec()
	checkErr(err)

	// Kill the connection
	_, _, err = c.Query("kill %d", c.Raw.ThreadId())
	checkErr(err)

	// Select from table
	rows, res, err := c.Query("select * from R")
	checkErr(err)
	id := res.Map("id")
	name := res.Map("name")
	if len(rows) != 2 ||
		rows[0].Int(id) != 1 || rows[0].Str(name) != "jeden" ||
		rows[1].Int(id) != 2 || rows[1].Str(name) != "dwa" {
		fmt.Println("Bad result")
	}

	// Kill the connection
	_, _, err = c.Query("kill %d", c.Raw.ThreadId())
	checkErr(err)

	// Drop table
	_, _, err = c.Query("drop table R")
	checkErr(err)

	// Disconnect
	c.Raw.Close()

}