-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
115 lines (95 loc) · 2.35 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
package main
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
"time"
"github.com/labstack/gommon/color"
"github.com/pkg/profile"
uuid "github.com/satori/go.uuid"
kingpin "gopkg.in/alecthomas/kingpin.v2"
"github.com/clagraff/pobox/endpoints"
"github.com/clagraff/pobox/monitoring"
"github.com/clagraff/pobox/requests"
)
var (
app = kingpin.New("pobox", "An app for logging requests from web-hooks")
routes = app.Arg("routes-files", "JSON/Yaml file path which defines custom routes").String()
prof = app.Flag("prof", "Enable profiling").Hidden().Bool()
)
func init() {
app.Version("0.0.0")
app.Parse(os.Args[1:])
}
func logRequests(receivedRequests chan http.Request) {
for {
select {
case r := <-receivedRequests:
color.Println(
color.Magenta("UTC Time:"),
"\t",
color.Yellow(time.Now().UTC().String()),
)
color.Println(
color.Magenta("Local Time:"),
"\t",
color.Yellow(time.Now().Local().String()),
)
color.Println(
color.Magenta("Method:"),
"\t",
color.Yellow(r.Method),
)
color.Println(
color.Magenta("URL:"),
"\t\t",
color.Yellow(r.URL.String()),
)
color.Println(color.Magenta("Headers"))
for key, value := range r.Header {
color.Println(
"\t",
color.Yellow(fmt.Sprintf("%s: ", key)),
color.Green(strings.TrimSpace(strings.Join(value, ""))),
)
}
buf, err := ioutil.ReadAll(r.Body)
if err != nil {
panic(err)
}
if len(buf) > 0 {
color.Println(color.Magenta("Body:"))
color.Println("\t", color.Blue(string(buf)))
}
fmt.Println("\n")
default:
}
}
}
func main() {
if prof != nil && *prof {
defer profile.Start().Stop()
}
endpointsPort := 8080
monitoringPort := 8090
apiUUID := uuid.Must(uuid.NewV4())
color.Println(
color.Green(
fmt.Sprintf("POBox web-hook server running on port: %d", endpointsPort),
),
)
color.Println(
color.Blue(
fmt.Sprintf("POBox API server running on port: %d with API Key: %s", monitoringPort, apiUUID),
),
)
fmt.Println("")
rr := make(chan requests.Request)
_, startEndpointsServer := endpoints.CreateServer(rr, endpointsPort)
_, startMonitoringServer := monitoring.CreateServer(apiUUID, rr, monitoringPort)
go func() { startEndpointsServer() }()
go func() { startMonitoringServer() }()
select {} // block as the servers run. less CPU intensive than for-loop.
}