ripple-api/main.go

95 lines
2.0 KiB
Go
Raw Permalink Normal View History

2016-04-03 17:59:27 +00:00
package main
import (
"database/sql"
2016-04-07 17:32:48 +00:00
"fmt"
2016-04-03 17:59:27 +00:00
"log"
2016-04-07 17:32:48 +00:00
"net"
"net/http"
"syscall"
"time"
2016-04-03 17:59:27 +00:00
2016-04-19 14:07:27 +00:00
"git.zxq.co/ripple/rippleapi/app"
"git.zxq.co/ripple/rippleapi/common"
2016-05-22 15:11:07 +00:00
"git.zxq.co/ripple/schiavolib"
2016-04-19 14:07:27 +00:00
"github.com/rcrowley/goagain"
2016-04-03 17:59:27 +00:00
// Golint pls dont break balls
_ "github.com/go-sql-driver/mysql"
)
2016-04-07 17:32:48 +00:00
func init() {
log.SetFlags(log.Ltime)
log.SetPrefix(fmt.Sprintf("%d|", syscall.Getpid()))
}
2016-04-03 17:59:27 +00:00
func main() {
conf, halt := common.Load()
if halt {
return
}
2016-05-22 15:11:07 +00:00
schiavo.Prefix = "Ripple API"
2016-04-03 17:59:27 +00:00
db, err := sql.Open(conf.DatabaseType, conf.DSN)
if err != nil {
2016-05-22 15:11:07 +00:00
schiavo.Bunker.Send(err.Error())
log.Fatalln(err)
2016-04-03 17:59:27 +00:00
}
2016-04-07 17:32:48 +00:00
engine := app.Start(conf, db)
// Inherit a net.Listener from our parent process or listen anew.
l, err := goagain.Listener()
if nil != err {
// Listen on a TCP or a UNIX domain socket (TCP here).
if conf.Unix {
l, err = net.Listen("unix", conf.ListenTo)
} else {
l, err = net.Listen("tcp", conf.ListenTo)
}
if nil != err {
2016-05-22 15:11:07 +00:00
schiavo.Bunker.Send(err.Error())
2016-04-07 17:32:48 +00:00
log.Fatalln(err)
}
2016-05-22 15:23:44 +00:00
schiavo.Bunker.Send(fmt.Sprint("LISTENINGU STARTUATO ON ", l.Addr()))
2016-04-07 17:32:48 +00:00
// Accept connections in a new goroutine.
go http.Serve(l, engine)
} else {
// Resume accepting connections in a new goroutine.
2016-05-22 15:23:44 +00:00
schiavo.Bunker.Send(fmt.Sprint("LISTENINGU RESUMINGU ON ", l.Addr()))
2016-04-07 17:32:48 +00:00
go http.Serve(l, engine)
// Kill the parent, now that the child has started successfully.
if err := goagain.Kill(); nil != err {
2016-05-22 15:11:07 +00:00
schiavo.Bunker.Send(err.Error())
2016-04-07 17:32:48 +00:00
log.Fatalln(err)
}
}
// Block the main goroutine awaiting signals.
if _, err := goagain.Wait(l); nil != err {
2016-05-22 15:11:07 +00:00
schiavo.Bunker.Send(err.Error())
2016-04-07 17:32:48 +00:00
log.Fatalln(err)
}
// Do whatever's necessary to ensure a graceful exit like waiting for
// goroutines to terminate or a channel to become closed.
//
// In this case, we'll simply stop listening and wait one second.
if err := l.Close(); nil != err {
2016-05-22 15:11:07 +00:00
schiavo.Bunker.Send(err.Error())
2016-04-07 17:32:48 +00:00
log.Fatalln(err)
}
if err := db.Close(); err != nil {
2016-05-22 15:11:07 +00:00
schiavo.Bunker.Send(err.Error())
2016-04-07 17:32:48 +00:00
log.Fatalln(err)
}
time.Sleep(time.Second * 1)
2016-04-03 17:59:27 +00:00
}