Skip to content

Commit 7ec4784

Browse files
committed
examples
1 parent 143934b commit 7ec4784

File tree

4 files changed

+91
-0
lines changed

4 files changed

+91
-0
lines changed

examples/certstream/main.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Example of using hookah to create an input stream from the CertStream
2+
// WebSocket API (https://certstream.calidog.io/).
3+
// The cert updates are filtered to remove heartbeat messages and processed by
4+
// restricting the JSON fields and adding indentation.
5+
// These updates are then written to stdout.
6+
package main
7+
8+
import (
9+
"encoding/json"
10+
"io"
11+
"log"
12+
13+
"github.com/wybiral/hookah"
14+
)
15+
16+
// CertStream JSON struct
17+
type certUpdate struct {
18+
MessageType string `json:"message_type"`
19+
Data struct {
20+
UpdateType string `json:"update_type"`
21+
LeafCert struct {
22+
AllDomains []string `json:"all_domains"`
23+
Fingerprint string `json:"fingerprint"`
24+
} `json:"leaf_cert"`
25+
Source struct {
26+
URL string `json:"url"`
27+
Name string `json:"name"`
28+
}
29+
} `json:"data"`
30+
}
31+
32+
func main() {
33+
// Create hookah input (certstream WebSocket API)
34+
r, err := hookah.NewInput("wss://certstream.calidog.io")
35+
if err != nil {
36+
log.Fatal(err)
37+
}
38+
defer r.Close()
39+
// Create hookah output (stdout)
40+
w, err := hookah.NewOutput("stdout")
41+
if err != nil {
42+
log.Fatal(err)
43+
}
44+
defer w.Close()
45+
// Start stream
46+
stream(w, r)
47+
}
48+
49+
// Copy from reader to writer
50+
// Drops heartbeat messages, restricts fields, and formats JSON
51+
func stream(w io.Writer, r io.Reader) {
52+
var u certUpdate
53+
d := json.NewDecoder(r)
54+
e := json.NewEncoder(w)
55+
e.SetIndent("", " ")
56+
for {
57+
err := d.Decode(&u)
58+
if err != nil {
59+
log.Fatal(err)
60+
}
61+
if u.MessageType == "heartbeat" {
62+
continue
63+
}
64+
err = e.Encode(u.Data)
65+
if err != nil {
66+
log.Fatal(err)
67+
}
68+
}
69+
}

examples_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ func ExampleNewInput_stdin() {
1717
r, err = hookah.NewInput("stdin")
1818
}
1919

20+
// Create file input from absolute path.
21+
func ExampleNewInput_fileAbsolute() {
22+
r, err = hookah.NewInput("file:///relative/path")
23+
}
24+
25+
// Create file input from relative path.
26+
func ExampleNewInput_fileRelative() {
27+
r, err = hookah.NewInput("file://relative/path")
28+
}
29+
2030
// Create HTTP client input.
2131
func ExampleNewInput_httpClient() {
2232
r, err = hookah.NewInput("http://localhost:8080")
@@ -69,6 +79,16 @@ func ExampleNewOutput_stderr() {
6979
w, err = hookah.NewOutput("stderr")
7080
}
7181

82+
// Create file output from absolute path (opens in append mode).
83+
func ExampleNewOutput_fileAbsolute() {
84+
w, err = hookah.NewOutput("file:///relative/path")
85+
}
86+
87+
// Create file output from relative path (opens in append mode).
88+
func ExampleNewOutput_fileRelative() {
89+
w, err = hookah.NewOutput("file://relative/path")
90+
}
91+
7292
// Create HTTP client output.
7393
func ExampleNewOutput_httpClient() {
7494
w, err = hookah.NewOutput("http://localhost:8080")

pkg/input/file.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"os"
66
)
77

8+
// Create a file input and return as ReadCloser
89
func file(path string) (io.ReadCloser, error) {
910
return os.Open(path)
1011
}

pkg/output/file.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"os"
66
)
77

8+
// Create a file output (in append mode) and return as WriteCloser
89
func file(path string) (io.WriteCloser, error) {
910
flags := os.O_APPEND | os.O_WRONLY | os.O_CREATE
1011
return os.OpenFile(path, flags, 0600)

0 commit comments

Comments
 (0)