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")
}