Compare commits

..

3 Commits

Author SHA1 Message Date
df318682fa fix: nfctl new(project name include '/') 2024-09-22 20:50:19 -07:00
af1e58bce9 update: add resp 418 2024-09-19 01:43:05 -07:00
940e86bd8d chore: update go module, readme 2024-08-27 14:39:24 +08:00
5 changed files with 110 additions and 57 deletions

View File

@ -5,7 +5,7 @@ on:
- 'release/nfctl/*' - 'release/nfctl/*'
env: env:
RELEASE_VERSION: v24.07.14-r3 RELEASE_VERSION: v24.09.23-r1
jobs: jobs:
build-job: build-job:

View File

@ -3,15 +3,16 @@ 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/loveuer/nf/nft/nfctl/version"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"net/url"
"os"
"path"
) )
var ( var (
@ -44,6 +45,7 @@ func initNew() {
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
@ -58,7 +60,8 @@ func initNew() {
return fmt.Errorf("get work dir err") return fmt.Errorf("get work dir err")
} }
projectDir = path.Join(pwd, args[0]) moduleName = 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")
@ -101,7 +104,8 @@ func initNew() {
} }
if renderBs, err = tp.RenderVar(initBs, map[string]any{ if renderBs, err = tp.RenderVar(initBs, map[string]any{
"PROJECT_NAME": args[0], "PROJECT_NAME": projectDir,
"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)
} }

View File

@ -2,9 +2,10 @@ 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 {
@ -102,6 +103,18 @@ 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 := ""

View File

@ -7,6 +7,7 @@ const (
MSG401 = "登录已过期, 请重新登录" MSG401 = "登录已过期, 请重新登录"
MSG403 = "请求权限不足" MSG403 = "请求权限不足"
MSG404 = "请求资源未找到" MSG404 = "请求资源未找到"
MSG418 = "请求条件不满足, 请稍后再试"
MSG429 = "请求过于频繁, 请稍后再试" MSG429 = "请求过于频繁, 请稍后再试"
MSG500 = "服务器开小差了, 请稍后再试" MSG500 = "服务器开小差了, 请稍后再试"
MSG501 = "功能开发中, 尽情期待" MSG501 = "功能开发中, 尽情期待"

129
readme.md
View File

@ -5,63 +5,98 @@
##### basic usage ##### basic usage
- get param - get param
```go
func main() {
app := nf.New()
app.Get("/hello/:name", func(c *nf.Ctx) error { ```go
name := c.Param("name") func main() {
return c.JSON(nf.Map{"status": 200, "data": "hello, " + name}) app := nf.New()
})
log.Fatal(app.Run("0.0.0.0:80")) app.Get("/hello/:name", func(c *nf.Ctx) error {
} name := c.Param("name")
``` return c.JSON(nf.Map{"status": 200, "data": "hello, " + name})
})
log.Fatal(app.Run("0.0.0.0:80"))
}
```
- parse request query - parse request query
```go
func handleQuery(c *nf.Ctx) error {
type Req struct {
Name string `query:"name"`
Addr []string `query:"addr"`
}
var ( ```go
err error func handleQuery(c *nf.Ctx) error {
req = Req{} type Req struct {
) Name string `query:"name"`
Addr []string `query:"addr"`
}
if err = c.QueryParser(&req); err != nil { var (
return nf.NewNFError(400, err.Error()) err error
} req = Req{}
)
return c.JSON(nf.Map{"query": req}) if err = c.QueryParser(&req); err != nil {
} return nf.NewNFError(400, err.Error())
``` }
return c.JSON(nf.Map{"query": req})
}
```
- parse application/json body - parse application/json body
```go
func handlePost(c *nf.Ctx) error {
type Req struct {
Name string `json:"name"`
Addr []string `json:"addr"`
}
var ( ```go
err error func handlePost(c *nf.Ctx) error {
req = Req{} type Req struct {
reqMap = make(map[string]interface{}) Name string `json:"name"`
) Addr []string `json:"addr"`
}
if err = c.BodyParser(&req); err != nil { var (
return nf.NewNFError(400, err.Error()) err error
} req = Req{}
reqMap = make(map[string]interface{})
)
// can parse body multi times if err = c.BodyParser(&req); err != nil {
if err = c.BodyParser(&reqMap); err != nil { return nf.NewNFError(400, err.Error())
return nf.NewNFError(400, err.Error()) }
}
return c.JSON(nf.Map{"struct": req, "map": reqMap}) // can parse body multi times
} if err = c.BodyParser(&reqMap); err != nil {
``` return nf.NewNFError(400, err.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")
}
...
}
```