summaryrefslogtreecommitdiff
path: root/src/hub/src/spreadspace.org/sfive-hub/s5hub.go
blob: 85274cf89d2d25cbec40e14b8b015bdb75e12bf4 (plain) (blame)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main

import (
	"flag"
	"log"
	"os"
	"os/signal"
	"spreadspace.org/sfive"
	"sync"
)

var s5hl = log.New(os.Stderr, "[s5hub]\t", log.LstdFlags)

func main() {
	db := flag.String("db", "/var/lib/sfive/db.sqlite", "path to the sqlite3 database file")
	dbMysql := flag.Bool("db-mysql", false, "use MySQL connector")
	pipe := flag.String("pipe", "/var/run/sfive/pipe", "path to the unix pipe for the pipeserver")
	ppipe := flag.String("pipegram", "/var/run/sfive/pipegram", "path to the unix datagram pipe for the pipeserver")
	startPipe := flag.Bool("start-pipe-server", true, "start a connection oriented pipe server; see option pipe")
	startGramPipe := flag.Bool("start-pipegram-server", true, "start a datagram oriented pipe server; see option pipegram")
	startWeb := flag.Bool("start-web-server", true, "start a webserver")
	forward := flag.String("forward-url", "", "forward to another sfive-server with http server at base-url")
	forwardES := flag.String("forward-es-url", "", "forward to an ElasticSearch *index* via http")
	forwardGraphite := flag.String("forward-graphite", "", "forward to Graphite at this host")
	GraphiteBasePath := flag.String("graphite-base-path", "sfive", "use this as base for all paths on graphite")
	vizAppDir := flag.String("viz-dir", "/usr/share/sfive/viz", "base-path to the viz application")
	help := flag.Bool("help", false, "show usage")

	flag.Parse()

	s5hl.Printf("Hello, world.\n")
	if *help {
		flag.Usage()
		return
	}

	server, err := sfive.NewServer(*dbMysql, *db)
	if err != nil {
		s5hl.Fatalf("failed to initialize: %v", err)
	}
	defer server.Close()

	var wg sync.WaitGroup

	if *startPipe {
		wg.Add(1)
		go func() {
			defer wg.Done()
			s5hl.Printf("start pipe at %v\n", *pipe)
			server.ServePipe(*pipe)
			s5hl.Println("pipe finished")
		}()
	}

	if *startGramPipe {
		wg.Add(1)
		go func() {
			defer wg.Done()
			s5hl.Printf("start pipegram at %v\n", *ppipe)
			server.ServeGramPipe(*ppipe)
			s5hl.Println("pipegram finished")
		}()
	}

	if *startWeb {
		wg.Add(1)
		go func() {
			defer wg.Done()
			s5hl.Println("start web-srv")
			server.ServeWeb(*vizAppDir)
			s5hl.Println("web finished")
		}()
	}

	if *forward != "" {
		wg.Add(1)
		go func() {
			defer wg.Done()
			s5hl.Println("start forward")
			server.RunForwarding(*forward)
			s5hl.Println("forward finished")
		}()
	}

	if *forwardES != "" {
		wg.Add(1)
		go func() {
			defer wg.Done()
			s5hl.Println("start elastic-search forward")
			server.RunForwardingToElasticSearch(*forwardES)
			s5hl.Println("elastic-search forward finished")
		}()
	}

	if *forwardGraphite != "" {
		wg.Add(1)
		go func() {
			defer wg.Done()
			s5hl.Println("start graphite forward")
			server.RunForwardingToGraphite(*forwardGraphite, *GraphiteBasePath)
			s5hl.Println("graphite forward finished")
		}()
	}

	alldone := make(chan bool)
	go func() {
		defer func() { alldone <- true }()
		wg.Wait()
	}()

	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt)

	select {
	case <-c:
		s5hl.Println("received interrupt, shutdown")
		return
	case <-alldone:
		return
	}
}