For AI agents: a documentation index is available at the root level at /llms.txt and /llms-full.txt. Append /llms.txt to any URL for a page-level index, or .md for the markdown version of any page.
HUMAN DashboardHUMAN WebsiteRequest a Demo
Product GuidesEnforcer GuidesMobile SDKAPI ReferenceCustomer support
Product GuidesEnforcer GuidesMobile SDKAPI ReferenceCustomer support
  • General
    • About Enforcers
    • Support first-party HUMAN calls
    • Troubleshoot Enforcer configurations
  • Enforcer frameworks
    • Akamai ESI
    • Apache - C Module
    • ASP.NET
    • Callout Enforcer
    • Envoy Proxy
    • F5 BIGIP
    • Fastly JavaScript Compute@Edge
      • What's New
        • Installing the Enforcer
        • Supported Features
        • Configuration Options
        • Upgrading the Enforcer
    • Google Cloud Platform (GCP) Callout Enforcer
    • Kong Plugin
    • NGINX - C Module
    • NGINX - LUA Module
    • PHP
    • Python
    • Ruby
    • Salesforce Commerce Cloud Cartridge
LogoLogo
Login
Login
HUMAN DashboardHUMAN WebsiteRequest a Demo
On this page
  • Installing the GO Enforcer
  • Import the HUMAN package
  • Implement the Runtime interface
  • GetConfig - Create configuration map.
  • PerformSyncRequest - perform synchronous request.
  • PerformAsyncRequest - perform asynchronous request.
  • Log - Logging implementation.
  • GetOS - get the Operating System name.
  • GetHostName - get the host name.
  • Implement the Enforcer
Enforcer frameworksGOV4 and Below

Installing the Enforcer

Was this page helpful?
Previous

Supported Features

Next
Built with

Installing the GO Enforcer

The GO Enforcer is used as a middleware inside a GO web application.
Implementation steps:

  1. Import the HUMAN package.
  2. Implement the Runtime interface.
  3. Implement the Enforcer.

Import the HUMAN package

$go get github.com/perimeterx/perimeterx-go-sdk/perimeterx
GitHub Repository Access

If you do not have access the Go SDK GitHub Repository, please reach out to our Customer Support team.

Implement the Runtime interface

The implementation consist of the Runtime methods:

1type Runtime interface {
2 GetConfig() PxConfig
3 PerformSyncRequest(*http.Request, int) (*http.Response, error)
4 PerformAsyncRequest(*http.Request, int)
5 Log(message string)
6 GetOS() string
7 GetHostName() string
8}

GetConfig - Create configuration map.

  • Return HUMANConfig
  • The Configuration fields should be set according to the Configuration Options section.
  • The HUMANApplication ID / AppId and HUMANToken / Auth Token can be found in the Portal, under Applications section.
  • The HUMANCookie Encryption Key can be found in the portal, under Policies section.
  • The Policy from where the Cookie Encryption Key is taken must correspond with the Application from where the Application ID / AppId and HUMANToken / Auth Token

PerformSyncRequest - perform synchronous request.

  • Return: response *http.Response, error.
  • Arguments: request *http.Request, timeout int.

PerformAsyncRequest - perform asynchronous request.

  • Arguments: request *http.Request, timeout int.
  • It is the caller’s responsibility to close the body.

Log - Logging implementation.

  • Argument: message string.

GetOS - get the Operating System name.

  • Return: name string.

GetHostName - get the host name.

  • Return: name string.

Implement the Enforcer

  • The enforcer is the main function of the module.
  • It enforces the request using the runtime instance as a context and can return a blocking response or an error.
  • In case the response is empty, the request should be forward to the origin.
  • Errors are silent.
  • In addition the enforcer handles HUMAN activities.

Example:

1import (
2 "github.com/perimeterx/perimeterx-go-sdk/perimeterx"
3)
4
5type Adapter func(http.Handler) http.Handler
6
7var pxConfig perimeterx.PxConfig
8
9func main() {
10 log.Printf("Go Server is running on port %d, %s OS with an %s CPU.", port, goruntime.GOOS, goruntime.GOARCH)
11
12 pxConfig = *NewPXConfigBuilder("App ID", "Cookie Secret", "Auth Token").
13 SetMonitorMode("active_blocking").
14 Build()
15
16 http.Handle("/", middleware()(http.FileServer(http.Dir("./static"))))
17 http.ListenAndServe(":3000", nil)
18}
19
20func middleware() Adapter {
21 return func(h http.Handler) http.Handler {
22 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
23 responseWriter := perimeterx.NewResponseWriterWrapper(w)
24 response, context, err := perimeterx.Enforce(r, &ExampleRuntime{})
25
26 fmt.Print(context.Score)
27
28 if err != nil {
29 fmt.Print(err)
30 }
31
32 if response.Body != nil {
33 responseWriter.WriteHeader(response.StatusCode)
34 responseWriter.Header().Set("Content-Type", response.Header.Get("Content-Type"))
35 var body, _ = ioutil.ReadAll(response.Body)
36 responseWriter.Write(body)
37 } else {
38 h.ServeHTTP(responseWriter, r)
39 }
40 })
41 }
42}
43
44var tr = &http.Transport{
45 MaxIdleConnsPerHost: 10,
46 ResponseHeaderTimeout: 60 * time.Second,
47}
48var client = &http.Client{Transport: tr}
49
50type ExampleRuntime struct {
51}
52
53func (runtime *ExampleRuntime) GetConfig() perimeterx.PxConfig {
54 return pxConfig
55}
56
57func (runtime *ExampleRuntime) PerformSyncRequest(req *http.Request, timeout int) (*http.Response, error) {
58 client.Timeout = time.Duration(timeout) * time.Millisecond
59 return client.Do(req)
60}
61
62func (runtime *ExampleRuntime) PerformAsyncRequest(req *http.Request, timeout int) {
63 go runtime.PerformSyncRequest(req, timeout)
64}
65
66func (runtime *ExampleRuntime) GetOS() string {
67 return goruntime.GOOS
68}
69
70func (runtime *ExampleRuntime) GetHostName() string {
71 osName, _ := os.Hostname()
72 return osName
73}
74
75func (runtime *ExampleRuntime) Log(message string) {
76 fmt.Print(message)
77}