feat: add trace_id to context
This commit is contained in:
parent
fb97d6e811
commit
9b87882970
20
ctx.go
20
ctx.go
@ -2,9 +2,11 @@ package nf
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/google/uuid"
|
||||
"github.com/loveuer/nf/internal/sse"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
@ -38,8 +40,18 @@ type Ctx struct {
|
||||
|
||||
func newContext(app *App, writer http.ResponseWriter, request *http.Request) *Ctx {
|
||||
|
||||
skippedNodes := make([]skippedNode, 0, app.maxSections)
|
||||
v := make(Params, 0, app.maxParams)
|
||||
var (
|
||||
traceId string
|
||||
skippedNodes = make([]skippedNode, 0, app.maxSections)
|
||||
v = make(Params, 0, app.maxParams)
|
||||
)
|
||||
|
||||
if traceId = request.Header.Get(TraceKey); traceId == "" {
|
||||
traceId = uuid.Must(uuid.NewV7()).String()
|
||||
}
|
||||
|
||||
c := context.WithValue(request.Context(), TraceKey, traceId)
|
||||
request.WithContext(c)
|
||||
|
||||
ctx := &Ctx{
|
||||
lock: sync.Mutex{},
|
||||
@ -112,6 +124,10 @@ func (c *Ctx) Cookies(key string, defaultValue ...string) string {
|
||||
return cookie.Value
|
||||
}
|
||||
|
||||
func (c *Ctx) Context() context.Context {
|
||||
return c.Request.Context()
|
||||
}
|
||||
|
||||
func (c *Ctx) Next() error {
|
||||
c.index++
|
||||
|
||||
|
9
nf.go
9
nf.go
@ -1,10 +1,11 @@
|
||||
package nf
|
||||
|
||||
const (
|
||||
banner = " _ _ _ ___ _ \n | \\| |___| |_ | __|__ _ _ _ _ __| |\n | .` / _ \\ _| | _/ _ \\ || | ' \\/ _` |\n |_|\\_\\___/\\__| |_|\\___/\\_,_|_||_\\__,_|\n "
|
||||
_404 = "<!doctype html><html lang=\"en\"><head><meta charset=\"UTF-8\"><meta name=\"viewport\" content=\"width=device-width,user-scalable=no,initial-scale=1,maximum-scale=1,minimum-scale=1\"><meta http-equiv=\"X-UA-Compatible\" content=\"ie=edge\"><title>Not Found</title><style>body{background:#333;margin:0;color:#ccc;display:flex;align-items:center;max-height:100vh;height:100vh;justify-content:center}textarea{min-height:5rem;min-width:20rem;text-align:center;border:none;background:0 0;color:#ccc;resize:none;user-input:none;user-select:none;cursor:default;-webkit-user-select:none;-webkit-touch-callout:none;-moz-user-select:none;-ms-user-select:none;outline:0}</style></head><body><textarea id=\"banner\" readonly=\"readonly\"></textarea><script type=\"text/javascript\">let htmlCodes = [\n ' _ _ _ ___ _ ',\n '| \\\\| |___| |_ | __|__ _ _ _ _ __| |',\n '| .` / _ \\\\ _| | _/ _ \\\\ || | \\' \\\\/ _` |',\n '|_|\\\\_\\\\___/\\\\__| |_|\\\\___/\\\\_,_|_||_\\\\__,_|'\n].join('\\n');\ndocument.querySelector('#banner').value = htmlCodes</script></body></html>"
|
||||
_405 = `405 Method Not Allowed`
|
||||
_500 = `500 Internal Server Error`
|
||||
banner = " _ _ _ ___ _ \n | \\| |___| |_ | __|__ _ _ _ _ __| |\n | .` / _ \\ _| | _/ _ \\ || | ' \\/ _` |\n |_|\\_\\___/\\__| |_|\\___/\\_,_|_||_\\__,_|\n "
|
||||
_404 = "<!doctype html><html lang=\"en\"><head><meta charset=\"UTF-8\"><meta name=\"viewport\" content=\"width=device-width,user-scalable=no,initial-scale=1,maximum-scale=1,minimum-scale=1\"><meta http-equiv=\"X-UA-Compatible\" content=\"ie=edge\"><title>Not Found</title><style>body{background:#333;margin:0;color:#ccc;display:flex;align-items:center;max-height:100vh;height:100vh;justify-content:center}textarea{min-height:5rem;min-width:20rem;text-align:center;border:none;background:0 0;color:#ccc;resize:none;user-input:none;user-select:none;cursor:default;-webkit-user-select:none;-webkit-touch-callout:none;-moz-user-select:none;-ms-user-select:none;outline:0}</style></head><body><textarea id=\"banner\" readonly=\"readonly\"></textarea><script type=\"text/javascript\">let htmlCodes = [\n ' _ _ _ ___ _ ',\n '| \\\\| |___| |_ | __|__ _ _ _ _ __| |',\n '| .` / _ \\\\ _| | _/ _ \\\\ || | \\' \\\\/ _` |',\n '|_|\\\\_\\\\___/\\\\__| |_|\\\\___/\\\\_,_|_||_\\\\__,_|'\n].join('\\n');\ndocument.querySelector('#banner').value = htmlCodes</script></body></html>"
|
||||
_405 = `405 Method Not Allowed`
|
||||
_500 = `500 Internal Server Error`
|
||||
TraceKey = "X-Trace-Id"
|
||||
)
|
||||
|
||||
type Map map[string]interface{}
|
||||
|
Loading…
x
Reference in New Issue
Block a user