This repository has been archived by the owner on Dec 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
97 lines (84 loc) · 1.93 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
package main
import (
"context"
"fmt"
"github.com/lintmx/dd-watcher/api"
"github.com/lintmx/dd-watcher/watcher"
"gopkg.in/yaml.v2"
"io/ioutil"
"net/http"
"net/url"
"os"
"os/signal"
"path/filepath"
"sync"
"syscall"
"time"
)
// Config struct
type Config struct {
Interval uint32 `yaml:"interval"`
Proxy string `yaml:"proxy"`
Rooms []string `yaml:"rooms"`
}
func main() {
fmt.Fprintf(os.Stdout, "誰でも大好き!\n")
var wait sync.WaitGroup
defer wait.Wait()
currentDir, err := os.Getwd()
if err != nil {
fmt.Fprintf(os.Stderr, "Get Pwd Error.\n")
os.Exit(1)
}
conf := filepath.Join(currentDir, "config.yml")
// Read Configuration file
file, err := ioutil.ReadFile(conf)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to read configuration file - %s\n", conf)
os.Exit(1)
}
config := &Config{}
// Parse yaml
err = yaml.Unmarshal(file, &config)
if err != nil {
fmt.Fprintf(os.Stderr, "Configuration file parsing failed - %s\n", conf)
os.Exit(1)
}
if config.Proxy != "" {
proxyURL, err := url.Parse(config.Proxy)
if err != nil {
fmt.Fprintf(os.Stderr, "Read Http Proxy Error.\n")
os.Exit(1)
}
http.DefaultTransport = &http.Transport{Proxy: http.ProxyURL(proxyURL)}
}
ctx, cannel := context.WithCancel(context.Background())
for _, room := range config.Rooms {
u, err := url.Parse(room)
if err != nil {
fmt.Fprintf(os.Stderr, "Room Url Parse Error - %s\n", room)
continue
}
api := api.Check(u)
if api == nil {
fmt.Fprintf(os.Stderr, "Room not support - %s\n", u.Host)
} else {
wait.Add(1)
w := &watcher.Watcher{
LiveAPI: api,
TimeTicker: time.Duration(config.Interval),
Wait: &wait,
}
go w.Run(ctx)
}
}
// Catch the exit signals
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
go func() {
<-sigCh
cannel()
}()
wait.Wait()
fmt.Fprintf(os.Stdout, "\nさようなら~\n")
}