Compare commits

...

3 Commits

Author SHA1 Message Date
1e66a221e0 feat: add RenderHTML, Redirect 2024-10-25 10:23:52 +08:00
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
5 changed files with 41 additions and 7 deletions

View File

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

16
ctx.go
View File

@ -8,6 +8,7 @@ import (
"fmt"
"github.com/google/uuid"
"github.com/loveuer/nf/internal/sse"
"html/template"
"io"
"mime/multipart"
"net"
@ -362,6 +363,21 @@ func (c *Ctx) HTML(html string) error {
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) {
return c.Writer.Write(data)
}

View File

@ -3,15 +3,16 @@ package cmd
import (
"errors"
"fmt"
"net/url"
"os"
"path"
"github.com/loveuer/nf/nft/log"
"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"
"path"
)
var (
@ -44,6 +45,7 @@ func initNew() {
err error
urlIns *url.URL
pwd string
moduleName string
projectDir string
initBs []byte
renderBs []byte
@ -58,7 +60,8 @@ func initNew() {
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) {
return fmt.Errorf("project folder already exist")
@ -101,7 +104,8 @@ func initNew() {
}
if renderBs, err = tp.RenderVar(initBs, map[string]any{
"PROJECT_NAME": args[0],
"PROJECT_NAME": projectDir,
"MODULE_NAME": moduleName,
}); err != nil {
return fmt.Errorf("render template init script err: %v", err)
}

View File

@ -2,9 +2,10 @@ package resp
import (
"fmt"
"github.com/loveuer/nf"
"strconv"
"strings"
"github.com/loveuer/nf"
)
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)
}
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 := ""

View File

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