优化:配置文件管理、启动参数解析等
git-svn-id: svn://47.119.165.148/zhub@167 e63fbceb-bcc3-4977-ac22-735b83d8d0f4
This commit is contained in:
133
zsub/config.go
133
zsub/config.go
@@ -1,133 +0,0 @@
|
||||
package zsub
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
dir, _ = os.Getwd()
|
||||
config = make(map[string]string)
|
||||
LogDebug bool
|
||||
datadir = dir + "/data"
|
||||
)
|
||||
|
||||
func LoadConf(path string) {
|
||||
//log.Println("APP_CONF =", path)
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
log.Panicln(err)
|
||||
}
|
||||
|
||||
reader := bufio.NewReader(f)
|
||||
space := ""
|
||||
for {
|
||||
bytes, err := reader.ReadBytes('\n')
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
line := string(bytes)
|
||||
line = strings.Trim(line, " \r\n")
|
||||
if len(line) == 0 {
|
||||
continue
|
||||
}
|
||||
if strings.Contains(line, "#") {
|
||||
line = line[0:strings.Index(line, "#")]
|
||||
}
|
||||
|
||||
switch {
|
||||
case strings.EqualFold(line, ""):
|
||||
case strings.Index(line, "[") == 0 && strings.Index(line, "]") > 0:
|
||||
space = line[1:strings.Index(line, "]")]
|
||||
space = strings.Trim(space, " ")
|
||||
case strings.Index(line, "=") > 0:
|
||||
arr := strings.Split(line, "=")
|
||||
if len(arr) < 2 {
|
||||
continue
|
||||
}
|
||||
|
||||
config[space+"."+strings.Trim(arr[0], " ")] = strings.Trim(arr[1], " ")
|
||||
default:
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
LogDebug = strings.EqualFold(config["log.level"], "debug")
|
||||
|
||||
datadir = GetStr("data.dir", "${APP_HOME}/data")
|
||||
datadir = strings.ReplaceAll(datadir, "${APP_HOME}", dir)
|
||||
|
||||
os.MkdirAll(datadir, os.ModeDir)
|
||||
os.Chmod(datadir, 0777)
|
||||
|
||||
initLog()
|
||||
}
|
||||
|
||||
func GetStr(key string, def string) string {
|
||||
if len(config[key]) == 0 {
|
||||
return def
|
||||
}
|
||||
return config[key]
|
||||
}
|
||||
|
||||
func GetInt(key string, def int) int {
|
||||
if len(config[key]) == 0 {
|
||||
return def
|
||||
}
|
||||
n, err := strconv.Atoi(config[key])
|
||||
if err != nil {
|
||||
log.Println(err, "return def;")
|
||||
return def
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
func initLog() {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
log.Println("initLog Err:", r)
|
||||
}
|
||||
}()
|
||||
|
||||
file, err := os.OpenFile("zhub.log", os.O_CREATE|os.O_APPEND|os.O_SYNC|os.O_RDWR, 0777)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
log.SetOutput(file)
|
||||
|
||||
/*
|
||||
if strings.EqualFold(GetStr("log.handlers", "console"), "console") {
|
||||
return
|
||||
}
|
||||
|
||||
var logfile = GetStr("log.pattern", "${APP_HOME}/logs-200601/log-20060102.log")
|
||||
|
||||
c := cron.New()
|
||||
fun := func() {
|
||||
now := time.Now()
|
||||
logfile := strings.ReplaceAll(logfile, "${APP_HOME}", dir)
|
||||
logfile = now.Format(logfile)
|
||||
|
||||
if strings.LastIndexAny(logfile, "/") > 0 {
|
||||
logdir := logfile[0:strings.LastIndexAny(logfile, "/")]
|
||||
os.MkdirAll(logdir, 0666)
|
||||
}
|
||||
|
||||
file, err := os.OpenFile(logfile, os.O_CREATE|os.O_APPEND|os.O_SYNC|os.O_RDWR, 0777)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
|
||||
//log.Println("SET LOG_FILE =", file.Name())
|
||||
log.SetOutput(file)
|
||||
}
|
||||
fun()
|
||||
|
||||
c.AddFunc("0 0 * * * *", fun)
|
||||
go c.Run()
|
||||
*/
|
||||
}
|
||||
@@ -23,7 +23,7 @@ func StartWatch() {
|
||||
http.HandleFunc("/retimer", retimer)
|
||||
http.HandleFunc("/topic/publish", publish)
|
||||
|
||||
watchAddr := GetStr("service.zhub.watch", "0.0.0.0:1217")
|
||||
watchAddr := Conf.Service.Watch
|
||||
log.Println("zhub.watch = ", watchAddr)
|
||||
http.ListenAndServe(watchAddr, nil)
|
||||
}
|
||||
|
||||
@@ -29,10 +29,30 @@ func msgAccept(v Message) {
|
||||
return
|
||||
}
|
||||
|
||||
if LogDebug {
|
||||
log.Printf("[%d] rcmd: %s\n", v.Conn.sn, strings.Join(rcmd, " "))
|
||||
if Conf.Log.Level == "debug" && rcmd[0] != "auth" {
|
||||
log.Printf("[%d] cmd: %s\n", v.Conn.sn, strings.Join(rcmd, " "))
|
||||
} else if rcmd[0] == "auth" {
|
||||
if len(rcmd) != 2 || strings.IndexAny(rcmd[1], "@") == -1 {
|
||||
c.send("-Error: invalid password!")
|
||||
return
|
||||
}
|
||||
|
||||
inx := strings.IndexAny(rcmd[1], "@") //user@pwd
|
||||
|
||||
authKey := rcmd[1][:inx] //user
|
||||
authValue := Conf.Auth[rcmd[1][:inx]] //pwd
|
||||
if strings.EqualFold(authValue, rcmd[1][inx+1:]) {
|
||||
c.auth = rcmd[1][:inx]
|
||||
c.send("+Auth: ok!")
|
||||
log.Printf("[%d] cmd: %s\n", v.Conn.sn, "auth "+authKey+"@******* "+"[OK]")
|
||||
} else {
|
||||
c.send("-Auth: invalid password!")
|
||||
log.Printf("[%d] cmd: %s\n", v.Conn.sn, "auth "+authKey+"@******* "+"[Error]")
|
||||
}
|
||||
return
|
||||
}
|
||||
if strings.TrimSpace(c.auth) == "" && !strings.EqualFold("auth", rcmd[0]) && strings.EqualFold(GetStr("service.auth", "0"), "1") {
|
||||
|
||||
if strings.TrimSpace(c.auth) == "" && rcmd[0] != "auth" && Conf.Service.Auth {
|
||||
c.send("-Auth: NOAUTH Authentication required:" + rcmd[0])
|
||||
return
|
||||
}
|
||||
@@ -153,21 +173,22 @@ func msgAccept(v Message) {
|
||||
return
|
||||
}
|
||||
zsub._unlock(Lock{key: rcmd[1], uuid: rcmd[2]})
|
||||
case "auth":
|
||||
if len(rcmd) != 2 || strings.IndexAny(rcmd[1], "@") == -1 {
|
||||
c.send("-Error: invalid password!")
|
||||
return
|
||||
}
|
||||
|
||||
inx := strings.IndexAny(rcmd[1], "@") //user@pwd
|
||||
|
||||
if strings.EqualFold(GetStr("auth."+rcmd[1][:inx], ""), rcmd[1][inx+1:]) {
|
||||
c.auth = rcmd[1][:inx]
|
||||
c.send("+Auth: ok!")
|
||||
} else {
|
||||
c.send("-Auth: invalid password!")
|
||||
}
|
||||
/*case "auth":
|
||||
if len(rcmd) != 2 || strings.IndexAny(rcmd[1], "@") == -1 {
|
||||
c.send("-Error: invalid password!")
|
||||
return
|
||||
}
|
||||
|
||||
inx := strings.IndexAny(rcmd[1], "@") //user@pwd
|
||||
|
||||
authKey := Conf.Auth[rcmd[1][:inx]]
|
||||
if strings.EqualFold(authKey, rcmd[1][inx+1:]) {
|
||||
c.auth = rcmd[1][:inx]
|
||||
c.send("+Auth: ok!")
|
||||
} else {
|
||||
c.send("-Auth: invalid password!")
|
||||
}
|
||||
return*/
|
||||
default:
|
||||
c.send("-Error: default not supported:[" + strings.Join(rcmd, " ") + "]")
|
||||
return
|
||||
|
||||
13
zsub/zdb.go
13
zsub/zdb.go
@@ -64,16 +64,19 @@ func (s *ZSub) dataStorage() {
|
||||
fmt.Println(err)
|
||||
}
|
||||
defer file.Close()
|
||||
writer := bufio.NewWriter(file)
|
||||
delays2 := s.delays
|
||||
|
||||
for _, delay := range delays2 {
|
||||
writer.WriteString(delay.topic)
|
||||
writer := bufio.NewWriter(file)
|
||||
_delays := s.delays
|
||||
|
||||
for _, delay := range _delays {
|
||||
delayStr := fmt.Sprintf("%s %s %d\n", delay.topic, delay.value, delay.exectime.Unix())
|
||||
writer.WriteString(delayStr)
|
||||
/*writer.WriteString(delay.topic)
|
||||
writer.WriteString(" ")
|
||||
writer.WriteString(delay.value)
|
||||
writer.WriteString(" ")
|
||||
writer.WriteString(strconv.FormatInt(delay.exectime.Unix(), 10))
|
||||
writer.WriteString("\n")
|
||||
writer.WriteString("\n")*/
|
||||
}
|
||||
writer.Flush()
|
||||
}()
|
||||
|
||||
14
zsub/zsub.go
14
zsub/zsub.go
@@ -12,10 +12,13 @@ import (
|
||||
"sync/atomic"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
"zhub/internal/config"
|
||||
)
|
||||
|
||||
var (
|
||||
zsub = &ZSub{
|
||||
Conf config.Config
|
||||
datadir string
|
||||
zsub = &ZSub{
|
||||
topics: make(map[string]*ZTopic),
|
||||
timers: make(map[string]*ZTimer),
|
||||
delays: make(map[string]*ZDelay),
|
||||
@@ -55,7 +58,7 @@ func init() {
|
||||
|
||||
// close
|
||||
for _, c := range conns {
|
||||
log.Println("========================================= conn ping close:", (*c.conn).RemoteAddr(), "[", c.groupid, "] =========================================")
|
||||
log.Printf("========================================= conn ping close:%s [%d] =========================================\n", (*c.conn).RemoteAddr(), c.sn)
|
||||
c.close()
|
||||
}
|
||||
|
||||
@@ -250,7 +253,10 @@ StartServer
|
||||
1、load history data
|
||||
2、init server
|
||||
*/
|
||||
func StartServer(addr string) {
|
||||
func StartServer(addr string, conf config.Config) {
|
||||
Conf = conf
|
||||
datadir = conf.Data.Dir
|
||||
|
||||
go func() {
|
||||
for {
|
||||
fun, ok := <-funChan
|
||||
@@ -281,7 +287,7 @@ func StartServer(addr string) {
|
||||
}
|
||||
zConn := NewZConn(&conn)
|
||||
|
||||
log.Println("conn start:", conn.RemoteAddr(), "[", zConn.sn, "]")
|
||||
log.Printf("conn start: %s [%d]\n", conn.RemoteAddr(), zConn.sn)
|
||||
go zsub.acceptHandler(zConn)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -99,10 +99,14 @@ func (s *ZSub) timer(rcmd []string, c *ZConn) {
|
||||
|
||||
func (s *ZSub) ReloadTimer() {
|
||||
db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8",
|
||||
GetStr("ztimer.db.user", "root"),
|
||||
Conf.Ztimer.Db.User,
|
||||
Conf.Ztimer.Db.Password,
|
||||
Conf.Ztimer.Db.Addr,
|
||||
Conf.Ztimer.Db.Database,
|
||||
/*GetStr("ztimer.db.user", "root"),
|
||||
GetStr("ztimer.db.pwd", "123456"),
|
||||
GetStr("ztimer.db.addr", "127.0.0.1:3306"),
|
||||
GetStr("ztimer.db.database", "zhub"),
|
||||
GetStr("ztimer.db.database", "zhub"),*/
|
||||
))
|
||||
|
||||
if err != nil {
|
||||
|
||||
@@ -23,10 +23,10 @@ func (t *ZTopic) init() {
|
||||
break
|
||||
}
|
||||
|
||||
for name, group := range t.groups {
|
||||
for groupName, group := range t.groups {
|
||||
// zgroup chan overload check
|
||||
if len(group.chMsg) == cap(group.chMsg) {
|
||||
log.Println(fmt.Sprintf("zgroup no cap: [%s.%s %s]", name, t.topic, msg))
|
||||
log.Println(fmt.Sprintf("zgroup no cap: [%s.%s %s]", groupName, t.topic, msg))
|
||||
continue
|
||||
}
|
||||
group.chMsg <- msg
|
||||
|
||||
Reference in New Issue
Block a user