🎉 搭完基本框架

This commit is contained in:
zhaoyupeng
2024-09-26 17:51:40 +08:00
commit 429046e2f6
47 changed files with 10662 additions and 0 deletions

View File

@ -0,0 +1,18 @@
package controller
import (
"github.com/loveuer/nf/nft/log"
"nf-disk/internal/handler"
"nf-disk/internal/ndh"
"reflect"
)
func (a *App) register(path string, handler ndh.Handler) {
name := reflect.ValueOf(handler).String()
log.Info("app register: path = %s, name = %s", path, name)
a.handlers[path] = handler
}
func initApi(a *App) {
a.register("/api/connection/test", handler.ConnectionTest)
}

View File

@ -0,0 +1,45 @@
package controller
import (
"bytes"
"context"
"encoding/json"
"github.com/loveuer/nf/nft/log"
"nf-disk/internal/ndh"
"nf-disk/internal/tool"
"strings"
)
type App struct {
ctx context.Context
handlers map[string]ndh.Handler
}
func NewApp() *App {
return &App{
handlers: make(map[string]ndh.Handler),
}
}
func (a *App) Startup(ctx context.Context) {
a.ctx = ctx
log.Info("app startup!!!")
initApi(a)
}
func (a *App) Invoke(path string, req string) (res string) {
log.Info("app invoke: path = %s, req = %s", path, req)
handler, ok := a.handlers[path]
if !ok {
return `{"err": "handler not found", "status": 404}`
}
var buf bytes.Buffer
ctx := ndh.NewCtx(tool.Timeout(), json.NewDecoder(strings.NewReader(req)), &buf)
if err := handler(ctx); err != nil {
return err.Error()
}
return buf.String()
}

View File

@ -0,0 +1,25 @@
package handler
import (
"nf-disk/internal/ndh"
)
func ConnectionTest(c *ndh.Ctx) error {
type Req struct {
Name string `json:"name"`
Endpoint string `json:"endpoint"`
Access string `json:"access"`
Key string `json:"key"`
}
var (
err error
req = new(Req)
)
if err = c.ReqParse(req); err != nil {
return err
}
return c.Send200("test success")
}

68
internal/ndh/ctx.go Normal file
View File

@ -0,0 +1,68 @@
package ndh
import (
"context"
"encoding/json"
"io"
)
type Ctx struct {
ctx context.Context
req *json.Decoder
res io.Writer
}
func NewCtx(ctx context.Context, req *json.Decoder, res io.Writer) *Ctx {
return &Ctx{
ctx: ctx,
req: req,
res: res,
}
}
func (c *Ctx) Write(bs []byte) (int, error) {
return c.res.Write(bs)
}
func (c *Ctx) ReqParse(req any) error {
return c.req.Decode(req)
}
func (c *Ctx) Send200(data any, msg ...string) error {
m := "操作成功"
if len(msg) > 0 && msg[0] != "" {
m = msg[0]
}
return c.Send(200, m, "", data)
}
func (c *Ctx) Send400(data any, msg ...string) error {
m := "参数错误"
if len(msg) > 0 && msg[0] != "" {
m = msg[0]
}
return c.Send(400, m, "", data)
}
func (c *Ctx) Send500(data any, msg ...string) error {
m := "系统错误"
if len(msg) > 0 && msg[0] != "" {
m = msg[0]
}
return c.Send(500, m, "", data)
}
func (c *Ctx) Send(status uint32, msg, error string, data any) error {
value := map[string]any{"status": status, "msg": msg, "err": error, "data": data}
bs, err := json.Marshal(value)
if err != nil {
return err
}
_, err = c.Write(bs)
return err
}

3
internal/ndh/handler.go Normal file
View File

@ -0,0 +1,3 @@
package ndh
type Handler func(c *Ctx) error

5
internal/opt/var.go Normal file
View File

@ -0,0 +1,5 @@
package opt
var (
Debug bool
)

38
internal/tool/ctx.go Normal file
View File

@ -0,0 +1,38 @@
package tool
import (
"context"
"time"
)
func Timeout(seconds ...int) (ctx context.Context) {
var (
duration time.Duration
)
if len(seconds) > 0 && seconds[0] > 0 {
duration = time.Duration(seconds[0]) * time.Second
} else {
duration = time.Duration(30) * time.Second
}
ctx, _ = context.WithTimeout(context.Background(), duration)
return
}
func TimeoutCtx(ctx context.Context, seconds ...int) context.Context {
var (
duration time.Duration
)
if len(seconds) > 0 && seconds[0] > 0 {
duration = time.Duration(seconds[0]) * time.Second
} else {
duration = time.Duration(30) * time.Second
}
nctx, _ := context.WithTimeout(ctx, duration)
return nctx
}