Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
d8d771aec6 | |||
1e66a221e0 | |||
df318682fa | |||
af1e58bce9 | |||
940e86bd8d |
2
.github/workflows/nfctl.yml
vendored
2
.github/workflows/nfctl.yml
vendored
@ -5,7 +5,7 @@ on:
|
||||
- 'release/nfctl/*'
|
||||
|
||||
env:
|
||||
RELEASE_VERSION: v24.07.14-r3
|
||||
RELEASE_VERSION: v24.09.23-r1
|
||||
|
||||
jobs:
|
||||
build-job:
|
||||
|
39
app.go
39
app.go
@ -5,13 +5,15 @@ import (
|
||||
"crypto/tls"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/loveuer/nf/internal/bytesconv"
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"path"
|
||||
"regexp"
|
||||
"sync"
|
||||
|
||||
"github.com/loveuer/nf/internal/bytesconv"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -29,6 +31,8 @@ type App struct {
|
||||
|
||||
trees methodTrees
|
||||
|
||||
pool *sync.Pool
|
||||
|
||||
maxParams uint16
|
||||
maxSections uint16
|
||||
|
||||
@ -40,13 +44,34 @@ type App struct {
|
||||
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) {
|
||||
var (
|
||||
err error
|
||||
c = newContext(a, writer, request)
|
||||
c = a.pool.Get().(*Ctx)
|
||||
nfe = new(Err)
|
||||
)
|
||||
|
||||
c.reset(writer, request)
|
||||
|
||||
if err = c.verify(); err != nil {
|
||||
if errors.As(err, nfe) {
|
||||
_ = c.Status(nfe.Status).SendString(nfe.Msg)
|
||||
@ -58,6 +83,8 @@ func (a *App) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
|
||||
}
|
||||
|
||||
a.handleHTTPRequest(c)
|
||||
|
||||
a.pool.Put(c)
|
||||
}
|
||||
|
||||
func (a *App) run(ln net.Listener) error {
|
||||
@ -137,9 +164,7 @@ func (a *App) addRoute(method, path string, handlers ...HandlerFunc) {
|
||||
}
|
||||
|
||||
func (a *App) handleHTTPRequest(c *Ctx) {
|
||||
var (
|
||||
err error
|
||||
)
|
||||
var err error
|
||||
|
||||
httpMethod := c.Request.Method
|
||||
rPath := c.Request.URL.Path
|
||||
@ -263,7 +288,7 @@ func redirectFixedPath(c *Ctx, root *node, trailingSlash bool) bool {
|
||||
|
||||
func redirectRequest(c *Ctx) {
|
||||
req := c.Request
|
||||
//rPath := req.URL.Path
|
||||
// rPath := req.URL.Path
|
||||
rURL := req.URL.String()
|
||||
|
||||
code := http.StatusMovedPermanently // Permanent redirect, request with GET method
|
||||
@ -271,7 +296,7 @@ func redirectRequest(c *Ctx) {
|
||||
code = http.StatusTemporaryRedirect
|
||||
}
|
||||
|
||||
//debugPrint("redirecting request %d: %s --> %s", code, rPath, rURL)
|
||||
// debugPrint("redirecting request %d: %s --> %s", code, rPath, rURL)
|
||||
|
||||
http.Redirect(c.Writer, req, rURL, code)
|
||||
c.writermem.WriteHeaderNow()
|
||||
|
79
ctx.go
79
ctx.go
@ -6,19 +6,19 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/google/uuid"
|
||||
"github.com/loveuer/nf/internal/sse"
|
||||
"html/template"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
"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 {
|
||||
lock sync.Mutex
|
||||
@ -38,45 +38,29 @@ type Ctx struct {
|
||||
fullPath string
|
||||
}
|
||||
|
||||
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 == "" {
|
||||
func (c *Ctx) reset(w http.ResponseWriter, r *http.Request) {
|
||||
traceId := r.Header.Get(TraceKey)
|
||||
if traceId == "" {
|
||||
traceId = uuid.Must(uuid.NewV7()).String()
|
||||
}
|
||||
|
||||
c := context.WithValue(request.Context(), TraceKey, traceId)
|
||||
c.writermem.reset(w)
|
||||
|
||||
ctx := &Ctx{
|
||||
lock: sync.Mutex{},
|
||||
Request: request.WithContext(c),
|
||||
path: request.URL.Path,
|
||||
method: request.Method,
|
||||
StatusCode: 200,
|
||||
c.Request = r.WithContext(context.WithValue(r.Context(), TraceKey, traceId))
|
||||
c.Writer = &c.writermem
|
||||
c.handlers = nil
|
||||
c.index = -1
|
||||
c.path = r.URL.Path
|
||||
c.method = r.Method
|
||||
c.StatusCode = 200
|
||||
|
||||
app: app,
|
||||
index: -1,
|
||||
locals: map[string]interface{}{},
|
||||
handlers: make([]HandlerFunc, 0),
|
||||
skippedNodes: &skippedNodes,
|
||||
params: &v,
|
||||
c.fullPath = ""
|
||||
*c.params = (*c.params)[:0]
|
||||
*c.skippedNodes = (*c.skippedNodes)[:0]
|
||||
for key := range c.locals {
|
||||
delete(c.locals, key)
|
||||
}
|
||||
|
||||
ctx.writermem = responseWriter{
|
||||
ResponseWriter: writer,
|
||||
size: -1,
|
||||
status: 0,
|
||||
}
|
||||
|
||||
ctx.Writer = &ctx.writermem
|
||||
ctx.writermem.Header().Set(TraceKey, traceId)
|
||||
|
||||
return ctx
|
||||
c.writermem.Header().Set(TraceKey, traceId)
|
||||
}
|
||||
|
||||
func (c *Ctx) Locals(key string, value ...interface{}) interface{} {
|
||||
@ -108,9 +92,7 @@ func (c *Ctx) Path(overWrite ...string) string {
|
||||
}
|
||||
|
||||
func (c *Ctx) Cookies(key string, defaultValue ...string) string {
|
||||
var (
|
||||
dv = ""
|
||||
)
|
||||
dv := ""
|
||||
|
||||
if len(defaultValue) > 0 {
|
||||
dv = defaultValue[0]
|
||||
@ -362,6 +344,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)
|
||||
}
|
||||
|
38
nf.go
38
nf.go
@ -1,5 +1,7 @@
|
||||
package nf
|
||||
|
||||
import "sync"
|
||||
|
||||
const (
|
||||
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>"
|
||||
@ -23,26 +25,24 @@ type Config struct {
|
||||
DisableRecover bool `json:"-"`
|
||||
DisableHttpErrorLog bool `json:"-"`
|
||||
|
||||
//EnableNotImplementHandler bool `json:"-"`
|
||||
// EnableNotImplementHandler bool `json:"-"`
|
||||
NotFoundHandler HandlerFunc `json:"-"`
|
||||
MethodNotAllowedHandler HandlerFunc `json:"-"`
|
||||
}
|
||||
|
||||
var (
|
||||
defaultConfig = &Config{
|
||||
BodyLimit: 4 * 1024 * 1024,
|
||||
NotFoundHandler: func(c *Ctx) error {
|
||||
c.Set("Content-Type", MIMETextHTML)
|
||||
_, err := c.Status(404).Write([]byte(_404))
|
||||
return err
|
||||
},
|
||||
MethodNotAllowedHandler: func(c *Ctx) error {
|
||||
c.Set("Content-Type", MIMETextPlain)
|
||||
_, err := c.Status(405).Write([]byte(_405))
|
||||
return err
|
||||
},
|
||||
}
|
||||
)
|
||||
var defaultConfig = &Config{
|
||||
BodyLimit: 4 * 1024 * 1024,
|
||||
NotFoundHandler: func(c *Ctx) error {
|
||||
c.Set("Content-Type", MIMETextHTML)
|
||||
_, err := c.Status(404).Write([]byte(_404))
|
||||
return err
|
||||
},
|
||||
MethodNotAllowedHandler: func(c *Ctx) error {
|
||||
c.Set("Content-Type", MIMETextPlain)
|
||||
_, err := c.Status(405).Write([]byte(_405))
|
||||
return err
|
||||
},
|
||||
}
|
||||
|
||||
func New(config ...Config) *App {
|
||||
app := &App{
|
||||
@ -52,6 +52,8 @@ func New(config ...Config) *App {
|
||||
root: true,
|
||||
},
|
||||
|
||||
pool: &sync.Pool{},
|
||||
|
||||
redirectTrailingSlash: true, // true
|
||||
redirectFixedPath: false, // false
|
||||
handleMethodNotAllowed: true, // false
|
||||
@ -89,5 +91,9 @@ func New(config ...Config) *App {
|
||||
app.Use(NewRecover(true))
|
||||
}
|
||||
|
||||
app.pool.New = func() any {
|
||||
return app.allocateContext()
|
||||
}
|
||||
|
||||
return app
|
||||
}
|
||||
|
@ -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 (
|
||||
@ -29,12 +30,12 @@ nfctl new {project} --template http://username:token@my.gitlab.com/my-zone/my-re
|
||||
disableInit bool
|
||||
|
||||
preTemplateMap = map[string]string{
|
||||
"ultone": "https://gitcode.com/loveuer/ultone.git",
|
||||
"ultone": "https://gitea.loveuer.com/loveuer/ultone.git",
|
||||
}
|
||||
)
|
||||
|
||||
func initNew() {
|
||||
cmdNew.Flags().StringVarP(&template, "template", "t", "", "template name/url[example:ultone, https://github.com/xxx/yyy.git]")
|
||||
cmdNew.Flags().StringVarP(&template, "template", "t", "ultone", "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 {
|
||||
@ -44,6 +45,7 @@ func initNew() {
|
||||
err error
|
||||
urlIns *url.URL
|
||||
pwd string
|
||||
moduleName string
|
||||
projectDir string
|
||||
initBs []byte
|
||||
renderBs []byte
|
||||
@ -58,13 +60,14 @@ 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")
|
||||
}
|
||||
|
||||
if err = os.MkdirAll(projectDir, 0750); err != nil {
|
||||
if err = os.MkdirAll(projectDir, 0o750); err != nil {
|
||||
return fmt.Errorf("create project dir err: %v", err)
|
||||
}
|
||||
|
||||
@ -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)
|
||||
}
|
||||
|
@ -2,18 +2,15 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/loveuer/nf/nft/nfctl/cmd"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"github.com/loveuer/nf/nft/nfctl/cmd"
|
||||
)
|
||||
|
||||
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)
|
||||
//}
|
||||
|
||||
_ = cmd.Root.ExecuteContext(ctx)
|
||||
}
|
||||
|
@ -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 := ""
|
||||
|
@ -7,6 +7,7 @@ const (
|
||||
MSG401 = "登录已过期, 请重新登录"
|
||||
MSG403 = "请求权限不足"
|
||||
MSG404 = "请求资源未找到"
|
||||
MSG418 = "请求条件不满足, 请稍后再试"
|
||||
MSG429 = "请求过于频繁, 请稍后再试"
|
||||
MSG500 = "服务器开小差了, 请稍后再试"
|
||||
MSG501 = "功能开发中, 尽情期待"
|
||||
|
129
readme.md
129
readme.md
@ -5,63 +5,98 @@
|
||||
##### basic usage
|
||||
|
||||
- get param
|
||||
```go
|
||||
func main() {
|
||||
app := nf.New()
|
||||
|
||||
app.Get("/hello/:name", func(c *nf.Ctx) error {
|
||||
name := c.Param("name")
|
||||
return c.JSON(nf.Map{"status": 200, "data": "hello, " + name})
|
||||
})
|
||||
```go
|
||||
func main() {
|
||||
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
|
||||
```go
|
||||
func handleQuery(c *nf.Ctx) error {
|
||||
type Req struct {
|
||||
Name string `query:"name"`
|
||||
Addr []string `query:"addr"`
|
||||
}
|
||||
|
||||
var (
|
||||
err error
|
||||
req = Req{}
|
||||
)
|
||||
```go
|
||||
func handleQuery(c *nf.Ctx) error {
|
||||
type Req struct {
|
||||
Name string `query:"name"`
|
||||
Addr []string `query:"addr"`
|
||||
}
|
||||
|
||||
if err = c.QueryParser(&req); err != nil {
|
||||
return nf.NewNFError(400, err.Error())
|
||||
}
|
||||
var (
|
||||
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
|
||||
```go
|
||||
func handlePost(c *nf.Ctx) error {
|
||||
type Req struct {
|
||||
Name string `json:"name"`
|
||||
Addr []string `json:"addr"`
|
||||
}
|
||||
|
||||
var (
|
||||
err error
|
||||
req = Req{}
|
||||
reqMap = make(map[string]interface{})
|
||||
)
|
||||
```go
|
||||
func handlePost(c *nf.Ctx) error {
|
||||
type Req struct {
|
||||
Name string `json:"name"`
|
||||
Addr []string `json:"addr"`
|
||||
}
|
||||
|
||||
if err = c.BodyParser(&req); err != nil {
|
||||
return nf.NewNFError(400, err.Error())
|
||||
}
|
||||
var (
|
||||
err error
|
||||
req = Req{}
|
||||
reqMap = make(map[string]interface{})
|
||||
)
|
||||
|
||||
// can parse body multi times
|
||||
if err = c.BodyParser(&reqMap); err != nil {
|
||||
return nf.NewNFError(400, err.Error())
|
||||
}
|
||||
if err = c.BodyParser(&req); err != nil {
|
||||
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")
|
||||
}
|
||||
|
||||
...
|
||||
}
|
||||
```
|
||||
|
Reference in New Issue
Block a user