# Middlewares

## CSRF

Add in the api.go of the project program

```go
func (x *API) Routes(h *server.Hertz) (err error) {
	csrfToken := x.Csrf.VerifyToken(!x.V.IsRelease())

	...
	h.GET("", x.Index.Ping)
	_login := h.Group("login", csrfToken)
	{
	    ...
	}
	...
}
```

To separate single page front-end loading XSRF\_TOKEN need to be added in `Index.Ping`

```go
func (x *Controller) Ping(_ context.Context, c *app.RequestContext) {
	x.Csrf.SetToken(c)
	r := M{
		"name": x.V.Hostname,
		"ip":   string(c.GetHeader(x.V.Ip)),
		"now":  time.Now(),
	}
	if !x.V.IsRelease() {
		r["values"] = x.V
	}
	c.JSON(200, r)
}
```

For further security, the front and back-end domain names need to be of the same origin, and the cookie SameSite is set to `Strict`

## Error Chain

Set up global error chain in Hertz middleware

```go
func UseHertz(v *common.Values) (h *server.Hertz, err error) {
    ...
	h = server.Default(opts...)
	h.Use(
		requestid.New(),
		help.EHandler(),
		tracing.ServerMiddleware(cfg),
	)
	return
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://weplanx.gitbook.io/latest/golang-library/middlewares.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
