🎉 搭完基本框架
This commit is contained in:
18
internal/controller/api.go
Normal file
18
internal/controller/api.go
Normal 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)
|
||||
}
|
45
internal/controller/app.go
Normal file
45
internal/controller/app.go
Normal 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()
|
||||
}
|
25
internal/handler/connection.go
Normal file
25
internal/handler/connection.go
Normal 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
68
internal/ndh/ctx.go
Normal 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
3
internal/ndh/handler.go
Normal file
@ -0,0 +1,3 @@
|
||||
package ndh
|
||||
|
||||
type Handler func(c *Ctx) error
|
5
internal/opt/var.go
Normal file
5
internal/opt/var.go
Normal file
@ -0,0 +1,5 @@
|
||||
package opt
|
||||
|
||||
var (
|
||||
Debug bool
|
||||
)
|
38
internal/tool/ctx.go
Normal file
38
internal/tool/ctx.go
Normal 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
|
||||
}
|
Reference in New Issue
Block a user