proxymysql/main.go

67 lines
1.4 KiB
Go

package main
import (
"flag"
"log"
"net"
"proxymysql/app/conf"
"proxymysql/app/db"
"proxymysql/app/mysqlserver"
"proxymysql/app/webserver"
"proxymysql/app/zlog"
)
func main() {
flag.StringVar(&conf.App.RemoteDb, "remote_db", "", "")
flag.StringVar(&conf.App.ServerAddr, "server_addr", ":5306", "")
flag.StringVar(&conf.App.WebAddr, "web_addr", "", "")
flag.StringVar(&conf.App.ServerVersion, "server_version", "8.0.30-my-mysql-proxy", "")
flag.BoolVar(&conf.App.SaveLog, "save_log", false, "")
flag.StringVar(&conf.App.LogLevel, "log_level", zlog.InfoLevel, "日志级别 debug info error")
flag.Parse()
cfg := zlog.DefaultConfig
cfg.Level = conf.App.LogLevel
zlog.Init("app", cfg)
if conf.App.RemoteDb == "" {
zlog.Fatal("remote db addr not set")
}
zlog.Infof("save query log: %v", conf.App.SaveLog)
zlog.Infof("server version: %s", conf.App.ServerVersion)
zlog.Infof("remote db: %s", conf.App.RemoteDb)
if conf.App.SaveLog {
db.InitAdminDb()
}
// todb
if conf.App.WebAddr != "" {
go webserver.Start()
}
listen, err := net.Listen("tcp", conf.App.ServerAddr)
if err != nil {
log.Fatal(err)
}
zlog.Infof("db server listen on: %s", conf.App.ServerAddr)
for {
conn, err := listen.Accept()
if err != nil {
log.Fatal(err)
}
go func(conn2 net.Conn) {
defer conn2.Close()
err := mysqlserver.NewProxyConn(conn2).Handle()
if err != nil {
zlog.Errorf("proxy conn handle err: %s", err)
}
}(conn)
}
}