diff --git a/doc_test.go b/doc_test.go new file mode 100644 index 0000000..dfa4cb8 --- /dev/null +++ b/doc_test.go @@ -0,0 +1,45 @@ +package sigsci_test + +import ( + "fmt" + "log" + "net/http" + "time" + + sigsci "github.com/signalsciences/sigsci-module-golang" +) + +func helloWorld(w http.ResponseWriter, r *http.Request) { + fmt.Fprintf(w, "Hello, %s!", r.URL.Path[1:]) +} + +func Example() { + // Existing http.Handler + mux := http.NewServeMux() + mux.HandleFunc("/", helloWorld) + + // Wrap the existing http.Handler with the SigSci module handler + wrapped, err := sigsci.NewModule( + // Existing handler to wrap + mux, + + // Any additional module options: + sigsci.Socket("unix", "/var/run/sigsci.sock"), + sigsci.Timeout(100*time.Millisecond), + sigsci.AnomalySize(512*1024), + sigsci.AnomalyDuration(1*time.Second), + sigsci.MaxContentLength(100000), + ) + + if err != nil { + log.Fatal(err) + } + + // Listen and Serve as usual using the wrapped sigsci handler + s := &http.Server{ + Handler: wrapped, + Addr: "localhost:8000", + } + + log.Fatal(s.ListenAndServe()) +}