-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
65 lines (53 loc) · 1.14 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
package main
import (
"fmt"
"log"
"net/http"
"os"
"strconv"
"github.com/julienschmidt/httprouter"
)
var (
response string
port = "8080"
bufferSize = 100000
logs chan *http.Request
disableCH = false
)
func index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
fmt.Fprintf(w, "%s", response)
if !disableCH {
logs <- r
}
}
func load(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
fmt.Fprintf(w, "%d", len(logs))
}
func main() {
if portEnv, present := os.LookupEnv("PORT"); present {
if v, err := strconv.Atoi(portEnv); err == nil {
if v > 0 && v < 65536 {
port = portEnv
}
}
}
if bufferSizeEnv, present := os.LookupEnv("BUFFER"); present {
if v, err := strconv.Atoi(bufferSizeEnv); err == nil {
if v > 0 {
bufferSize = v
}
}
}
if v, present := os.LookupEnv("DISABLE_CH"); present && v == "true" {
disableCH = true
}
logs = make(chan *http.Request, bufferSize)
response = os.Getenv("RESPONSE")
if !disableCH {
go logger(logs)
}
router := httprouter.New()
router.GET("/", index)
router.GET("/load", load)
log.Fatal(http.ListenAndServe(":"+port, router))
}