rebound/cmd/server/main.go

51 lines
1.1 KiB
Go

package main
import (
"flag"
"log"
"os"
"os/signal"
"forge.cadoles.com/wpetit/rebound"
"github.com/caarlos0/env/v6"
"github.com/pkg/errors"
)
func main() {
opts := rebound.DefaultOptions()
if err := env.Parse(opts); err != nil {
log.Fatalf("[ERROR] %+v", errors.WithStack(err))
}
address := flag.String("address", opts.Address, "server listening address")
sockDir := flag.String("sock-dir", opts.SockDir, "sock directory")
publicPort := flag.Uint("public-port", opts.PublicPort, "public port")
publicHost := flag.String("public-host", opts.PublicHost, "public host")
hostKey := flag.String("host-key", opts.HostKey, "host key")
flag.Parse()
server := rebound.NewServer(
rebound.WithAddress(*address),
rebound.WithSockDir(*sockDir),
rebound.WithPublicPort(*publicPort),
rebound.WithPublicHost(*publicHost),
rebound.WithHostKey(*hostKey),
)
go func() {
if err := server.Run(); err != nil {
log.Fatalf("[FATAL] %+v", errors.WithStack(err))
}
}()
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
<-c
if err := server.Stop(); err != nil {
log.Fatalf("[FATAL] %+v", errors.WithStack(err))
}
}