-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
67 lines (58 loc) · 1.89 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Service registry keeps track of all services in the network.
//
// It makes mDNS queries to get information about how many origins
// and stream publishers are there in the network. And then it constructs
// urls for the clients to access in the home page.
//
// The no. of urls are a n*m product
// where n = no. of origins and,
// m = no. of cache servers
//
// The list is re-queried on a specified interval.
//
// The client can choose to access any of these urls depending on where
// they are located. The origin info is sent to the cache server in a
// query string from which it knows which origin to point to.
package main
import (
"flag"
"log"
"os"
"os/signal"
"syscall"
"time"
)
var (
host string
port string
dir string
iface string
queryInterval string
)
func main() {
flag.StringVar(&host, "host", "", "host address to bind to")
flag.StringVar(&port, "port", "8080", "listening port")
flag.StringVar(&dir, "dir", "", "directory path to serve")
flag.StringVar(&iface, "iface", "wlp4s0", "interface to publish service info")
flag.StringVar(&queryInterval, "queryInterval", "5m", `interval after which to re-query the list of services. Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".`)
flag.Parse()
logger := log.New(os.Stdout, "[serve] ", log.LstdFlags|log.Lshortfile)
if p, ok := os.LookupEnv("PORT"); ok {
port = p
}
duration, err := time.ParseDuration(queryInterval)
if err != nil {
logger.Fatal(err)
}
registry := NewRegistry(dir, host, port, duration, logger)
registry.Start()
// listen for signals
signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM)
logger.Printf("Started server at %s\n", port)
// Block until one of the signals above is received
<-signalCh
logger.Println("Quit signal received, initializing shutdown...")
logger.Println("Stopping registry")
registry.Stop()
}