summaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
Diffstat (limited to 'main.go')
-rw-r--r--main.go78
1 files changed, 78 insertions, 0 deletions
diff --git a/main.go b/main.go
new file mode 100644
index 0000000..decde72
--- /dev/null
+++ b/main.go
@@ -0,0 +1,78 @@
+package main
+
+import (
+ "expvar"
+ "fmt"
+ "syscall"
+ "time"
+ //"github.com/pkg/profile"
+ "net"
+ "net/http"
+ "os"
+ "os/signal"
+)
+
+// Exported vars
+var (
+ queriesTotal = expvar.NewInt("queriesTotal")
+ start = time.Now()
+)
+
+func uptime() interface{} {
+ return int64(time.Since(start).Seconds())
+}
+
+func main() {
+ //defer profile.Start(profile.CPUProfile).Stop()
+ expvar.Publish("uptime", expvar.Func(uptime))
+
+ loadConfig()
+
+ server := &Server{
+ host: host,
+ port: port,
+ }
+ server.Run()
+
+ // HTTP Server
+ if !Config.NoHttp {
+ http.HandleFunc("/", indexHandler)
+ httpPort, err := strconv.Itoa(Config.HttpPort)
+ if err != nil {
+ fmt.Println("Error parsing HTTP port, using default 8080")
+ httpPort = "8080"
+ }
+ sock, _ := net.Listen("tcp", Config.HttpAddrress+":"+httpPort)
+ go func() {
+ if !Config.Quiet {
+ fmt.Printf("HTTP now available at %s:%s\n", Config.HttpAddress, httpPort)
+ }
+ http.Serve(sock, nil)
+ }()
+ }
+
+ // Notify on all signals, one at a time
+ signals := make(chan os.Signal, 1)
+ signal.Notify(signals)
+
+ // Trigger for list updates
+ ticker := time.Tick(time.Second * cfg.ListUpdate)
+
+ // Persistent loop to catch signals etc.
+ for {
+ select {
+ case sig = <-signals:
+ select {
+ case sig == syscall.SIGKILL:
+ os.Exit(1)
+ case sig == syscall.SIGINT || sig == syscall.SIGTERM:
+ os.Exit(0)
+ case sig == syscall.SIGHUP:
+ loadConfigFile()
+ }
+ fmt.Printf("Received signal %v\n", sig)
+ case <-ticker:
+ // Reload lists
+ }
+ }
+}