-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
httppanic.go
45 lines (40 loc) · 1.06 KB
/
httppanic.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
package httppanic
import (
"fmt"
"math/rand"
"net/http"
"os"
"time"
"github.com/elastic/beats/v7/libbeat/logp"
)
var ch = make(chan struct{}, 1)
func StartServer() {
port := fmt.Sprintf(":424%d", rand.Intn(10))
go func() {
s := &http.Server{
Addr: port,
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
fmt.Print("logging to stdout")
fmt.Fprintf(os.Stderr, "logging to os.Stderr")
fmt.Fprintln(w, "logged to stdout and os.StdErr")
case http.MethodPost:
case "PANIC":
fmt.Fprintln(w, "HTTP PANIC server called: seeding signal to panic")
fmt.Println("logging to stdout before PANIC panic!")
time.Sleep(50 * time.Millisecond)
ch <- struct{}{}
}
}),
}
fmt.Printf("starting HTTP panic server on port %s\n", port)
logp.L().Infof("starting HTTP panic server on port %s\n", port)
if err := s.ListenAndServe(); err != nil {
logp.L().Error(fmt.Errorf("panic http server error: %w", err))
}
}()
}
func PanicCh() chan struct{} {
return ch
}