-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
123 lines (97 loc) · 2.72 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
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
122
123
package main
import (
"flag"
"fmt"
"net"
"os"
"os/signal"
"syscall"
"github.com/nlopes/slack"
"github.com/gregory-m/tcp-paste/handler"
"github.com/gregory-m/tcp-paste/server"
)
var runPaste = flag.Bool("paste", true, "Run paste service (save output on local disk)")
var pasteHost = flag.String("paste-host", ":4343", "Host and port for post service")
var runHTTP = flag.Bool("http", true, "Run HTTP service (to server saved output from local disk)")
var httpHost = flag.String("http-host", ":8080", "Host and port for HTTP service")
var storageDir = flag.String("storage", "/tmp", "Storage directory")
var hostName = flag.String("hostname", "localhost:8080", "Hostname to use in links")
var runSlack = flag.Bool("slack", true, "Run Slack service (to post output to slack)")
var slackHost = flag.String("slack-host", ":9393", "Host and port for slack service")
var slackToken = flag.String("slack-token", "", "Slack API token")
var slackChannel = flag.String("slack-chanel", "testa", "Slack API token")
func main() {
flag.Parse()
var services []server.Service
if *runPaste {
pasteAddr, err := net.ResolveTCPAddr("tcp", *pasteHost)
if err != nil {
fmt.Printf("Can't parse paste-host falg: %s\n", err)
os.Exit(1)
}
pasteH := &handler.SaveToDisk{
HostName: *hostName,
StorageDir: *storageDir,
Prefix: server.FileServerPrefix,
}
pasteS := &server.TCP{
Addr: pasteAddr,
Handler: pasteH,
}
services = append(services, pasteS)
}
if *runHTTP {
httpAddr, err := net.ResolveTCPAddr("tcp", *httpHost)
if err != nil {
fmt.Printf("Can't parse http-host falg: %s\n", err)
os.Exit(1)
}
httpS := &server.HTTP{
Host: httpAddr,
StorageDir: *storageDir,
Prefix: server.FileServerPrefix,
}
services = append(services, httpS)
}
if *runSlack {
slackAddr, err := net.ResolveTCPAddr("tcp", *slackHost)
if err != nil {
fmt.Printf("Can't parse slack-host falg: %s\n", err)
os.Exit(1)
}
if *slackToken == "" {
fmt.Print("Can't run slack service without slack token\n")
os.Exit(1)
}
api := slack.New(*slackToken)
slackH := &handler.Slack{
Channel: *slackChannel,
API: api,
}
slcakS := &server.TCP{
Addr: slackAddr,
Handler: slackH,
}
services = append(services, slcakS)
}
errChan := make(chan error)
exit := make(chan bool)
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt, syscall.SIGTERM)
for _, s := range services {
go func(s server.Service) {
fmt.Printf("Starting: %s\n", s.Name())
errChan <- s.Start()
}(s)
}
go func() {
for range signalChan {
fmt.Print("Interrupted, stopping services...\n")
for _, s := range services {
s.Stop()
}
exit <- true
}
}()
<-exit
}