func initSvc() (err error) { if err = ilog.Open(ServerConfig.LogFile); err != nil { return err } // Init randdom generater rand.Seed(time.Now().UnixNano()) err = mc.Init(ServerConfig.MC.Host, ServerConfig.MC.Port, "mc") if err != nil { ilog.Error("mc - init fail! " + err.Error()) } err = nosql.Init(ServerConfig.Nosql.Host, ServerConfig.Nosql.Port, "nosql") if err != nil { ilog.Error("nosql - init fail! " + err.Error()) } err = db.Init(ServerConfig.DB.Host, ServerConfig.DB.Port, ServerConfig.DB.User, ServerConfig.DB.Passwd, ServerConfig.DB.Charset, ServerConfig.DB.Name) if err != nil { ilog.Error("db - init fail! " + err.Error()) } return err }
func startServer() error { index := func(w http.ResponseWriter, r *http.Request) { err := mc.Set("PING", "x") if err != nil { ilog.Error("redis - " + err.Error()) } else { io.WriteString(w, fmt.Sprintf("%s", "ok")) } } http.HandleFunc("/", index) http.ListenAndServe(":8081", nil) return nil }
func main() { parseFlag() if showVersion { println("version 1.0.0") os.Exit(0) } if err := loadConfig(); err != nil { println("config - ", err.Error()) os.Exit(0) } if toDaemon { if err := daemon(); err != nil { println("daemon - ", err.Error()) os.Exit(0) } } runtime.GOMAXPROCS(runtime.NumCPU() * 2) go signalHandle() if toProfile { prof.StartProf() } if err := initSvc(); err != nil { println("init - ", err.Error()) os.Exit(1) } if err := outputPid(ServerConfig.PidFile); err != nil { ilog.Error("pid - " + err.Error()) os.Exit(0) } ilog.Rinfo("launch ok") err := startServer() if err != nil { println("start - ", err.Error()) } os.Exit(1) }