rove/main.go

65 lines
1.2 KiB
Go
Raw Normal View History

2020-05-29 16:51:13 +01:00
package main
2020-05-29 17:41:11 +01:00
import (
"flag"
"fmt"
"os"
"os/signal"
"syscall"
"github.com/mdiluz/rove/pkg/persistence"
"github.com/mdiluz/rove/pkg/server"
2020-06-02 15:54:22 +01:00
"github.com/mdiluz/rove/pkg/version"
2020-05-29 17:41:11 +01:00
)
2020-05-29 16:51:13 +01:00
2020-06-02 15:54:22 +01:00
var ver = flag.Bool("version", false, "Display version number")
var port = flag.Int("port", 8080, "The port to host on")
var data = flag.String("data", os.TempDir(), "Directory to store persistant data")
2020-05-29 16:51:13 +01:00
func main() {
2020-06-01 18:10:25 +01:00
flag.Parse()
2020-06-03 17:29:56 +01:00
// Print the version if requested
2020-06-02 15:54:22 +01:00
if *ver {
fmt.Println(version.Version)
os.Exit(0)
}
2020-06-03 17:29:56 +01:00
fmt.Printf("Initialising version %s...\n", version.Version)
// Set the persistence path
persistence.SetPath(*data)
2020-06-03 17:29:56 +01:00
// Create the server data
s := server.NewServer(
server.OptionPort(*port),
server.OptionPersistentData())
2020-05-29 17:41:11 +01:00
2020-06-03 17:29:56 +01:00
// Initialise the server
if err := s.Initialise(); err != nil {
panic(err)
}
// Set up the close handler
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
fmt.Println("SIGTERM recieved, exiting...")
2020-06-03 17:29:56 +01:00
if err := s.Close(); err != nil {
panic(err)
}
os.Exit(0)
}()
2020-06-03 17:29:56 +01:00
fmt.Println("Running...")
2020-06-03 17:29:56 +01:00
// Run the server
s.Run()
2020-06-03 17:29:56 +01:00
// Close the server
if err := s.Close(); err != nil {
panic(err)
}
2020-05-29 17:41:11 +01:00
}