92 lines
2 KiB
Go
92 lines
2 KiB
Go
// 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"
|
|
"os"
|
|
"os/signal"
|
|
"runtime"
|
|
"runtime/pprof"
|
|
|
|
log "github.com/golang/glog"
|
|
|
|
"github.com/chihaya/chihaya/config"
|
|
_ "github.com/chihaya/chihaya/drivers/backend/mock"
|
|
_ "github.com/chihaya/chihaya/drivers/tracker/mock"
|
|
"github.com/chihaya/chihaya/server"
|
|
)
|
|
|
|
var (
|
|
profile bool
|
|
configPath string
|
|
)
|
|
|
|
func init() {
|
|
flag.BoolVar(&profile, "profile", false, "Generate profiling data for pprof into ./chihaya.cpu")
|
|
flag.StringVar(&configPath, "config", "", "Provide the filesystem path of a valid configuration file.")
|
|
}
|
|
|
|
func main() {
|
|
flag.Parse()
|
|
runtime.GOMAXPROCS(runtime.NumCPU())
|
|
|
|
// Enable the profile if flagged.
|
|
if profile {
|
|
f, err := os.Create("chihaya.cpu")
|
|
if err != nil {
|
|
log.Fatalf("chihaya: failed to create profile file: %s\n", err)
|
|
}
|
|
defer f.Close()
|
|
|
|
pprof.StartCPUProfile(f)
|
|
log.Info("chihaya: started profiling")
|
|
}
|
|
|
|
// Load the config file.
|
|
conf, err := config.Open(configPath)
|
|
if err != nil {
|
|
log.Fatalf("chihaya: failed to parse configuration file: %s\n", err)
|
|
}
|
|
log.Infoln("chihaya: succesfully loaded config")
|
|
|
|
// Create a new server.
|
|
s, err := server.New(conf)
|
|
if err != nil {
|
|
log.Fatalf("chihaya: failed to create server: %s\n", err)
|
|
}
|
|
|
|
// Spawn a goroutine to handle interrupts and safely shut down.
|
|
go func() {
|
|
interrupts := make(chan os.Signal, 1)
|
|
signal.Notify(interrupts, os.Interrupt)
|
|
|
|
<-interrupts
|
|
log.Info("chihaya: caught interrupt, shutting down...")
|
|
|
|
if profile {
|
|
pprof.StopCPUProfile()
|
|
log.Info("chihaya: stopped profiling")
|
|
}
|
|
|
|
err := s.Stop()
|
|
if err != nil {
|
|
log.Fatalf("chihaya: failed to shutdown cleanly: %s", err)
|
|
}
|
|
|
|
log.Info("chihaya: shutdown cleanly")
|
|
|
|
<-interrupts
|
|
|
|
log.Flush()
|
|
os.Exit(0)
|
|
}()
|
|
|
|
// Start the server listening and handling requests.
|
|
err = s.ListenAndServe()
|
|
if err != nil {
|
|
log.Fatalf("chihaya: failed to start server: %s\n", err)
|
|
}
|
|
}
|