Skip to content

Commit 92bf900

Browse files
docs: allow not using sigsci in the example so it can be used as an example in other documentation
1 parent 39ba35f commit 92bf900

File tree

2 files changed

+33
-31
lines changed

2 files changed

+33
-31
lines changed

README.md

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,23 +47,18 @@ log.Fatal(s.ListenAndServe())
4747
The [examples](examples/) directory contains complete example code.
4848

4949
To run the simple [helloworld](examples/helloworld/main.go) example:
50-
5150
```bash
51+
# Run __without__ sigsci enabled
5252
go run examples/helloworld/main.go
53-
```
54-
55-
Or, if your agent is running with a non-default `rpc-address`, you can
56-
pass the sigsci-agent address as an argument such as one of the following:
57-
58-
```bash
59-
# Another UNIX Domain socket
60-
go run examples/helloworld/main.go /tmp/sigsci.sock
61-
# A TCP address:port
53+
# Run with sigsci-agent listening via a UNIX Domain socket file
54+
go run examples/helloworld/main.go /var/run/sigsci.sock
55+
# Run with sigsci-agent listening via a TCP address:port
6256
go run examples/helloworld/main.go localhost:9999
6357
```
6458

65-
This will run an HTTP listener on `localhost:8000`, which will send any
66-
traffic to this listener to a running sigsci-agent for inspection.
59+
This will run a HTTP listener on `localhost:8000`, which will send any
60+
traffic to this listener to a running sigsci-agent for inspection (if
61+
configured).
6762

6863
[doc-img]: https://godoc.org/github.com/signalsciences/sigsci-module-golang?status.svg
6964
[doc]: https://godoc.org/github.com/signalsciences/sigsci-module-golang

examples/helloworld/main.go

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,42 +13,49 @@ import (
1313
func main() {
1414
// Process sigsci-agent rpc-address if passed
1515
sigsciAgentNetwork := "unix"
16-
sigsciAgentAddress := "/var/run/sigsci.sock"
16+
sigsciAgentAddress := ""
1717
if len(os.Args) > 1 {
1818
sigsciAgentAddress = os.Args[1]
1919
}
2020
if !strings.Contains(sigsciAgentAddress, "/") {
2121
sigsciAgentNetwork = "tcp"
2222
}
23-
log.Printf("Using sigsci-agent address (pass address as program argument to change): %s:%s", sigsciAgentNetwork, sigsciAgentAddress)
2423

2524
// Existing handler, in this case a simple http.ServeMux,
2625
// but could be any http.Handler in the application
2726
mux := http.NewServeMux()
2827
mux.HandleFunc("/", helloworld)
28+
handler := http.Handler(mux)
2929

30-
// Wrap the existing http.Handler with the SigSci module handler
31-
wrapped, err := sigsci.NewModule(
32-
// Existing handler to wrap
33-
mux,
30+
if len(sigsciAgentAddress) > 0 {
31+
// Wrap the existing http.Handler with the SigSci module handler
32+
wrapped, err := sigsci.NewModule(
33+
// Existing handler to wrap
34+
mux,
3435

35-
// Any additional module options:
36-
sigsci.Socket(sigsciAgentNetwork, sigsciAgentAddress),
37-
//sigsci.Timeout(100 * time.Millisecond),
38-
//sigsci.AnomalySize(512 * 1024),
39-
//sigsci.AnomalyDuration(1 * time.Second),
40-
//sigsci.MaxContentLength(100000),
36+
// Any additional module options:
37+
sigsci.Socket(sigsciAgentNetwork, sigsciAgentAddress),
38+
//sigsci.Timeout(100 * time.Millisecond),
39+
//sigsci.AnomalySize(512 * 1024),
40+
//sigsci.AnomalyDuration(1 * time.Second),
41+
//sigsci.MaxContentLength(100000),
4142

42-
// Turn on debug logging for this example (do not use in production)
43-
sigsci.Debug(true),
44-
)
45-
if err != nil {
46-
log.Fatal(err)
43+
// Turn on debug logging for this example (do not use in production)
44+
sigsci.Debug(true),
45+
)
46+
if err != nil {
47+
log.Fatal(err)
48+
}
49+
50+
log.Printf("Using sigsci-agent address (pass address as program argument to change): %s:%s", sigsciAgentNetwork, sigsciAgentAddress)
51+
52+
// Use the wrapped sigsci handler
53+
handler = wrapped
4754
}
4855

49-
// Listen and Serve as usual using the wrapped sigsci handler
56+
// Listen and Serve as usual using the wrapped sigsci handler if enabled
5057
s := &http.Server{
51-
Handler: wrapped,
58+
Handler: handler,
5259
Addr: "localhost:8000",
5360
}
5461
log.Printf("Server URL: http://%s/", s.Addr)

0 commit comments

Comments
 (0)