M O O N I T O

Integrate Analytics API with your Go Application (e.g., Echo, Gin, Buffalo, etc)

Here is the documentation guide on integrating Analytics API into Go application (e.g., Echo).

  • First, sign up for the domain you want to protect and learn about its visitors on the Analytics page.

Add New Domain

  • Now, you can use the following Go code.
package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"net/url"
	"strings"

	"github.com/labstack/echo/v4"
)

// Configuration
const (
	apiPublicKey = "Your API Public Key" // Put your API Public Key here
	apiSecretKey = "Your API Secret Key" // Put your API Secret Key here
	apiURL       = "https://moonito.net/api/v1/analytics"
)

func main() {
	e := echo.New()

	// Middleware
	e.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
		return func(c echo.Context) error {
			// Client IP Address Retrieval
			clientIP := c.Request().Header.Get("CF-Connecting-IP")
			if clientIP == "" {
				clientIP = c.Request().Header.Get("X-Forwarded-For")
			}
			if clientIP == "" {
				clientIP = c.RealIP()
			}

			// Construct URL for API request
			apiParams := url.Values{
				"ip":     {clientIP},
				"ua":     {c.Request().Header.Get("User-Agent")},
				"events": {c.Request().RequestURI},
				"domain": {strings.ToLower(c.Request().Host)},
			}
			apiURLWithParams := fmt.Sprintf("%s?%s", apiURL, apiParams.Encode())

			// Set up HTTP client
			client := http.Client{}

			// Set up HTTP request
			req, err := http.NewRequest("GET", apiURLWithParams, nil)
			if err != nil {
				return c.String(http.StatusInternalServerError, "Internal Server Error")
			}

			// Set headers
			req.Header.Set("User-Agent", "Mozilla/5.0 (Linux; Android 13; SM-G991U) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Mobile Safari/537.36")
			req.Header.Set("X-Public-Key", apiPublicKey)
			req.Header.Set("X-Secret-Key", apiSecretKey)

			// Execute HTTP request
			res, err := client.Do(req)
			if err != nil {
				return c.String(http.StatusInternalServerError, "Internal Server Error")
			}
			defer res.Body.Close()

			// Process data as needed
			body, err := ioutil.ReadAll(res.Body)
			if err != nil {
				return c.String(http.StatusInternalServerError, "Internal Server Error")
			}

			// Check if the visitor needs to be blocked
			// Adjust this part based on the actual response structure
			if strings.Contains(string(body), `"need_to_block":true`) {
				// Do some action when a visitor is detected as "need_to_block"
				// For example, return 403 Forbidden
				return c.String(http.StatusForbidden, "Forbidden")
			}

			// Continue processing the request
			return next(c)
		}
	})

	// Routes
	e.GET("/", func(c echo.Context) error {
		return c.String(http.StatusOK, "Hello, World!")
	})

	// Start server
	e.Start(":8080")
}
  • Input your Secret and Public API Key, then save.
// Configuration
const (
	apiPublicKey = "Your API Public Key" // Put your API Public Key here
	apiSecretKey = "Your API Secret Key" // Put your API Secret Key here
	apiURL       = "https://moonito.net/api/v1/analytics"
)
  • Edit response handling, the code processes the API response, and if the status indicates the need to block the visitor, it takes action (e.g., return 403 Forbidden).
// Check if the visitor needs to be blocked
// Adjust this part based on the actual response structure
if strings.Contains(string(body), `"need_to_block":true`) {
  // Do some action when a visitor is detected as "need_to_block"
  // For example, return 403 Forbidden
  return c.String(http.StatusForbidden, "Forbidden")
}
  • This example assumes you're using Echo. If you are using another Go framework, you need to adjust the code accordingly. Also, make sure your application has the necessary permissions to make outbound requests.
  • Do some testing. Open your Echo application on a web browser. This Go code will run when the page loads, requesting the analytics endpoint.
  • For information regarding visitor statistics, you can go to the Analytics page for details.