GuidesProtectionsHTTP Server

HTTP Server

Waffle provides Middleware to embed Waffle in your application, which detects common web attacks.
You need to call waffle.Start() at any timing, such as application initialization.

When using net/http

import (
    ...
  	"github.com/sitebatch/waffle-go"
    waffleHttp "github.com/sitebatch/waffle-go/contrib/net/http"
    waffleGin "github.com/sitebatch/waffle-go/contrib/gin-gonic/gin"
    ...
)
 
func main() {
    waffle.Start()
    ...
    srv.ListenAndServe()
}
 
func yourHTTPHandler() http.Handler {
	mux := http.NewServeMux()
	mux.Handle("/", http.HandlerFunc(yourSomeHandlerFunc))
 
	handler := waffleHttp.WafMiddleware(mux)
	return handler
}

When using gin

func main() {
	r := gin.Default()
 
  ...
	r.Use(waffleGin.WafMiddleware())
	waffle.Start(waffle.WithDebug())
  ...
  r.Run(":8080")
}

When using echo

package main
 
import (
	"github.com/labstack/echo/v4"
	"github.com/labstack/echo/v4/middleware"
	"github.com/sitebatch/waffle-go"
	waffleEcho "github.com/sitebatch/waffle-go/contrib/labstack/echo"
)
 
e := echo.New()
e.Use(waffleEcho.WafMiddleware())
e.Use(middleware.Recover())
 
waffle.Start()
 
e.Logger.Fatal(e.Start(":1323"))