Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
940e86bd8d | |||
5263cba44a | |||
63f7516667 | |||
9b7f1e4413 |
2
.github/workflows/nfctl.yml
vendored
2
.github/workflows/nfctl.yml
vendored
@ -2,7 +2,7 @@ name: Auto Build
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- 'master'
|
||||
- 'release/nfctl/*'
|
||||
|
||||
env:
|
||||
RELEASE_VERSION: v24.07.14-r3
|
||||
|
23
ctx.go
23
ctx.go
@ -51,11 +51,10 @@ func newContext(app *App, writer http.ResponseWriter, request *http.Request) *Ct
|
||||
}
|
||||
|
||||
c := context.WithValue(request.Context(), TraceKey, traceId)
|
||||
request.WithContext(c)
|
||||
|
||||
ctx := &Ctx{
|
||||
lock: sync.Mutex{},
|
||||
Request: request,
|
||||
Request: request.WithContext(c),
|
||||
path: request.URL.Path,
|
||||
method: request.Method,
|
||||
StatusCode: 200,
|
||||
@ -75,6 +74,7 @@ func newContext(app *App, writer http.ResponseWriter, request *http.Request) *Ct
|
||||
}
|
||||
|
||||
ctx.Writer = &ctx.writermem
|
||||
ctx.writermem.Header().Set(TraceKey, traceId)
|
||||
|
||||
return ctx
|
||||
}
|
||||
@ -289,23 +289,30 @@ func (c *Ctx) Status(code int) *Ctx {
|
||||
c.lock.Lock()
|
||||
defer c.lock.Unlock()
|
||||
|
||||
c.writermem.WriteHeader(code)
|
||||
c.Writer.WriteHeader(code)
|
||||
c.StatusCode = c.writermem.status
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
// Set set response header
|
||||
func (c *Ctx) Set(key string, value string) {
|
||||
c.writermem.Header().Set(key, value)
|
||||
c.Writer.Header().Set(key, value)
|
||||
}
|
||||
|
||||
// AddHeader add response header
|
||||
func (c *Ctx) AddHeader(key string, value string) {
|
||||
c.Writer.Header().Add(key, value)
|
||||
}
|
||||
|
||||
// SetHeader set response header
|
||||
func (c *Ctx) SetHeader(key string, value string) {
|
||||
c.writermem.Header().Set(key, value)
|
||||
c.Writer.Header().Set(key, value)
|
||||
}
|
||||
|
||||
func (c *Ctx) SendStatus(code int) error {
|
||||
c.Status(code)
|
||||
c.writermem.WriteHeaderNow()
|
||||
c.Writer.WriteHeaderNow()
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -323,7 +330,7 @@ func (c *Ctx) Writef(format string, values ...interface{}) (int, error) {
|
||||
func (c *Ctx) JSON(data interface{}) error {
|
||||
c.SetHeader("Content-Type", MIMEApplicationJSON)
|
||||
|
||||
encoder := json.NewEncoder(&c.writermem)
|
||||
encoder := json.NewEncoder(c.Writer)
|
||||
|
||||
if err := encoder.Encode(data); err != nil {
|
||||
return err
|
||||
@ -356,5 +363,5 @@ func (c *Ctx) HTML(html string) error {
|
||||
}
|
||||
|
||||
func (c *Ctx) Write(data []byte) (int, error) {
|
||||
return c.writermem.Write(data)
|
||||
return c.Writer.Write(data)
|
||||
}
|
||||
|
@ -2,11 +2,9 @@ package nf
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/google/uuid"
|
||||
"github.com/loveuer/nf/nft/log"
|
||||
"os"
|
||||
"runtime/debug"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
@ -29,33 +27,19 @@ func NewRecover(enableStackTrace bool) HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
func NewLogger(traceHeader ...string) HandlerFunc {
|
||||
Header := "X-Trace-ID"
|
||||
if len(traceHeader) > 0 && traceHeader[0] != "" {
|
||||
Header = traceHeader[0]
|
||||
}
|
||||
func NewLogger() HandlerFunc {
|
||||
|
||||
return func(c *Ctx) error {
|
||||
var (
|
||||
now = time.Now()
|
||||
trace = c.Get(Header)
|
||||
logFn func(msg string, data ...any)
|
||||
ip = c.IP()
|
||||
)
|
||||
|
||||
if trace == "" {
|
||||
trace = uuid.Must(uuid.NewV7()).String()
|
||||
}
|
||||
|
||||
c.SetHeader(Header, trace)
|
||||
|
||||
traces := strings.Split(trace, "-")
|
||||
shortTrace := traces[len(traces)-1]
|
||||
|
||||
err := c.Next()
|
||||
duration := time.Since(now)
|
||||
|
||||
msg := fmt.Sprintf("NF | %s | %15s | %3d | %s | %6s | %s", shortTrace, ip, c.StatusCode, HumanDuration(duration.Nanoseconds()), c.Method(), c.Path())
|
||||
msg := fmt.Sprintf("NF | %v | %15s | %3d | %s | %6s | %s", c.Context().Value(TraceKey), ip, c.StatusCode, HumanDuration(duration.Nanoseconds()), c.Method(), c.Path())
|
||||
|
||||
switch {
|
||||
case c.StatusCode >= 500:
|
||||
|
@ -21,7 +21,7 @@ var (
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
defaultLogger = &logger{
|
||||
DefaultLogger = &logger{
|
||||
Mutex: sync.Mutex{},
|
||||
timeFormat: "2006-01-02T15:04:05",
|
||||
writer: os.Stdout,
|
||||
@ -36,32 +36,32 @@ var (
|
||||
)
|
||||
|
||||
func SetTimeFormat(format string) {
|
||||
defaultLogger.SetTimeFormat(format)
|
||||
DefaultLogger.SetTimeFormat(format)
|
||||
}
|
||||
|
||||
func SetLogLevel(level LogLevel) {
|
||||
defaultLogger.SetLogLevel(level)
|
||||
DefaultLogger.SetLogLevel(level)
|
||||
}
|
||||
|
||||
func Debug(msg string, data ...any) {
|
||||
defaultLogger.Debug(msg, data...)
|
||||
DefaultLogger.Debug(msg, data...)
|
||||
}
|
||||
func Info(msg string, data ...any) {
|
||||
defaultLogger.Info(msg, data...)
|
||||
DefaultLogger.Info(msg, data...)
|
||||
}
|
||||
|
||||
func Warn(msg string, data ...any) {
|
||||
defaultLogger.Warn(msg, data...)
|
||||
DefaultLogger.Warn(msg, data...)
|
||||
}
|
||||
|
||||
func Error(msg string, data ...any) {
|
||||
defaultLogger.Error(msg, data...)
|
||||
DefaultLogger.Error(msg, data...)
|
||||
}
|
||||
|
||||
func Panic(msg string, data ...any) {
|
||||
defaultLogger.Panic(msg, data...)
|
||||
DefaultLogger.Panic(msg, data...)
|
||||
}
|
||||
|
||||
func Fatal(msg string, data ...any) {
|
||||
defaultLogger.Fatal(msg, data...)
|
||||
DefaultLogger.Fatal(msg, data...)
|
||||
}
|
||||
|
35
readme.md
35
readme.md
@ -5,6 +5,7 @@
|
||||
##### basic usage
|
||||
|
||||
- get param
|
||||
|
||||
```go
|
||||
func main() {
|
||||
app := nf.New()
|
||||
@ -19,6 +20,7 @@ func main() {
|
||||
```
|
||||
|
||||
- parse request query
|
||||
|
||||
```go
|
||||
func handleQuery(c *nf.Ctx) error {
|
||||
type Req struct {
|
||||
@ -40,6 +42,7 @@ func handleQuery(c *nf.Ctx) error {
|
||||
```
|
||||
|
||||
- parse application/json body
|
||||
|
||||
```go
|
||||
func handlePost(c *nf.Ctx) error {
|
||||
type Req struct {
|
||||
@ -65,3 +68,35 @@ func handlePost(c *nf.Ctx) error {
|
||||
return c.JSON(nf.Map{"struct": req, "map": reqMap})
|
||||
}
|
||||
```
|
||||
|
||||
- pass local value
|
||||
|
||||
```go
|
||||
type User struct {
|
||||
Id int
|
||||
Username string
|
||||
}
|
||||
|
||||
func main() {
|
||||
app := nf.New()
|
||||
app.Use(auth())
|
||||
|
||||
app.Get("/item/list", list)
|
||||
}
|
||||
|
||||
func auth() nf.HandlerFunc {
|
||||
return func(c *nf.Ctx) error {
|
||||
c.Locals("user", &User{Id: 1, Username:"user"})
|
||||
return c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
func list(c *nf.Ctx) error {
|
||||
user, ok := c.Locals("user").(*User)
|
||||
if !ok {
|
||||
return c.Status(401).SendString("login required")
|
||||
}
|
||||
|
||||
...
|
||||
}
|
||||
```
|
||||
|
Reference in New Issue
Block a user