1
Fork 0
galene/sfu.go

117 lines
2.5 KiB
Go
Raw Permalink Normal View History

2020-04-24 19:38:21 +02:00
// Copyright (c) 2020 by Juliusz Chroboczek.
// This is not open source software. Copy it, and I'll break into your
// house and tell your three year-old that Santa doesn't exist.
package main
import (
"flag"
"log"
2020-08-08 13:44:33 +02:00
"net"
2020-04-24 19:38:21 +02:00
"os"
"os/signal"
"path/filepath"
2020-05-20 23:04:31 +02:00
"runtime"
"runtime/pprof"
2020-04-24 19:38:21 +02:00
"syscall"
2020-09-13 14:12:00 +02:00
"sfu/disk"
"sfu/group"
2020-09-18 13:11:21 +02:00
"sfu/webserver"
2020-04-24 19:38:21 +02:00
)
func main() {
2020-09-18 13:11:21 +02:00
var cpuprofile, memprofile, mutexprofile, httpAddr, dataDir string
2020-08-08 13:44:33 +02:00
var tcpPort int
2020-05-20 23:04:31 +02:00
2020-04-24 19:38:21 +02:00
flag.StringVar(&httpAddr, "http", ":8443", "web server `address`")
2020-09-18 13:11:21 +02:00
flag.StringVar(&webserver.StaticRoot, "static", "./static/",
2020-04-24 19:38:21 +02:00
"web server root `directory`")
2020-08-08 13:44:33 +02:00
flag.IntVar(&tcpPort, "tcp-port", -1,
"TCP listener `port`. If 0, an ephemeral port is used.\n"+
"If -1, the TCP listerer is disabled")
2020-04-24 19:38:21 +02:00
flag.StringVar(&dataDir, "data", "./data/",
"data `directory`")
flag.StringVar(&group.Directory, "groups", "./groups/",
2020-04-25 02:25:51 +02:00
"group description `directory`")
2020-09-13 14:12:00 +02:00
flag.StringVar(&disk.Directory, "recordings", "./recordings/",
2020-05-23 01:48:36 +02:00
"recordings `directory`")
2020-05-20 23:04:31 +02:00
flag.StringVar(&cpuprofile, "cpuprofile", "",
"store CPU profile in `file`")
flag.StringVar(&memprofile, "memprofile", "",
"store memory profile in `file`")
flag.StringVar(&mutexprofile, "mutexprofile", "",
"store mutex profile in `file`")
2020-04-24 19:38:21 +02:00
flag.Parse()
2020-05-20 23:04:31 +02:00
if cpuprofile != "" {
f, err := os.Create(cpuprofile)
if err != nil {
log.Printf("Create(cpuprofile): %v", err)
return
}
pprof.StartCPUProfile(f)
defer func() {
pprof.StopCPUProfile()
f.Close()
}()
}
if memprofile != "" {
defer func() {
f, err := os.Create(memprofile)
if err != nil {
log.Printf("Create(memprofile): %v", err)
return
}
pprof.WriteHeapProfile(f)
f.Close()
}()
}
if mutexprofile != "" {
runtime.SetMutexProfileFraction(1)
defer func() {
f, err := os.Create(mutexprofile)
if err != nil {
log.Printf("Create(mutexprofile): %v", err)
return
}
pprof.Lookup("mutex").WriteTo(f, 0)
f.Close()
}()
}
group.IceFilename = filepath.Join(dataDir, "ice-servers.json")
2020-04-24 19:38:21 +02:00
2020-08-08 13:44:33 +02:00
if tcpPort >= 0 {
err := group.StartTCPListener(&net.TCPAddr{
Port: tcpPort,
})
if err != nil {
log.Fatalf("Couldn't start ICE TCP: %v", err)
}
}
go group.ReadPublicGroups()
serverDone := make(chan struct{})
go func() {
err := webserver.Serve(httpAddr, dataDir)
if err != nil {
log.Printf("Server: %v", err)
}
close(serverDone)
}()
2020-04-24 19:38:21 +02:00
terminate := make(chan os.Signal, 1)
signal.Notify(terminate, syscall.SIGINT, syscall.SIGTERM)
select {
case <-terminate:
webserver.Shutdown()
case <-serverDone:
os.Exit(1)
}
2020-04-24 19:38:21 +02:00
}