-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
84 lines (77 loc) · 2.38 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
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"context"
"flag"
"log"
"net/http"
"time"
_ "github.com/mattn/go-sqlite3"
)
// TODO: Start and PollAndProcess should be part of the commander
func Start(ctx context.Context, opts *CommanderOptions) error {
// TODO: Move telegram building into NewCommander, NewCommander will accept
// only http.Client
t := &Telegram{hc: http.Client{}}
c, err := NewCommander(t, opts)
if err != nil {
return err
}
if opts.push {
return c.StartPush(opts)
} else {
return c.StartPoll()
}
}
func main() {
log.SetFlags(log.Flags() | log.Lshortfile)
db := flag.String("db_path", "./db.sql", "Path to the persistent sqlite3 database.")
push := flag.Bool("push", false, "If true will register webhook, otherwise will rely on polling to get updates.")
ip := flag.String("ip", "", "IP address of the server. Needed only if push is set to true.")
port := flag.Int("port", 8443, "Port of which webhook should listen. Needed only if push is set to true.")
cert := flag.String("cert_path", "webhook.crt", "TLS certificate. Needed only if push is set to true.")
key := flag.String("key_path", "webhook.key", "Private key for TLS. Needed only if push is set to true.")
flag.Parse()
log.Printf("db_path: %q", *db)
ctx := context.Background()
opts := &CommanderOptions{
useCache: false,
dbPath: *db,
port: *port,
certPath: *cert,
keyPath: *key,
ip: *ip,
push: *push,
stages: []time.Duration{
20 * time.Second,
1 * time.Hour * 23,
2 * time.Hour * 23,
3 * time.Hour * 23,
5 * time.Hour * 23,
8 * time.Hour * 24,
13 * time.Hour * 24,
21 * time.Hour * 24,
34 * time.Hour * 24,
55 * time.Hour * 24,
89 * time.Hour * 24,
144 * time.Hour * 24,
233 * time.Hour * 24,
377 * time.Hour * 24,
},
}
if err := Start(ctx, opts); err != nil {
log.Fatal(err)
}
}