Golang module install

IMPORTANT

This guide only applies to Next-Gen WAF customers with access to the Next-Gen WAF control panel. If you have access to the Next-Gen WAF product in the Fastly control panel, you can only deploy the Next-Gen WAF with the Edge WAF deployment method.

Download and install prerequisites

The Golang module requires two prerequisite packages to be installed: MessagePack Code Generator and the Signal Sciences custom tlstext package.

Install these packages using the go get command to download and install these packages directly from their GitHub repositories:

$ go get -u -t github.com/tinylib/msgp/msgp
$ go get -u -t github.com/signalsciences/tlstext

Download and extract the Golang module

  1. Download the latest version of the Golang module:

    $ curl -O -L https://dl.signalsciences.net/sigsci-module-golang/sigsci-module-golang_latest.tar.gz
  2. Extract the Golang module to $GOPATH/src/github.com/signalsciences:

    $ sudo mkdir -p $GOPATH/src/github.com/signalsciences
    $ sudo tar -xf sigsci-module-golang_latest.tar.gz -C $GOPATH/src/github.com/signalsciences

Wrap your application

You will need to wrap your application in the Next-Gen WAF Golang module handler for the module to process requests and secure your application.

NOTE

How to best wrap your application will depend on how your application is designed. The steps listed below are provided as an example, but the methods listed may not be ideal for your specific application. More information about the Golang http package can be found in the Golang documentation.

  1. In the import section of your Golang application, add the following line to import the Golang module:

    sigsci "github.com/signalsciences/sigsci-module-golang"
  2. Create a new ServeMux in your main() function to be used with the module:

    muxname := http.NewServeMux()
  3. Add functions to the ServeMux by adding mux.handleFunc lines. For example, functions named hellofunc and examplefunc can be added with lines such as these:

    muxname.HandleFunc(/hello“, hellofunc)
    muxname.HandleFunc(/example”, examplefunc)
  4. Wrap your ServeMux in the Next-Gen WAF Golang module by adding lines similar to this example:

    1wrappername, err := sigsci.NewModule(muxname)
    2if err != nil {
    3 log.Fatal(err)
    4}
  5. Call the wrapper in the method your application uses to serve HTTP requests. For example, if you’re using the ListenAndServe method, then you would use call the wrapper with:

    http.ListenAndServe(127.0.0.1:80, wrappername)

Example Application

Below is an example hello world application with the Next-Gen WAF Golang module successfully integrated:

1package main
2
3import (
4 "fmt"
5 "log"
6 "net/http"
7
8 sigsci "github.com/signalsciences/sigsci-module-golang"
9)
10
11func hellofunc(w http.ResponseWriter, r *http.Request) {
12 fmt.Fprintf(w, "Hello, world")
13}
14
15func examplefunc(w http.ResponseWriter, r *http.Request) {
16 fmt.Fprintf(w, "Example function output")
17}
18
19func main() {
20 muxname := http.NewServeMux()
21 muxname.HandleFunc("/hello", hellofunc)
22 muxname.HandleFunc("/example", examplefunc)
23
24 wrappername, err := sigsci.NewModule(muxname)
25 if err != nil {
26 log.Fatal(err)
27 }
28
29 http.ListenAndServe("127.0.0.1:80", wrappername)
30}
Was this guide helpful?

Do not use this form to send sensitive information. If you need assistance, contact support. This form is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.