Go (Gin)
package main
import (
"fmt"
"io"
"net/http"
"net/url"
"os"
"github.com/gin-gonic/gin"
)
func BlockDisposable() gin.HandlerFunc {
return func(c *gin.Context) {
email := c.Query("email")
if email == "" {
email = c.PostForm("email")
}
if email == "" {
c.Next()
return
}
req, _ := http.NewRequest("GET",
"https://api.disposableguard.com/v1/check", nil)
q := req.URL.Query()
q.Add("email", email)
req.URL.RawQuery = q.Encode()
req.Header.Set("Authorization", "Bearer "+os.Getenv("DG_KEY"))
resp, err := http.DefaultClient.Do(req)
if err != nil {
c.Next() // fail-open
return
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
// Parse JSON and check is_disposable field
// For brevity, use a JSON library like encoding/json
_ = body
c.Next()
}
}
func main() {
r := gin.Default()
r.POST("/signup", BlockDisposable(), signupHandler)
r.Run()
}Notes
Add JSON parsing with `encoding/json` to check the `is_disposable` field. Requires `github.com/gin-gonic/gin`.