Skip to content

Commit 31286f8

Browse files
Merge pull request #11 from signalsciences/example-updates
Example updates
2 parents 39ba35f + ded17fe commit 31286f8

File tree

3 files changed

+57
-41
lines changed

3 files changed

+57
-41
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
* Updated helloworld example to be more configurable allowing it to be used in other example documentation
6+
57
## 1.6.2 2019-08-25
68

79
* Added support for a custom header extractor fn

README.md

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
The Signal Sciences module in Golang allows for integrating your Golang
77
application directly with the Signal Sciences agent at the source code
8-
level. It is written as an `http.Handler` wrapper. To
8+
level. It is written as a `http.Handler` wrapper. To
99
integrate your application with the module, you will need to wrap your
1010
existing handler with the module handler.
1111

@@ -14,7 +14,7 @@ existing handler with the module handler.
1414

1515
## Example Code Snippet
1616
```go
17-
// Existing http.Handler
17+
// Example existing http.Handler
1818
mux := http.NewServeMux()
1919
mux.HandleFunc("/", helloworld)
2020

@@ -44,26 +44,24 @@ log.Fatal(s.ListenAndServe())
4444

4545
## Examples
4646

47-
The [examples](examples/) directory contains complete example code.
47+
The [examples/helloworld](examples/helloworld) directory contains complete example code.
4848

4949
To run the simple [helloworld](examples/helloworld/main.go) example:
50-
51-
```bash
52-
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
62-
go run examples/helloworld/main.go localhost:9999
50+
```shell
51+
# Syntax:
52+
# go run ./examples/helloworld <listener-address:port> [<sigsci-agent-rpc-address>]
53+
#
54+
# Run WITHOUT sigsci enabled
55+
go run ./examples/helloworld localhost:8000
56+
# Run WITH sigsci-agent listening via a UNIX Domain socket file
57+
go run ./examples/helloworld localhost:8000 /var/run/sigsci.sock
58+
# Run WITH sigsci-agent listening via a TCP address:port
59+
go run ./examples/helloworld localhost:8000 localhost:9999
6360
```
6461

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.
62+
The above will run a HTTP listener on `localhost:8000`, which will send any
63+
traffic to this listener to a running sigsci-agent for inspection (if
64+
an agent address is configured).
6765

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

examples/helloworld/main.go

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,45 +11,61 @@ import (
1111
)
1212

1313
func main() {
14-
// Process sigsci-agent rpc-address if passed
15-
sigsciAgentNetwork := "unix"
16-
sigsciAgentAddress := "/var/run/sigsci.sock"
14+
// Get the listener address from the first arg
15+
listenerAddress := ""
1716
if len(os.Args) > 1 {
18-
sigsciAgentAddress = os.Args[1]
17+
listenerAddress = os.Args[1]
18+
}
19+
if len(listenerAddress) == 0 {
20+
listenerAddress = "localhost:8000"
21+
}
22+
23+
// Process sigsci-agent rpc-address from the optional second arg
24+
sigsciAgentNetwork := "unix"
25+
sigsciAgentAddress := ""
26+
if len(os.Args) > 2 {
27+
sigsciAgentAddress = os.Args[2]
1928
}
2029
if !strings.Contains(sigsciAgentAddress, "/") {
2130
sigsciAgentNetwork = "tcp"
2231
}
23-
log.Printf("Using sigsci-agent address (pass address as program argument to change): %s:%s", sigsciAgentNetwork, sigsciAgentAddress)
2432

2533
// Existing handler, in this case a simple http.ServeMux,
2634
// but could be any http.Handler in the application
2735
mux := http.NewServeMux()
2836
mux.HandleFunc("/", helloworld)
37+
handler := http.Handler(mux)
38+
39+
if len(sigsciAgentAddress) > 0 {
40+
// Wrap the existing http.Handler with the SigSci module handler
41+
wrapped, err := sigsci.NewModule(
42+
// Existing handler to wrap
43+
mux,
44+
45+
// Any additional module options:
46+
sigsci.Socket(sigsciAgentNetwork, sigsciAgentAddress),
47+
//sigsci.Timeout(100 * time.Millisecond),
48+
//sigsci.AnomalySize(512 * 1024),
49+
//sigsci.AnomalyDuration(1 * time.Second),
50+
//sigsci.MaxContentLength(100000),
2951

30-
// Wrap the existing http.Handler with the SigSci module handler
31-
wrapped, err := sigsci.NewModule(
32-
// Existing handler to wrap
33-
mux,
52+
// Turn on debug logging for this example (do not use in production)
53+
sigsci.Debug(true),
54+
)
55+
if err != nil {
56+
log.Fatal(err)
57+
}
3458

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),
59+
log.Printf("Signal Sciences agent RPC address: %s:%s", sigsciAgentNetwork, sigsciAgentAddress)
4160

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)
61+
// Use the wrapped sigsci handler
62+
handler = wrapped
4763
}
4864

49-
// Listen and Serve as usual using the wrapped sigsci handler
65+
// Listen and Serve as usual using the wrapped sigsci handler if enabled
5066
s := &http.Server{
51-
Handler: wrapped,
52-
Addr: "localhost:8000",
67+
Handler: handler,
68+
Addr: listenerAddress,
5369
}
5470
log.Printf("Server URL: http://%s/", s.Addr)
5571
log.Fatal(s.ListenAndServe())

0 commit comments

Comments
 (0)