ripple-api/main.go

86 lines
1.7 KiB
Go
Raw 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
// Golint pls dont break balls
_ "github.com/go-sql-driver/mysql"
"github.com/osuripple/api/app"
"github.com/osuripple/api/common"
2016-04-07 17:32:48 +00:00
"github.com/rcrowley/goagain"
2016-04-03 17:59:27 +00:00
)
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
}
db, err := sql.Open(conf.DatabaseType, conf.DSN)
if err != nil {
log.Fatal(err)
}
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 {
log.Fatalln(err)
}
log.Println("LISTENINGU STARTUATO ON", l.Addr())
// Accept connections in a new goroutine.
go http.Serve(l, engine)
} else {
// Resume accepting connections in a new goroutine.
log.Println("LISTENINGU RESUMINGU ON", l.Addr())
go http.Serve(l, engine)
// Kill the parent, now that the child has started successfully.
if err := goagain.Kill(); nil != err {
log.Fatalln(err)
}
}
// Block the main goroutine awaiting signals.
if _, err := goagain.Wait(l); nil != err {
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 {
log.Fatalln(err)
}
if err := db.Close(); err != nil {
log.Fatalln(err)
}
time.Sleep(time.Second * 1)
2016-04-03 17:59:27 +00:00
}