forked from Flyclops/GoCM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
96 lines (79 loc) · 2.27 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
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"runtime"
"sync"
)
type procSettings struct {
Host string
Port string
GCMAPIKey string
Logto string
}
type canonicalReplacement struct {
Original string `json:"original"`
Canonical string `json:"canonical"`
}
type report struct {
Attempts int `json:"attempts"`
Failures int `json:"failures"`
Pending int `json:"pending"`
Canonicals int `json:"canonicals"`
NotRegistered int `json:"notregistered"`
}
var settings procSettings
// Reporting, with matching sync mutexes
var runReport report
var runReportMutex sync.Mutex
var canonicalReplacements []canonicalReplacement
var canonicalReplacementsMutex sync.Mutex
var notRegisteredKeys []string
var notRegisteredMutex sync.Mutex
//=====================
//
// Main method
//
//=====================
func main() {
// Set max parallelism
i := runtime.NumCPU()
log.Printf("Running on %d CPUs\n", i)
runtime.GOMAXPROCS(i)
// Read in flag args
flag.StringVar(&settings.Host, "host", "0.0.0.0", "IP address to listen on (default: 0.0.0.0)")
flag.StringVar(&settings.Port, "port", "5601", "TCP port to listen on (default: 5601)")
flag.StringVar(&settings.GCMAPIKey, "apikey", "", "GCM API key (required)")
flag.StringVar(&settings.Logto, "logto", "", "Path to log file (default: stdout)")
flag.Parse()
// Set up logging
if settings.Logto != "" {
fmt.Println("Log path: ", settings.Logto)
f, err := os.OpenFile(settings.Logto, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
fmt.Println("Log file won't open", err)
} else {
log.SetOutput(f)
defer f.Close()
}
}
// Make sure there's an API key
if settings.GCMAPIKey == "" {
log.Fatal("No GCM API key provided (--apikey) - GCM API key is required")
}
// API Key in log
log.Printf("Using GCM API key %s\n", settings.GCMAPIKey)
// Start listening
listenAddress := fmt.Sprintf("%s:%s", settings.Host, settings.Port)
log.Println("Listening on " + listenAddress)
// Set up handlers
http.HandleFunc("/gcm/send", send)
http.HandleFunc("/gcm/report", getReport)
http.HandleFunc("/gcm/report/canonical", getCanonicalReport)
http.HandleFunc("/gcm/report/notregistered", getNotRegisteredReport)
// Start the listener
log.Fatal(http.ListenAndServe(listenAddress, nil))
}