This repository has been archived by the owner on Oct 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
relay.go
60 lines (48 loc) · 1.47 KB
/
relay.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
package main
import "bufio"
import "bytes"
import "encoding/json"
import "flag"
import "fmt"
import "net/http"
import "os"
import "time"
type Message struct {
Timestamp time.Time `json:"timestamp"`
Level string `json:"level"`
Sublevel string `json:"sublevel"`
Message string `json:"message"`
}
const version_number = "v0.0.3"
func post_message(endpoint, level, sublevel *string, message string){
m := Message{time.Now(), *level, *sublevel, message}
b, err := json.Marshal(m)
if err != nil {
panic(err)
}
resp, err := http.Post(*endpoint, "application/json", bytes.NewBuffer(b))
if err != nil {
panic(err)
}
defer resp.Body.Close()
return
}
// The simplest use of a Scanner, to read standard input as a set of lines.
func main() {
version := flag.Bool("version", false, "print out the current version")
level := flag.String("level", "INFO", "level. Default: INFO")
sublevel := flag.String("sublevel", "user", "sublevel. Default: user")
endpoint := flag.String("endpoint", "http://127.0.0.1:9000", "endpoint. Default: http://127.0.0.1:9000")
flag.Parse()
if *version == true {
fmt.Println(version_number)
return
}
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
post_message(endpoint, level, sublevel, scanner.Text())
}
if err := scanner.Err(); err != nil {
fmt.Fprintln(os.Stderr, "reading standard input:", err)
}
}