-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
74 lines (64 loc) · 1.91 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
// Copyright 2015, 2016, 2017 Ed Marshall. All rights reserved.
// Use of this source code is governed by a GPL-style
// license that can be found in the COPYING file.
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
)
// Config is our global (read-only) configuration state.
var Config Configuration
// Commands are any Slack commands that we recognize, and their handlers
var Commands SlashCommands
var version = "development version"
var timestamp = "unknown"
func versionString() string {
return fmt.Sprintf("slacker %s (build date %s)", version, timestamp)
}
func parseCli() Configuration {
var c Configuration
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "%s: [flags]\n", os.Args[0])
flag.PrintDefaults()
}
flag.StringVar(&c.File, "config-file", "slack.toml",
"Configuration file to load")
flag.Var(&c.Tokens, "token",
"Token to accept (can be specified multiple times)")
flag.StringVar(&c.ListenAddress, "listen-address", "0.0.0.0:8000",
"Address and port to listen on")
flag.BoolVar(&c.AsyncResponse, "async-response", true,
"Whether to respond to requests asynchronously")
flag.DurationVar(&c.HTTPClientTimeout, "http-client-timeout", 0,
"Time to wait before cancelling an external request")
ver := flag.Bool("version", false, "Display current version")
flag.Parse()
if *ver {
fmt.Println(versionString())
os.Exit(0)
}
if f, err := os.Open(c.File); err != nil {
log.Printf("Warning: could not load configuration file %s\n",
c.File)
} else {
if err = LoadConfig(&c, f); err != nil {
log.Fatal("Could not decode configuration: ", err)
}
f.Close()
}
return c
}
func main() {
Config = parseCli()
log.Printf("%s started\n", versionString())
Commands = SlashCommands{
"/ticker": Ticker,
}
http.Handle("/cmd", RequestIDMiddleware(ErrorHandler(SlackDispatcher)))
if err := http.ListenAndServe(Config.ListenAddress, nil); err != nil {
log.Fatal("ListenAndServe: ", err)
}
}