2014-07-08 10:59:16 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/stvp/rollbar"
|
|
|
|
logpkg "log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"runtime"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
Message []byte
|
|
|
|
Key []byte
|
|
|
|
)
|
|
|
|
|
|
|
|
func NewKey(queue string, index uint) Key {
|
|
|
|
istr := strconv.FormatUint(uint64(index), 10)
|
|
|
|
key := strings.Join([]string{queue, istr}, "_")
|
|
|
|
return Key(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2014-07-10 12:19:39 +00:00
|
|
|
log *logpkg.Logger
|
2014-07-08 10:59:16 +00:00
|
|
|
)
|
|
|
|
|
2014-07-10 12:19:39 +00:00
|
|
|
func HandleShutdown() {
|
|
|
|
ch := make(chan os.Signal)
|
|
|
|
signal.Notify(ch, os.Interrupt, os.Kill, syscall.SIGTERM, syscall.SIGINT)
|
2014-07-08 10:59:16 +00:00
|
|
|
|
|
|
|
go func() {
|
2014-07-10 12:19:39 +00:00
|
|
|
<-ch
|
2014-07-08 10:59:16 +00:00
|
|
|
SaveState()
|
|
|
|
log.Printf("State successfully persisted")
|
|
|
|
storage.Close()
|
|
|
|
rollbar.Wait()
|
|
|
|
log.Println("Storage closed")
|
|
|
|
log.Printf("Server stopped")
|
2014-07-10 12:19:39 +00:00
|
|
|
os.Exit(1)
|
2014-07-08 10:59:16 +00:00
|
|
|
}()
|
2014-07-10 12:19:39 +00:00
|
|
|
}
|
2014-07-08 10:59:16 +00:00
|
|
|
|
2014-07-10 12:19:39 +00:00
|
|
|
func main() {
|
|
|
|
log = logpkg.New(os.Stdout, "", logpkg.Ldate|logpkg.Lmicroseconds)
|
2014-07-08 10:59:16 +00:00
|
|
|
|
2014-07-10 12:19:39 +00:00
|
|
|
rollbar.Token = "***REMOVED***" // klit access token
|
|
|
|
rollbar.Environment = cfg.Env
|
|
|
|
|
|
|
|
SetupConfig()
|
|
|
|
SetupStorage()
|
|
|
|
SetupServer()
|
|
|
|
HandleShutdown()
|
|
|
|
LoadState()
|
2014-07-08 10:59:16 +00:00
|
|
|
go KeepStatePersisted()
|
|
|
|
go PersistMessages()
|
|
|
|
|
|
|
|
log.Printf("GOMAXPROCS = %d", runtime.GOMAXPROCS(-1))
|
2014-07-10 12:19:39 +00:00
|
|
|
log.Printf("Starting HTTP server on port %d", cfg.Port)
|
2014-07-08 10:59:16 +00:00
|
|
|
|
2014-07-10 12:19:39 +00:00
|
|
|
http.ListenAndServe(cfg.PortString(), nil)
|
2014-07-08 10:59:16 +00:00
|
|
|
}
|