-
Notifications
You must be signed in to change notification settings - Fork 14
/
main.go
87 lines (82 loc) · 2.04 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
"code.google.com/p/go.net/websocket"
"flag"
"fmt"
"log"
"net/http"
"net/http/pprof"
"os"
"runtime"
)
var (
Log *log.Logger
Conf *Config
)
func init() {
Log = log.New(os.Stdout, "", log.LstdFlags|log.Lshortfile)
}
func main() {
var err error
// parse cmd-line arguments
flag.Parse()
// init config
Conf, err = InitConfig(ConfFile)
if err != nil {
Log.Printf("initConfig(\"%s\") failed (%s)", ConfFile, err.Error())
return
}
// init log
if Conf.Log != "" {
f, err := os.OpenFile(Conf.Log, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
Log.Printf("os.OpenFile(\"%s\", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644) failed (%s)", Conf.Log, err.Error())
return
}
defer f.Close()
Log = log.New(f, "", log.LstdFlags|log.Lshortfile)
}
// Set max routine
runtime.GOMAXPROCS(Conf.MaxProcs)
// init redis
InitRedis()
// for test client
http.HandleFunc("/client", Client)
// sub
http.Handle("/sub", websocket.Handler(Subscribe))
Log.Printf("gopush service start.")
// pprof
if Conf.Pprof == 1 {
if Conf.PprofAddr != Conf.Addr || Conf.PprofPort != Conf.Port {
go func() {
profServeMux := http.NewServeMux()
profServeMux.HandleFunc("/debug/pprof/", pprof.Index)
profServeMux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
profServeMux.HandleFunc("/debug/pprof/profile", pprof.Profile)
profServeMux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
err := http.ListenAndServe(fmt.Sprintf("%s:%d", Conf.PprofAddr, Conf.PprofPort), profServeMux)
if err != nil {
panic(err)
}
}()
}
}
// publish
if Conf.PubAddr != Conf.Addr || Conf.PubPort != Conf.Port {
go func() {
pubServeMux := http.NewServeMux()
pubServeMux.HandleFunc("/pub", Publish)
err := http.ListenAndServe(fmt.Sprintf("%s:%d", Conf.PubAddr, Conf.PubPort), pubServeMux)
if err != nil {
panic(err)
}
}()
} else {
http.HandleFunc("/pub", Publish)
}
// start listen and pending here
if err = Listen(); err != nil {
Log.Printf("Listen() failed (%s)", err.Error())
return
}
}