-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
69 lines (63 loc) · 1.25 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
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"os/signal"
"sync"
"syscall"
)
var authToken = os.Getenv("MINIO_WEBHOOK_AUTH_TOKEN")
var port = os.Getenv("MINIO_WEBHOOK_PORT")
func main() {
if len(os.Args) != 2 {
fmt.Println("Usage: minio-webhook filename.log")
os.Exit(1)
}
if port == "" {
port = "8080"
}
logFilePath := os.Args[1]
logFile, err := os.OpenFile(logFilePath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0640)
if err != nil {
log.Fatal(err)
}
var logFileMu sync.Mutex
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGHUP)
go func() {
for _ = range sigs {
logFileMu.Lock()
logFile.Close()
logFile, err = os.OpenFile(logFilePath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0640)
if err != nil {
log.Fatal(err)
}
logFileMu.Unlock()
}
}()
err = http.ListenAndServe(":"+port, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if authToken != "" {
if authToken != r.Header.Get("Authorization") {
return
}
}
switch r.Method {
case "POST":
data, err := ioutil.ReadAll(r.Body)
if err != nil {
return
}
logFileMu.Lock()
logFile.Write(data)
logFile.WriteString("\n")
logFileMu.Unlock()
default:
}
}))
if err != nil {
log.Fatal(err)
}
}