Compare commits
1 Commits
v0.2.11
...
Release-nf
Author | SHA1 | Date | |
---|---|---|---|
d72d2a8302 |
34
.github/workflows/nfctl.yml
vendored
34
.github/workflows/nfctl.yml
vendored
@ -2,10 +2,10 @@ name: Auto Build
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- 'release/nfctl/*'
|
||||
- 'master'
|
||||
|
||||
env:
|
||||
RELEASE_VERSION: v24.07.14-r3
|
||||
RELEASE_VERSION: v24.07.13-r1
|
||||
|
||||
jobs:
|
||||
build-job:
|
||||
@ -25,38 +25,36 @@ jobs:
|
||||
go-version: '1.20'
|
||||
|
||||
- name: build linux amd64
|
||||
run: CGO_ENABLE=0 GOOS=linux GOARCH=amd64 go build -ldflags='-s -w' -o "dist/nfctl-linux_amd64-${{ env.RELEASE_VERSION }}" nft/nfctl/main.go
|
||||
run: CGO_ENABLE=0 GOOS=linux GOARCH=amd64 go build -ldflags='-s -w' -o dist/nfctl-linux_amd64-$RELEASE_VERSION nft/nfctl/main.go
|
||||
|
||||
- name: build linux arm64
|
||||
run: CGO_ENABLE=0 GOOS=linux GOARCH=arm64 go build -ldflags='-s -w' -o "dist/nfctl-linux_arm64-${{ env.RELEASE_VERSION }}" nft/nfctl/main.go
|
||||
run: CGO_ENABLE=0 GOOS=linux GOARCH=arm64 go build -ldflags='-s -w' -o dist/nfctl-linux_arm64-$RELEASE_VERSION nft/nfctl/main.go
|
||||
|
||||
- name: build windows amd64
|
||||
run: CGO_ENABLE=0 GOOS=windows GOARCH=amd64 go build -ldflags='-s -w' -o "dist/nfctl-win_amd64-${{ env.RELEASE_VERSION }}.exe" nft/nfctl/main.go
|
||||
run: CGO_ENABLE=0 GOOS=windows GOARCH=amd64 go build -ldflags='-s -w' -o dist/nfctl-win_amd64-$RELEASE_VERSION.exe nft/nfctl/main.go
|
||||
|
||||
- name: build windows arm64
|
||||
run: CGO_ENABLE=0 GOOS=windows GOARCH=arm64 go build -ldflags='-s -w' -o "dist/nfctl-win_arm64-${{ env.RELEASE_VERSION }}.exe" nft/nfctl/main.go
|
||||
run: CGO_ENABLE=0 GOOS=windows GOARCH=arm64 go build -ldflags='-s -w' -o dist/nfctl-win_arm64-$RELEASE_VERSION.exe nft/nfctl/main.go
|
||||
|
||||
- name: build darwin amd64
|
||||
run: CGO_ENABLE=0 GOOS=darwin GOARCH=amd64 go build -ldflags='-s -w' -o "dist/nfctl-darwin_amd64-${{ env.RELEASE_VERSION }}" nft/nfctl/main.go
|
||||
run: CGO_ENABLE=0 GOOS=darwin GOARCH=amd64 go build -ldflags='-s -w' -o dist/nfctl-darwin_arm64-$RELEASE_VERSION nft/nfctl/main.go
|
||||
|
||||
- name: build darwin arm64
|
||||
run: CGO_ENABLE=0 GOOS=darwin GOARCH=arm64 go build -ldflags='-s -w' -o "dist/nfctl-darwin_arm64-${{ env.RELEASE_VERSION }}" nft/nfctl/main.go
|
||||
run: CGO_ENABLE=0 GOOS=darwin GOARCH=arm64 go build -ldflags='-s -w' -o dist/nfctl-darwin_arm64-$RELEASE_VERSION nft/nfctl/main.go
|
||||
|
||||
- name: show all builds
|
||||
run: ls -lash dist
|
||||
|
||||
- name: create releases
|
||||
id: create_releases
|
||||
uses: "marvinpinto/action-automatic-releases@latest"
|
||||
with:
|
||||
automatic_release_tag: "Release-nfctl-${{ env.RELEASE_VERSION }}"
|
||||
automatic_release_tag: "Release-nfctl-$RELEASE_VERSION"
|
||||
repo_token: "${{ secrets.GITHUB_TOKEN }}"
|
||||
title: "Release_${{ env.RELEASE_VERSION }}"
|
||||
title: "Release_$RELEASE_VERSION"
|
||||
prerelease: false
|
||||
files: |
|
||||
dist/nfctl-linux_amd64-${{ env.RELEASE_VERSION }}
|
||||
dist/nfctl-linux_arm64-${{ env.RELEASE_VERSION }}
|
||||
dist/nfctl-win_amd64-${{ env.RELEASE_VERSION }}.exe
|
||||
dist/nfctl-win_arm64-${{ env.RELEASE_VERSION }}.exe
|
||||
dist/nfctl-darwin_amd64-${{ env.RELEASE_VERSION }}
|
||||
dist/nfctl-darwin_arm64-${{ env.RELEASE_VERSION }}
|
||||
dist/nfctl-linux_amd64-$RELEASE_VERSION
|
||||
dist/nfctl-linux_arm64-$RELEASE_VERSION
|
||||
dist/nfctl-win_amd64-$RELEASE_VERSION.exe
|
||||
dist/nfctl-win_arm64-$RELEASE_VERSION.exe
|
||||
dist/nfctl-darwin_arm64-$RELEASE_VERSION
|
||||
dist/nfctl-darwin_arm64-$RELEASE_VERSION
|
||||
|
41
ctx.go
41
ctx.go
@ -2,11 +2,9 @@ package nf
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/google/uuid"
|
||||
"github.com/loveuer/nf/internal/sse"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
@ -40,21 +38,12 @@ type Ctx struct {
|
||||
|
||||
func newContext(app *App, writer http.ResponseWriter, request *http.Request) *Ctx {
|
||||
|
||||
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)
|
||||
skippedNodes := make([]skippedNode, 0, app.maxSections)
|
||||
v := make(Params, 0, app.maxParams)
|
||||
|
||||
ctx := &Ctx{
|
||||
lock: sync.Mutex{},
|
||||
Request: request.WithContext(c),
|
||||
Request: request,
|
||||
path: request.URL.Path,
|
||||
method: request.Method,
|
||||
StatusCode: 200,
|
||||
@ -74,7 +63,6 @@ func newContext(app *App, writer http.ResponseWriter, request *http.Request) *Ct
|
||||
}
|
||||
|
||||
ctx.Writer = &ctx.writermem
|
||||
ctx.writermem.Header().Set(TraceKey, traceId)
|
||||
|
||||
return ctx
|
||||
}
|
||||
@ -124,10 +112,6 @@ 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++
|
||||
|
||||
@ -289,30 +273,23 @@ func (c *Ctx) Status(code int) *Ctx {
|
||||
c.lock.Lock()
|
||||
defer c.lock.Unlock()
|
||||
|
||||
c.Writer.WriteHeader(code)
|
||||
c.writermem.WriteHeader(code)
|
||||
c.StatusCode = c.writermem.status
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
// Set set response header
|
||||
func (c *Ctx) Set(key string, value string) {
|
||||
c.Writer.Header().Set(key, value)
|
||||
c.writermem.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.Writer.Header().Set(key, value)
|
||||
c.writermem.Header().Set(key, value)
|
||||
}
|
||||
|
||||
func (c *Ctx) SendStatus(code int) error {
|
||||
c.Status(code)
|
||||
c.Writer.WriteHeaderNow()
|
||||
c.writermem.WriteHeaderNow()
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -330,7 +307,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.Writer)
|
||||
encoder := json.NewEncoder(&c.writermem)
|
||||
|
||||
if err := encoder.Encode(data); err != nil {
|
||||
return err
|
||||
@ -363,5 +340,5 @@ func (c *Ctx) HTML(html string) error {
|
||||
}
|
||||
|
||||
func (c *Ctx) Write(data []byte) (int, error) {
|
||||
return c.Writer.Write(data)
|
||||
return c.writermem.Write(data)
|
||||
}
|
||||
|
10
go.mod
10
go.mod
@ -4,9 +4,9 @@ go 1.20
|
||||
|
||||
require (
|
||||
github.com/fatih/color v1.17.0
|
||||
github.com/go-git/go-billy/v5 v5.5.0
|
||||
github.com/go-git/go-git/v5 v5.12.0
|
||||
github.com/google/uuid v1.6.0
|
||||
github.com/savioxavier/termlink v1.3.0
|
||||
github.com/spf13/cobra v1.8.1
|
||||
)
|
||||
|
||||
@ -18,7 +18,6 @@ require (
|
||||
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
|
||||
github.com/emirpasic/gods v1.18.1 // indirect
|
||||
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
|
||||
github.com/go-git/go-billy/v5 v5.5.0 // indirect
|
||||
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
|
||||
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
||||
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
|
||||
@ -26,14 +25,15 @@ require (
|
||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/pjbgf/sha1cd v0.3.0 // indirect
|
||||
github.com/savioxavier/termlink v1.3.0 // indirect
|
||||
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
|
||||
github.com/skeema/knownhosts v1.2.2 // indirect
|
||||
github.com/spf13/pflag v1.0.5 // indirect
|
||||
github.com/xanzy/ssh-agent v0.3.3 // indirect
|
||||
golang.org/x/crypto v0.23.0 // indirect
|
||||
golang.org/x/crypto v0.21.0 // indirect
|
||||
golang.org/x/mod v0.12.0 // indirect
|
||||
golang.org/x/net v0.25.0 // indirect
|
||||
golang.org/x/sys v0.20.0 // indirect
|
||||
golang.org/x/net v0.22.0 // indirect
|
||||
golang.org/x/sys v0.18.0 // indirect
|
||||
golang.org/x/tools v0.13.0 // indirect
|
||||
gopkg.in/warnings.v0 v0.1.2 // indirect
|
||||
)
|
||||
|
16
go.sum
16
go.sum
@ -83,8 +83,8 @@ golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5y
|
||||
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
|
||||
golang.org/x/crypto v0.3.1-0.20221117191849-2c476679df9a/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4=
|
||||
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
|
||||
golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI=
|
||||
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
|
||||
golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA=
|
||||
golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs=
|
||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
||||
golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc=
|
||||
@ -96,8 +96,8 @@ golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug
|
||||
golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY=
|
||||
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
|
||||
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
|
||||
golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
|
||||
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
|
||||
golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc=
|
||||
golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
@ -116,14 +116,14 @@ golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
|
||||
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
|
||||
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||
golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc=
|
||||
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
|
||||
golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U=
|
||||
golang.org/x/term v0.20.0 h1:VnkxpohqXaOBYJtBmEppKUG6mXpi+4O6purfc2+sMhw=
|
||||
golang.org/x/term v0.18.0 h1:FcHjZXDMxI8mM3nwhX9HlKop4C0YQvCVCdwYl2wOtE8=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
@ -131,7 +131,7 @@ golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
||||
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
||||
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
|
||||
golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk=
|
||||
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
||||
|
@ -2,9 +2,11 @@ package nf
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/google/uuid"
|
||||
"github.com/loveuer/nf/nft/log"
|
||||
"os"
|
||||
"runtime/debug"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
@ -27,19 +29,33 @@ func NewRecover(enableStackTrace bool) HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
func NewLogger() HandlerFunc {
|
||||
func NewLogger(traceHeader ...string) HandlerFunc {
|
||||
Header := "X-Trace-ID"
|
||||
if len(traceHeader) > 0 && traceHeader[0] != "" {
|
||||
Header = traceHeader[0]
|
||||
}
|
||||
|
||||
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 | %v | %15s | %3d | %s | %6s | %s", c.Context().Value(TraceKey), ip, c.StatusCode, HumanDuration(duration.Nanoseconds()), c.Method(), c.Path())
|
||||
msg := fmt.Sprintf("NF | %s | %15s | %3d | %s | %6s | %s", shortTrace, ip, c.StatusCode, HumanDuration(duration.Nanoseconds()), c.Method(), c.Path())
|
||||
|
||||
switch {
|
||||
case c.StatusCode >= 500:
|
||||
|
1
nf.go
1
nf.go
@ -5,7 +5,6 @@ const (
|
||||
_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{}
|
||||
|
@ -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...)
|
||||
}
|
||||
|
@ -1,16 +0,0 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/loveuer/nf/nft/nfctl/version"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
checkCmd = &cobra.Command{
|
||||
Use: "check",
|
||||
Short: "nfctl new version check",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
version.Check(true, true, 30)
|
||||
},
|
||||
}
|
||||
)
|
@ -1,30 +1,19 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/loveuer/nf/nft/log"
|
||||
"github.com/loveuer/nf/nft/nfctl/opt"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
import "github.com/spf13/cobra"
|
||||
|
||||
var (
|
||||
Root = &cobra.Command{
|
||||
Use: "nfctl",
|
||||
Short: "nfctl: easy start your nf backend work",
|
||||
PersistentPreRun: func(cmd *cobra.Command, args []string) {
|
||||
if opt.Debug == true {
|
||||
log.SetLogLevel(log.LogLevelDebug)
|
||||
}
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
func init() {
|
||||
initNew()
|
||||
Root.PersistentFlags().BoolVar(&opt.Debug, "debug", false, "debug mode")
|
||||
|
||||
Root.AddCommand(
|
||||
versionCmd,
|
||||
checkCmd,
|
||||
cmdNew,
|
||||
)
|
||||
}
|
||||
|
@ -7,7 +7,6 @@ import (
|
||||
"github.com/loveuer/nf/nft/nfctl/clone"
|
||||
"github.com/loveuer/nf/nft/nfctl/opt"
|
||||
"github.com/loveuer/nf/nft/nfctl/tp"
|
||||
"github.com/loveuer/nf/nft/nfctl/version"
|
||||
"github.com/spf13/cobra"
|
||||
"net/url"
|
||||
"os"
|
||||
@ -34,11 +33,14 @@ nfctl new {project} --template http://username:token@my.gitlab.com/my-zone/my-re
|
||||
)
|
||||
|
||||
func initNew() {
|
||||
cmdNew.Flags().BoolVar(&opt.Debug, "debug", false, "debug mode")
|
||||
cmdNew.Flags().StringVarP(&template, "template", "t", "", "template name/url[example:ultone, https://github.com/xxx/yyy.git]")
|
||||
cmdNew.Flags().BoolVar(&disableInit, "without-init", false, "don't run template init script")
|
||||
|
||||
cmdNew.RunE = func(cmd *cobra.Command, args []string) error {
|
||||
version.Check(true, false, 5)
|
||||
if opt.Debug {
|
||||
log.SetLogLevel(log.LogLevelDebug)
|
||||
}
|
||||
|
||||
var (
|
||||
err error
|
||||
|
@ -1,7 +1,7 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/fatih/color"
|
||||
"github.com/loveuer/nf/nft/log"
|
||||
"github.com/loveuer/nf/nft/nfctl/version"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
@ -11,8 +11,7 @@ var (
|
||||
Use: "version",
|
||||
Short: "print nfctl version and exit",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
color.Cyan("nfctl - version: %s", version.Version)
|
||||
version.Check(true, false, 5)
|
||||
log.Info("version: %s", version.Version)
|
||||
},
|
||||
}
|
||||
)
|
||||
|
@ -3,17 +3,24 @@ package main
|
||||
import (
|
||||
"context"
|
||||
"github.com/loveuer/nf/nft/nfctl/cmd"
|
||||
"github.com/loveuer/nf/nft/nfctl/version"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
|
||||
defer cancel()
|
||||
|
||||
//if !(len(os.Args) >= 2 && os.Args[1] == "version") {
|
||||
// version.Check(5)
|
||||
//}
|
||||
version.Check()
|
||||
defer version.Fn()
|
||||
|
||||
_ = cmd.Root.ExecuteContext(ctx)
|
||||
|
||||
select {
|
||||
case <-time.After(3 * time.Second):
|
||||
case <-ctx.Done():
|
||||
case <-version.OkCh:
|
||||
}
|
||||
}
|
||||
|
@ -1,3 +0,0 @@
|
||||
package version
|
||||
|
||||
const Version = "v24.07.14-r3"
|
@ -1,95 +1,68 @@
|
||||
package version
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"bufio"
|
||||
"fmt"
|
||||
"github.com/fatih/color"
|
||||
"github.com/loveuer/nf/nft/log"
|
||||
"github.com/savioxavier/termlink"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
"sync"
|
||||
)
|
||||
|
||||
const Version = "v24.07.13-r1"
|
||||
|
||||
var (
|
||||
uri = "https://raw.gitcode.com/loveuer/nf/raw/master/nft/nfctl/version/var.go"
|
||||
prefix = "const Version = "
|
||||
lk = &sync.Mutex{}
|
||||
empty = func() {}
|
||||
upgrade = func(v string) func() {
|
||||
return func() {
|
||||
color.Green("\n🎉 🎉 🎉 [nfctl] New Version Found: %s", v)
|
||||
color.Cyan("Upgrade it with: [go install github.com/loveuer/nf/nft/nfctl@master]")
|
||||
fmt.Print("Or Download by: ")
|
||||
color.Cyan(termlink.Link("Releases", "https://github.com/loveuer/nf/releases"))
|
||||
fmt.Println()
|
||||
}
|
||||
}
|
||||
Fn = empty
|
||||
OkCh = make(chan struct{}, 1)
|
||||
)
|
||||
|
||||
func UpgradePrint(newVersion string) {
|
||||
fmt.Printf(`+----------------------------------------------------------------------+
|
||||
| 🎉 🎉 🎉 %s 🎉 🎉 🎉 |
|
||||
| %s |
|
||||
| Or Download by: |
|
||||
| %s |
|
||||
| %s |
|
||||
+----------------------------------------------------------------------+
|
||||
`,
|
||||
color.GreenString("New Version Found: %s", newVersion),
|
||||
color.CyanString("Upgrade it with: [go install github.com/loveuer/nf/nft/nfctl@master]"),
|
||||
color.CyanString(termlink.Link("Releases", "https://github.com/loveuer/nf/releases")),
|
||||
color.CyanString(termlink.Link("Releases", "https://gitcode.com/loveuer/nf/releases")),
|
||||
)
|
||||
}
|
||||
|
||||
func Check(printUpgradable bool, printNoNeedUpgrade bool, timeouts ...int) string {
|
||||
var (
|
||||
v string
|
||||
)
|
||||
|
||||
defer func() {
|
||||
if printUpgradable {
|
||||
if v > Version {
|
||||
UpgradePrint(v)
|
||||
}
|
||||
}
|
||||
|
||||
if printNoNeedUpgrade {
|
||||
if v == Version {
|
||||
color.Cyan("Your Version: %s is Newest", Version)
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
timeout := time.Duration(30) * time.Second
|
||||
if len(timeouts) > 0 && timeouts[0] > 0 {
|
||||
timeout = time.Duration(timeouts[0]) * time.Second
|
||||
}
|
||||
|
||||
req, _ := http.NewRequest(http.MethodGet, uri, nil)
|
||||
resp, err := (&http.Client{
|
||||
Timeout: timeout,
|
||||
Transport: &http.Transport{
|
||||
TLSClientConfig: &tls.Config{
|
||||
InsecureSkipVerify: true,
|
||||
},
|
||||
},
|
||||
}).Do(req)
|
||||
func Check() {
|
||||
ready := make(chan struct{})
|
||||
go func() {
|
||||
ready <- struct{}{}
|
||||
uri := "https://raw.gitcode.com/loveuer/nf/raw/master/nft/nfctl/version/version.go"
|
||||
prefix := "const Version = "
|
||||
resp, err := http.Get(uri)
|
||||
if err != nil {
|
||||
log.Debug("[Check] http get[%s] err: %v", uri, err.Error())
|
||||
return ""
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
content, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
log.Debug("[Check] http read all body err: %v", err)
|
||||
}
|
||||
scanner := bufio.NewScanner(resp.Body)
|
||||
scanner.Buffer(make([]byte, 16*1024), 1024*1024)
|
||||
|
||||
log.Debug("[Check] http get[%s] body:\n%s", uri, string(content))
|
||||
|
||||
for _, line := range strings.Split(string(content), "\n") {
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
log.Debug("[Check] version.go line: %s", line)
|
||||
if strings.HasPrefix(line, prefix) {
|
||||
may := strings.TrimPrefix(line, prefix)
|
||||
if len(may) > 2 {
|
||||
v = may[1 : len(may)-1]
|
||||
v := strings.TrimPrefix(line, prefix)
|
||||
if len(v) > 2 {
|
||||
v = v[1 : len(v)-1]
|
||||
}
|
||||
|
||||
return v
|
||||
if v != "" && v > Version {
|
||||
lk.Lock()
|
||||
Fn = upgrade(v)
|
||||
lk.Unlock()
|
||||
OkCh <- struct{}{}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
}()
|
||||
<-ready
|
||||
}
|
||||
|
@ -1,17 +0,0 @@
|
||||
package version
|
||||
|
||||
import (
|
||||
"github.com/loveuer/nf/nft/log"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestUpgradePrint(t *testing.T) {
|
||||
log.SetLogLevel(log.LogLevelDebug)
|
||||
UpgradePrint("v24.07.14-r5")
|
||||
}
|
||||
|
||||
func TestCheck(t *testing.T) {
|
||||
log.SetLogLevel(log.LogLevelDebug)
|
||||
v := Check(true, true, 1)
|
||||
t.Logf("got version: %s", v)
|
||||
}
|
@ -2,10 +2,9 @@ package resp
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/loveuer/nf"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/loveuer/nf"
|
||||
)
|
||||
|
||||
func handleEmptyMsg(status uint32, msg string) string {
|
||||
@ -103,18 +102,6 @@ func Resp403(c *nf.Ctx, data any, msgs ...string) error {
|
||||
return Resp(c, 403, msg, err, data)
|
||||
}
|
||||
|
||||
func Resp418(c *nf.Ctx, data any, msgs ...string) error {
|
||||
msg := MSG418
|
||||
err := ""
|
||||
|
||||
if len(msgs) > 0 && msgs[0] != "" {
|
||||
msg = fmt.Sprintf("%s: %s", msg, strings.Join(msgs, "; "))
|
||||
err = ""
|
||||
}
|
||||
|
||||
return Resp(c, 418, msg, err, data)
|
||||
}
|
||||
|
||||
func Resp429(c *nf.Ctx, data any, msgs ...string) error {
|
||||
msg := MSG429
|
||||
err := ""
|
||||
|
@ -7,7 +7,6 @@ const (
|
||||
MSG401 = "登录已过期, 请重新登录"
|
||||
MSG403 = "请求权限不足"
|
||||
MSG404 = "请求资源未找到"
|
||||
MSG418 = "请求条件不满足, 请稍后再试"
|
||||
MSG429 = "请求过于频繁, 请稍后再试"
|
||||
MSG500 = "服务器开小差了, 请稍后再试"
|
||||
MSG501 = "功能开发中, 尽情期待"
|
||||
|
59
readme.md
59
readme.md
@ -5,9 +5,8 @@
|
||||
##### basic usage
|
||||
|
||||
- get param
|
||||
|
||||
```go
|
||||
func main() {
|
||||
```go
|
||||
func main() {
|
||||
app := nf.New()
|
||||
|
||||
app.Get("/hello/:name", func(c *nf.Ctx) error {
|
||||
@ -16,13 +15,12 @@
|
||||
})
|
||||
|
||||
log.Fatal(app.Run("0.0.0.0:80"))
|
||||
}
|
||||
```
|
||||
}
|
||||
```
|
||||
|
||||
- parse request query
|
||||
|
||||
```go
|
||||
func handleQuery(c *nf.Ctx) error {
|
||||
```go
|
||||
func handleQuery(c *nf.Ctx) error {
|
||||
type Req struct {
|
||||
Name string `query:"name"`
|
||||
Addr []string `query:"addr"`
|
||||
@ -38,13 +36,12 @@
|
||||
}
|
||||
|
||||
return c.JSON(nf.Map{"query": req})
|
||||
}
|
||||
```
|
||||
}
|
||||
```
|
||||
|
||||
- parse application/json body
|
||||
|
||||
```go
|
||||
func handlePost(c *nf.Ctx) error {
|
||||
```go
|
||||
func handlePost(c *nf.Ctx) error {
|
||||
type Req struct {
|
||||
Name string `json:"name"`
|
||||
Addr []string `json:"addr"`
|
||||
@ -66,37 +63,5 @@
|
||||
}
|
||||
|
||||
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