2013-06-22 01:31:32 +02:00
|
|
|
// Copyright 2013 The Chihaya Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by the BSD 2-Clause license,
|
|
|
|
// which can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"runtime"
|
|
|
|
"runtime/pprof"
|
|
|
|
|
2013-06-22 03:43:11 +02:00
|
|
|
"github.com/pushrax/chihaya/config"
|
|
|
|
"github.com/pushrax/chihaya/server"
|
2013-09-07 00:39:14 +02:00
|
|
|
|
|
|
|
_ "github.com/pushrax/chihaya/cache/redis"
|
2013-09-07 01:13:44 +02:00
|
|
|
_ "github.com/pushrax/chihaya/storage/batter"
|
2013-09-07 00:39:14 +02:00
|
|
|
_ "github.com/pushrax/chihaya/storage/gazelle"
|
2013-06-22 01:31:32 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
profile bool
|
2013-06-23 09:56:28 +02:00
|
|
|
configPath string
|
2013-06-22 01:31:32 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
flag.BoolVar(&profile, "profile", false, "Generate profiling data for pprof into chihaya.cpu")
|
2013-06-23 09:56:28 +02:00
|
|
|
flag.StringVar(&configPath, "config", "", "The location of a valid configuration file.")
|
2013-06-22 01:31:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
runtime.GOMAXPROCS(runtime.NumCPU())
|
|
|
|
|
|
|
|
if profile {
|
|
|
|
log.Println("Running with profiling enabled")
|
|
|
|
f, err := os.Create("chihaya.cpu")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to create profile file: %s\n", err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
pprof.StartCPUProfile(f)
|
|
|
|
}
|
|
|
|
|
2013-06-23 09:56:28 +02:00
|
|
|
if configPath == "" {
|
|
|
|
log.Fatalf("Must specify a configuration file")
|
|
|
|
}
|
2013-07-05 12:50:52 +02:00
|
|
|
conf, err := config.Open(configPath)
|
2013-06-23 09:56:28 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to parse configuration file: %s\n", err)
|
|
|
|
}
|
2013-06-24 06:18:59 +02:00
|
|
|
s, err := server.New(conf)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to create server: %s\n", err)
|
|
|
|
}
|
2013-06-22 01:31:32 +02:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
c := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(c, os.Interrupt)
|
|
|
|
<-c
|
|
|
|
|
|
|
|
if profile {
|
|
|
|
pprof.StopCPUProfile()
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("Caught interrupt, shutting down..")
|
|
|
|
err := s.Stop()
|
|
|
|
if err != nil {
|
|
|
|
panic("Failed to shutdown cleanly")
|
|
|
|
}
|
|
|
|
log.Println("Shutdown successfully")
|
|
|
|
<-c
|
|
|
|
os.Exit(0)
|
|
|
|
}()
|
|
|
|
|
2013-06-23 09:56:28 +02:00
|
|
|
err = s.ListenAndServe()
|
2013-06-22 01:31:32 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to start server: %s\n", err)
|
|
|
|
}
|
|
|
|
}
|