Compare commits
1 Commits
v0.3.0
...
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:
|
on:
|
||||||
push:
|
push:
|
||||||
branches:
|
branches:
|
||||||
- 'release/nfctl/*'
|
- 'master'
|
||||||
|
|
||||||
env:
|
env:
|
||||||
RELEASE_VERSION: v24.09.23-r1
|
RELEASE_VERSION: v24.07.13-r1
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build-job:
|
build-job:
|
||||||
@ -25,38 +25,36 @@ jobs:
|
|||||||
go-version: '1.20'
|
go-version: '1.20'
|
||||||
|
|
||||||
- name: build linux amd64
|
- 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
|
- 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
|
- 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
|
- 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
|
- 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
|
- 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
|
- name: create releases
|
||||||
id: create_releases
|
id: create_releases
|
||||||
uses: "marvinpinto/action-automatic-releases@latest"
|
uses: "marvinpinto/action-automatic-releases@latest"
|
||||||
with:
|
with:
|
||||||
automatic_release_tag: "Release-nfctl-${{ env.RELEASE_VERSION }}"
|
automatic_release_tag: "Release-nfctl-$RELEASE_VERSION"
|
||||||
repo_token: "${{ secrets.GITHUB_TOKEN }}"
|
repo_token: "${{ secrets.GITHUB_TOKEN }}"
|
||||||
title: "Release_${{ env.RELEASE_VERSION }}"
|
title: "Release_$RELEASE_VERSION"
|
||||||
prerelease: false
|
prerelease: false
|
||||||
files: |
|
files: |
|
||||||
dist/nfctl-linux_amd64-${{ env.RELEASE_VERSION }}
|
dist/nfctl-linux_amd64-$RELEASE_VERSION
|
||||||
dist/nfctl-linux_arm64-${{ env.RELEASE_VERSION }}
|
dist/nfctl-linux_arm64-$RELEASE_VERSION
|
||||||
dist/nfctl-win_amd64-${{ env.RELEASE_VERSION }}.exe
|
dist/nfctl-win_amd64-$RELEASE_VERSION.exe
|
||||||
dist/nfctl-win_arm64-${{ env.RELEASE_VERSION }}.exe
|
dist/nfctl-win_arm64-$RELEASE_VERSION.exe
|
||||||
dist/nfctl-darwin_amd64-${{ env.RELEASE_VERSION }}
|
dist/nfctl-darwin_arm64-$RELEASE_VERSION
|
||||||
dist/nfctl-darwin_arm64-${{ env.RELEASE_VERSION }}
|
dist/nfctl-darwin_arm64-$RELEASE_VERSION
|
||||||
|
35
app.go
35
app.go
@ -5,15 +5,13 @@ import (
|
|||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/loveuer/nf/internal/bytesconv"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"path"
|
"path"
|
||||||
"regexp"
|
"regexp"
|
||||||
"sync"
|
|
||||||
|
|
||||||
"github.com/loveuer/nf/internal/bytesconv"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -31,8 +29,6 @@ type App struct {
|
|||||||
|
|
||||||
trees methodTrees
|
trees methodTrees
|
||||||
|
|
||||||
pool *sync.Pool
|
|
||||||
|
|
||||||
maxParams uint16
|
maxParams uint16
|
||||||
maxSections uint16
|
maxSections uint16
|
||||||
|
|
||||||
@ -44,34 +40,13 @@ type App struct {
|
|||||||
removeExtraSlash bool // false
|
removeExtraSlash bool // false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *App) allocateContext() *Ctx {
|
|
||||||
var (
|
|
||||||
skippedNodes = make([]skippedNode, 0, a.maxSections)
|
|
||||||
v = make(Params, 0, a.maxParams)
|
|
||||||
)
|
|
||||||
|
|
||||||
ctx := Ctx{
|
|
||||||
lock: sync.Mutex{},
|
|
||||||
app: a,
|
|
||||||
index: -1,
|
|
||||||
locals: make(map[string]any),
|
|
||||||
handlers: make([]HandlerFunc, 0),
|
|
||||||
skippedNodes: &skippedNodes,
|
|
||||||
params: &v,
|
|
||||||
}
|
|
||||||
|
|
||||||
return &ctx
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *App) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
|
func (a *App) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
|
||||||
var (
|
var (
|
||||||
err error
|
err error
|
||||||
c = a.pool.Get().(*Ctx)
|
c = newContext(a, writer, request)
|
||||||
nfe = new(Err)
|
nfe = new(Err)
|
||||||
)
|
)
|
||||||
|
|
||||||
c.reset(writer, request)
|
|
||||||
|
|
||||||
if err = c.verify(); err != nil {
|
if err = c.verify(); err != nil {
|
||||||
if errors.As(err, nfe) {
|
if errors.As(err, nfe) {
|
||||||
_ = c.Status(nfe.Status).SendString(nfe.Msg)
|
_ = c.Status(nfe.Status).SendString(nfe.Msg)
|
||||||
@ -83,8 +58,6 @@ func (a *App) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
a.handleHTTPRequest(c)
|
a.handleHTTPRequest(c)
|
||||||
|
|
||||||
a.pool.Put(c)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *App) run(ln net.Listener) error {
|
func (a *App) run(ln net.Listener) error {
|
||||||
@ -164,7 +137,9 @@ func (a *App) addRoute(method, path string, handlers ...HandlerFunc) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (a *App) handleHTTPRequest(c *Ctx) {
|
func (a *App) handleHTTPRequest(c *Ctx) {
|
||||||
var err error
|
var (
|
||||||
|
err error
|
||||||
|
)
|
||||||
|
|
||||||
httpMethod := c.Request.Method
|
httpMethod := c.Request.Method
|
||||||
rPath := c.Request.URL.Path
|
rPath := c.Request.URL.Path
|
||||||
|
98
ctx.go
98
ctx.go
@ -2,23 +2,21 @@ package nf
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
"github.com/loveuer/nf/internal/sse"
|
||||||
"io"
|
"io"
|
||||||
"mime/multipart"
|
"mime/multipart"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
|
||||||
"github.com/loveuer/nf/internal/sse"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var forwardHeaders = []string{"CF-Connecting-IP", "X-Forwarded-For", "X-Real-Ip"}
|
var (
|
||||||
|
forwardHeaders = []string{"CF-Connecting-IP", "X-Forwarded-For", "X-Real-Ip"}
|
||||||
|
)
|
||||||
|
|
||||||
type Ctx struct {
|
type Ctx struct {
|
||||||
lock sync.Mutex
|
lock sync.Mutex
|
||||||
@ -38,29 +36,35 @@ type Ctx struct {
|
|||||||
fullPath string
|
fullPath string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Ctx) reset(w http.ResponseWriter, r *http.Request) {
|
func newContext(app *App, writer http.ResponseWriter, request *http.Request) *Ctx {
|
||||||
traceId := r.Header.Get(TraceKey)
|
|
||||||
if traceId == "" {
|
skippedNodes := make([]skippedNode, 0, app.maxSections)
|
||||||
traceId = uuid.Must(uuid.NewV7()).String()
|
v := make(Params, 0, app.maxParams)
|
||||||
|
|
||||||
|
ctx := &Ctx{
|
||||||
|
lock: sync.Mutex{},
|
||||||
|
Request: request,
|
||||||
|
path: request.URL.Path,
|
||||||
|
method: request.Method,
|
||||||
|
StatusCode: 200,
|
||||||
|
|
||||||
|
app: app,
|
||||||
|
index: -1,
|
||||||
|
locals: map[string]interface{}{},
|
||||||
|
handlers: make([]HandlerFunc, 0),
|
||||||
|
skippedNodes: &skippedNodes,
|
||||||
|
params: &v,
|
||||||
}
|
}
|
||||||
|
|
||||||
c.writermem.reset(w)
|
ctx.writermem = responseWriter{
|
||||||
|
ResponseWriter: writer,
|
||||||
c.Request = r.WithContext(context.WithValue(r.Context(), TraceKey, traceId))
|
size: -1,
|
||||||
c.Writer = &c.writermem
|
status: 0,
|
||||||
c.handlers = nil
|
|
||||||
c.index = -1
|
|
||||||
c.path = r.URL.Path
|
|
||||||
c.method = r.Method
|
|
||||||
c.StatusCode = 200
|
|
||||||
|
|
||||||
c.fullPath = ""
|
|
||||||
*c.params = (*c.params)[:0]
|
|
||||||
*c.skippedNodes = (*c.skippedNodes)[:0]
|
|
||||||
for key := range c.locals {
|
|
||||||
delete(c.locals, key)
|
|
||||||
}
|
}
|
||||||
c.writermem.Header().Set(TraceKey, traceId)
|
|
||||||
|
ctx.Writer = &ctx.writermem
|
||||||
|
|
||||||
|
return ctx
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Ctx) Locals(key string, value ...interface{}) interface{} {
|
func (c *Ctx) Locals(key string, value ...interface{}) interface{} {
|
||||||
@ -92,7 +96,9 @@ func (c *Ctx) Path(overWrite ...string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Ctx) Cookies(key string, defaultValue ...string) string {
|
func (c *Ctx) Cookies(key string, defaultValue ...string) string {
|
||||||
dv := ""
|
var (
|
||||||
|
dv = ""
|
||||||
|
)
|
||||||
|
|
||||||
if len(defaultValue) > 0 {
|
if len(defaultValue) > 0 {
|
||||||
dv = defaultValue[0]
|
dv = defaultValue[0]
|
||||||
@ -106,10 +112,6 @@ func (c *Ctx) Cookies(key string, defaultValue ...string) string {
|
|||||||
return cookie.Value
|
return cookie.Value
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Ctx) Context() context.Context {
|
|
||||||
return c.Request.Context()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Ctx) Next() error {
|
func (c *Ctx) Next() error {
|
||||||
c.index++
|
c.index++
|
||||||
|
|
||||||
@ -271,30 +273,23 @@ func (c *Ctx) Status(code int) *Ctx {
|
|||||||
c.lock.Lock()
|
c.lock.Lock()
|
||||||
defer c.lock.Unlock()
|
defer c.lock.Unlock()
|
||||||
|
|
||||||
c.Writer.WriteHeader(code)
|
c.writermem.WriteHeader(code)
|
||||||
c.StatusCode = c.writermem.status
|
c.StatusCode = c.writermem.status
|
||||||
|
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set set response header
|
|
||||||
func (c *Ctx) Set(key string, value string) {
|
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) {
|
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 {
|
func (c *Ctx) SendStatus(code int) error {
|
||||||
c.Status(code)
|
c.Status(code)
|
||||||
c.Writer.WriteHeaderNow()
|
c.writermem.WriteHeaderNow()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -312,7 +307,7 @@ func (c *Ctx) Writef(format string, values ...interface{}) (int, error) {
|
|||||||
func (c *Ctx) JSON(data interface{}) error {
|
func (c *Ctx) JSON(data interface{}) error {
|
||||||
c.SetHeader("Content-Type", MIMEApplicationJSON)
|
c.SetHeader("Content-Type", MIMEApplicationJSON)
|
||||||
|
|
||||||
encoder := json.NewEncoder(c.Writer)
|
encoder := json.NewEncoder(&c.writermem)
|
||||||
|
|
||||||
if err := encoder.Encode(data); err != nil {
|
if err := encoder.Encode(data); err != nil {
|
||||||
return err
|
return err
|
||||||
@ -344,21 +339,6 @@ func (c *Ctx) HTML(html string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Ctx) RenderHTML(name, html string, obj any) error {
|
|
||||||
c.SetHeader("Content-Type", "text/html")
|
|
||||||
t, err := template.New(name).Parse(html)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return t.Execute(c.Writer, obj)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Ctx) Redirect(url string, code int) error {
|
|
||||||
http.Redirect(c.Writer, c.Request, url, code)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Ctx) Write(data []byte) (int, 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 (
|
require (
|
||||||
github.com/fatih/color v1.17.0
|
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/go-git/go-git/v5 v5.12.0
|
||||||
github.com/google/uuid v1.6.0
|
github.com/google/uuid v1.6.0
|
||||||
github.com/savioxavier/termlink v1.3.0
|
|
||||||
github.com/spf13/cobra v1.8.1
|
github.com/spf13/cobra v1.8.1
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -18,7 +18,6 @@ require (
|
|||||||
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
|
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
|
||||||
github.com/emirpasic/gods v1.18.1 // 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/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/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
|
||||||
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
||||||
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // 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-colorable v0.1.13 // indirect
|
||||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||||
github.com/pjbgf/sha1cd v0.3.0 // 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/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
|
||||||
github.com/skeema/knownhosts v1.2.2 // indirect
|
github.com/skeema/knownhosts v1.2.2 // indirect
|
||||||
github.com/spf13/pflag v1.0.5 // indirect
|
github.com/spf13/pflag v1.0.5 // indirect
|
||||||
github.com/xanzy/ssh-agent v0.3.3 // 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/mod v0.12.0 // indirect
|
||||||
golang.org/x/net v0.25.0 // indirect
|
golang.org/x/net v0.22.0 // indirect
|
||||||
golang.org/x/sys v0.20.0 // indirect
|
golang.org/x/sys v0.18.0 // indirect
|
||||||
golang.org/x/tools v0.13.0 // indirect
|
golang.org/x/tools v0.13.0 // indirect
|
||||||
gopkg.in/warnings.v0 v0.1.2 // 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.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.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.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
|
||||||
golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI=
|
golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA=
|
||||||
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
|
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.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.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
||||||
golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc=
|
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.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.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.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
|
||||||
golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
|
golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc=
|
||||||
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
|
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-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.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
golang.org/x/sync v0.1.0/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.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.5.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.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
|
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
|
||||||
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
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-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.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.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc=
|
||||||
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
|
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.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.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.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
golang.org/x/text v0.3.6/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.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.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.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-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.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||||
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
||||||
|
@ -2,9 +2,11 @@ package nf
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/google/uuid"
|
||||||
"github.com/loveuer/nf/nft/log"
|
"github.com/loveuer/nf/nft/log"
|
||||||
"os"
|
"os"
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
|
"strings"
|
||||||
"time"
|
"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 {
|
return func(c *Ctx) error {
|
||||||
var (
|
var (
|
||||||
now = time.Now()
|
now = time.Now()
|
||||||
|
trace = c.Get(Header)
|
||||||
logFn func(msg string, data ...any)
|
logFn func(msg string, data ...any)
|
||||||
ip = c.IP()
|
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()
|
err := c.Next()
|
||||||
duration := time.Since(now)
|
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 {
|
switch {
|
||||||
case c.StatusCode >= 500:
|
case c.StatusCode >= 500:
|
||||||
|
13
nf.go
13
nf.go
@ -1,13 +1,10 @@
|
|||||||
package nf
|
package nf
|
||||||
|
|
||||||
import "sync"
|
|
||||||
|
|
||||||
const (
|
const (
|
||||||
banner = " _ _ _ ___ _ \n | \\| |___| |_ | __|__ _ _ _ _ __| |\n | .` / _ \\ _| | _/ _ \\ || | ' \\/ _` |\n |_|\\_\\___/\\__| |_|\\___/\\_,_|_||_\\__,_|\n "
|
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>"
|
_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`
|
_405 = `405 Method Not Allowed`
|
||||||
_500 = `500 Internal Server Error`
|
_500 = `500 Internal Server Error`
|
||||||
TraceKey = "X-Trace-Id"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Map map[string]interface{}
|
type Map map[string]interface{}
|
||||||
@ -30,7 +27,8 @@ type Config struct {
|
|||||||
MethodNotAllowedHandler HandlerFunc `json:"-"`
|
MethodNotAllowedHandler HandlerFunc `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var defaultConfig = &Config{
|
var (
|
||||||
|
defaultConfig = &Config{
|
||||||
BodyLimit: 4 * 1024 * 1024,
|
BodyLimit: 4 * 1024 * 1024,
|
||||||
NotFoundHandler: func(c *Ctx) error {
|
NotFoundHandler: func(c *Ctx) error {
|
||||||
c.Set("Content-Type", MIMETextHTML)
|
c.Set("Content-Type", MIMETextHTML)
|
||||||
@ -43,6 +41,7 @@ var defaultConfig = &Config{
|
|||||||
return err
|
return err
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
)
|
||||||
|
|
||||||
func New(config ...Config) *App {
|
func New(config ...Config) *App {
|
||||||
app := &App{
|
app := &App{
|
||||||
@ -52,8 +51,6 @@ func New(config ...Config) *App {
|
|||||||
root: true,
|
root: true,
|
||||||
},
|
},
|
||||||
|
|
||||||
pool: &sync.Pool{},
|
|
||||||
|
|
||||||
redirectTrailingSlash: true, // true
|
redirectTrailingSlash: true, // true
|
||||||
redirectFixedPath: false, // false
|
redirectFixedPath: false, // false
|
||||||
handleMethodNotAllowed: true, // false
|
handleMethodNotAllowed: true, // false
|
||||||
@ -91,9 +88,5 @@ func New(config ...Config) *App {
|
|||||||
app.Use(NewRecover(true))
|
app.Use(NewRecover(true))
|
||||||
}
|
}
|
||||||
|
|
||||||
app.pool.New = func() any {
|
|
||||||
return app.allocateContext()
|
|
||||||
}
|
|
||||||
|
|
||||||
return app
|
return app
|
||||||
}
|
}
|
||||||
|
@ -21,7 +21,7 @@ var (
|
|||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
DefaultLogger = &logger{
|
defaultLogger = &logger{
|
||||||
Mutex: sync.Mutex{},
|
Mutex: sync.Mutex{},
|
||||||
timeFormat: "2006-01-02T15:04:05",
|
timeFormat: "2006-01-02T15:04:05",
|
||||||
writer: os.Stdout,
|
writer: os.Stdout,
|
||||||
@ -36,32 +36,32 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func SetTimeFormat(format string) {
|
func SetTimeFormat(format string) {
|
||||||
DefaultLogger.SetTimeFormat(format)
|
defaultLogger.SetTimeFormat(format)
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetLogLevel(level LogLevel) {
|
func SetLogLevel(level LogLevel) {
|
||||||
DefaultLogger.SetLogLevel(level)
|
defaultLogger.SetLogLevel(level)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Debug(msg string, data ...any) {
|
func Debug(msg string, data ...any) {
|
||||||
DefaultLogger.Debug(msg, data...)
|
defaultLogger.Debug(msg, data...)
|
||||||
}
|
}
|
||||||
func Info(msg string, data ...any) {
|
func Info(msg string, data ...any) {
|
||||||
DefaultLogger.Info(msg, data...)
|
defaultLogger.Info(msg, data...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Warn(msg string, data ...any) {
|
func Warn(msg string, data ...any) {
|
||||||
DefaultLogger.Warn(msg, data...)
|
defaultLogger.Warn(msg, data...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Error(msg string, data ...any) {
|
func Error(msg string, data ...any) {
|
||||||
DefaultLogger.Error(msg, data...)
|
defaultLogger.Error(msg, data...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Panic(msg string, data ...any) {
|
func Panic(msg string, data ...any) {
|
||||||
DefaultLogger.Panic(msg, data...)
|
defaultLogger.Panic(msg, data...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Fatal(msg string, data ...any) {
|
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
|
package cmd
|
||||||
|
|
||||||
import (
|
import "github.com/spf13/cobra"
|
||||||
"github.com/loveuer/nf/nft/log"
|
|
||||||
"github.com/loveuer/nf/nft/nfctl/opt"
|
|
||||||
"github.com/spf13/cobra"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
Root = &cobra.Command{
|
Root = &cobra.Command{
|
||||||
Use: "nfctl",
|
Use: "nfctl",
|
||||||
Short: "nfctl: easy start your nf backend work",
|
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() {
|
func init() {
|
||||||
initNew()
|
initNew()
|
||||||
Root.PersistentFlags().BoolVar(&opt.Debug, "debug", false, "debug mode")
|
|
||||||
|
|
||||||
Root.AddCommand(
|
Root.AddCommand(
|
||||||
versionCmd,
|
versionCmd,
|
||||||
checkCmd,
|
|
||||||
cmdNew,
|
cmdNew,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -3,16 +3,14 @@ package cmd
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
|
||||||
"os"
|
|
||||||
"path"
|
|
||||||
|
|
||||||
"github.com/loveuer/nf/nft/log"
|
"github.com/loveuer/nf/nft/log"
|
||||||
"github.com/loveuer/nf/nft/nfctl/clone"
|
"github.com/loveuer/nf/nft/nfctl/clone"
|
||||||
"github.com/loveuer/nf/nft/nfctl/opt"
|
"github.com/loveuer/nf/nft/nfctl/opt"
|
||||||
"github.com/loveuer/nf/nft/nfctl/tp"
|
"github.com/loveuer/nf/nft/nfctl/tp"
|
||||||
"github.com/loveuer/nf/nft/nfctl/version"
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
"net/url"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -30,22 +28,24 @@ nfctl new {project} --template http://username:token@my.gitlab.com/my-zone/my-re
|
|||||||
disableInit bool
|
disableInit bool
|
||||||
|
|
||||||
preTemplateMap = map[string]string{
|
preTemplateMap = map[string]string{
|
||||||
"ultone": "https://gitea.loveuer.com/loveuer/ultone.git",
|
"ultone": "https://gitcode.com/loveuer/ultone.git",
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func initNew() {
|
func initNew() {
|
||||||
cmdNew.Flags().StringVarP(&template, "template", "t", "ultone", "template name/url[example:ultone, https://github.com/xxx/yyy.git]")
|
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.Flags().BoolVar(&disableInit, "without-init", false, "don't run template init script")
|
||||||
|
|
||||||
cmdNew.RunE = func(cmd *cobra.Command, args []string) error {
|
cmdNew.RunE = func(cmd *cobra.Command, args []string) error {
|
||||||
version.Check(true, false, 5)
|
if opt.Debug {
|
||||||
|
log.SetLogLevel(log.LogLevelDebug)
|
||||||
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
err error
|
err error
|
||||||
urlIns *url.URL
|
urlIns *url.URL
|
||||||
pwd string
|
pwd string
|
||||||
moduleName string
|
|
||||||
projectDir string
|
projectDir string
|
||||||
initBs []byte
|
initBs []byte
|
||||||
renderBs []byte
|
renderBs []byte
|
||||||
@ -60,14 +60,13 @@ func initNew() {
|
|||||||
return fmt.Errorf("get work dir err")
|
return fmt.Errorf("get work dir err")
|
||||||
}
|
}
|
||||||
|
|
||||||
moduleName = args[0]
|
projectDir = path.Join(pwd, args[0])
|
||||||
projectDir = path.Join(pwd, path.Base(args[0]))
|
|
||||||
|
|
||||||
if _, err = os.Stat(projectDir); !errors.Is(err, os.ErrNotExist) {
|
if _, err = os.Stat(projectDir); !errors.Is(err, os.ErrNotExist) {
|
||||||
return fmt.Errorf("project folder already exist")
|
return fmt.Errorf("project folder already exist")
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = os.MkdirAll(projectDir, 0o750); err != nil {
|
if err = os.MkdirAll(projectDir, 0750); err != nil {
|
||||||
return fmt.Errorf("create project dir err: %v", err)
|
return fmt.Errorf("create project dir err: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -104,8 +103,7 @@ func initNew() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if renderBs, err = tp.RenderVar(initBs, map[string]any{
|
if renderBs, err = tp.RenderVar(initBs, map[string]any{
|
||||||
"PROJECT_NAME": projectDir,
|
"PROJECT_NAME": args[0],
|
||||||
"MODULE_NAME": moduleName,
|
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
return fmt.Errorf("render template init script err: %v", err)
|
return fmt.Errorf("render template init script err: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/fatih/color"
|
"github.com/loveuer/nf/nft/log"
|
||||||
"github.com/loveuer/nf/nft/nfctl/version"
|
"github.com/loveuer/nf/nft/nfctl/version"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
@ -11,8 +11,7 @@ var (
|
|||||||
Use: "version",
|
Use: "version",
|
||||||
Short: "print nfctl version and exit",
|
Short: "print nfctl version and exit",
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
color.Cyan("nfctl - version: %s", version.Version)
|
log.Info("version: %s", version.Version)
|
||||||
version.Check(true, false, 5)
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
@ -2,15 +2,25 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"github.com/loveuer/nf/nft/nfctl/cmd"
|
||||||
|
"github.com/loveuer/nf/nft/nfctl/version"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
"time"
|
||||||
"github.com/loveuer/nf/nft/nfctl/cmd"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
|
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
|
version.Check()
|
||||||
|
defer version.Fn()
|
||||||
|
|
||||||
_ = cmd.Root.ExecuteContext(ctx)
|
_ = 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
|
package version
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/tls"
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
"github.com/loveuer/nf/nft/log"
|
"github.com/loveuer/nf/nft/log"
|
||||||
"github.com/savioxavier/termlink"
|
"github.com/savioxavier/termlink"
|
||||||
"io"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const Version = "v24.07.13-r1"
|
||||||
|
|
||||||
var (
|
var (
|
||||||
uri = "https://raw.gitcode.com/loveuer/nf/raw/master/nft/nfctl/version/var.go"
|
lk = &sync.Mutex{}
|
||||||
prefix = "const Version = "
|
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) {
|
func Check() {
|
||||||
fmt.Printf(`+----------------------------------------------------------------------+
|
ready := make(chan struct{})
|
||||||
| 🎉 🎉 🎉 %s 🎉 🎉 🎉 |
|
go func() {
|
||||||
| %s |
|
ready <- struct{}{}
|
||||||
| Or Download by: |
|
uri := "https://raw.gitcode.com/loveuer/nf/raw/master/nft/nfctl/version/version.go"
|
||||||
| %s |
|
prefix := "const Version = "
|
||||||
| %s |
|
resp, err := http.Get(uri)
|
||||||
+----------------------------------------------------------------------+
|
|
||||||
`,
|
|
||||||
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)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Debug("[Check] http get[%s] err: %v", uri, err.Error())
|
log.Debug("[Check] http get[%s] err: %v", uri, err.Error())
|
||||||
return ""
|
return
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
content, err := io.ReadAll(resp.Body)
|
scanner := bufio.NewScanner(resp.Body)
|
||||||
if err != nil {
|
scanner.Buffer(make([]byte, 16*1024), 1024*1024)
|
||||||
log.Debug("[Check] http read all body err: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Debug("[Check] http get[%s] body:\n%s", uri, string(content))
|
for scanner.Scan() {
|
||||||
|
line := scanner.Text()
|
||||||
for _, line := range strings.Split(string(content), "\n") {
|
|
||||||
log.Debug("[Check] version.go line: %s", line)
|
log.Debug("[Check] version.go line: %s", line)
|
||||||
if strings.HasPrefix(line, prefix) {
|
if strings.HasPrefix(line, prefix) {
|
||||||
may := strings.TrimPrefix(line, prefix)
|
v := strings.TrimPrefix(line, prefix)
|
||||||
if len(may) > 2 {
|
if len(v) > 2 {
|
||||||
v = may[1 : len(may)-1]
|
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 (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/loveuer/nf"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/loveuer/nf"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func handleEmptyMsg(status uint32, msg string) string {
|
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)
|
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 {
|
func Resp429(c *nf.Ctx, data any, msgs ...string) error {
|
||||||
msg := MSG429
|
msg := MSG429
|
||||||
err := ""
|
err := ""
|
||||||
|
@ -7,7 +7,6 @@ const (
|
|||||||
MSG401 = "登录已过期, 请重新登录"
|
MSG401 = "登录已过期, 请重新登录"
|
||||||
MSG403 = "请求权限不足"
|
MSG403 = "请求权限不足"
|
||||||
MSG404 = "请求资源未找到"
|
MSG404 = "请求资源未找到"
|
||||||
MSG418 = "请求条件不满足, 请稍后再试"
|
|
||||||
MSG429 = "请求过于频繁, 请稍后再试"
|
MSG429 = "请求过于频繁, 请稍后再试"
|
||||||
MSG500 = "服务器开小差了, 请稍后再试"
|
MSG500 = "服务器开小差了, 请稍后再试"
|
||||||
MSG501 = "功能开发中, 尽情期待"
|
MSG501 = "功能开发中, 尽情期待"
|
||||||
|
35
readme.md
35
readme.md
@ -5,7 +5,6 @@
|
|||||||
##### basic usage
|
##### basic usage
|
||||||
|
|
||||||
- get param
|
- get param
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func main() {
|
func main() {
|
||||||
app := nf.New()
|
app := nf.New()
|
||||||
@ -20,7 +19,6 @@
|
|||||||
```
|
```
|
||||||
|
|
||||||
- parse request query
|
- parse request query
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func handleQuery(c *nf.Ctx) error {
|
func handleQuery(c *nf.Ctx) error {
|
||||||
type Req struct {
|
type Req struct {
|
||||||
@ -42,7 +40,6 @@
|
|||||||
```
|
```
|
||||||
|
|
||||||
- parse application/json body
|
- parse application/json body
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func handlePost(c *nf.Ctx) error {
|
func handlePost(c *nf.Ctx) error {
|
||||||
type Req struct {
|
type Req struct {
|
||||||
@ -68,35 +65,3 @@
|
|||||||
return c.JSON(nf.Map{"struct": req, "map": reqMap})
|
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