feat: wrap message by fluent ui toast
This commit is contained in:
@ -1,18 +0,0 @@
|
||||
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)
|
||||
}
|
@ -1,13 +1,13 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"github.com/loveuer/nf-disk/internal/api"
|
||||
"github.com/loveuer/nf-disk/internal/db"
|
||||
"github.com/loveuer/nf-disk/internal/model"
|
||||
"github.com/loveuer/nf-disk/internal/tool"
|
||||
"github.com/loveuer/nf-disk/ndh"
|
||||
"github.com/loveuer/nf/nft/log"
|
||||
"nf-disk/internal/ndh"
|
||||
"nf-disk/internal/tool"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type App struct {
|
||||
@ -22,24 +22,11 @@ func NewApp() *App {
|
||||
}
|
||||
|
||||
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()
|
||||
|
||||
a.ctx = ctx
|
||||
|
||||
tool.Must(db.Init(ctx, "sqlite::memory", db.OptSqliteByMem(nil)))
|
||||
tool.Must(model.Init(db.Default.Session()))
|
||||
tool.Must(api.Init(ctx))
|
||||
}
|
||||
|
51
internal/controller/invoke.go
Normal file
51
internal/controller/invoke.go
Normal file
@ -0,0 +1,51 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/loveuer/nf-disk/internal/api"
|
||||
"github.com/loveuer/nf-disk/internal/opt"
|
||||
"github.com/loveuer/nf-disk/internal/tool"
|
||||
"github.com/loveuer/nf-disk/ndh"
|
||||
"github.com/loveuer/nf/nft/log"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func handleError(err error) string {
|
||||
bs, _ := json.Marshal(map[string]any{
|
||||
"err": err.Error(),
|
||||
"msg": opt.Msg500,
|
||||
"status": 500,
|
||||
})
|
||||
|
||||
return string(bs)
|
||||
}
|
||||
|
||||
func handleNotFound(path string) string {
|
||||
bs, _ := json.Marshal(map[string]any{
|
||||
"err": fmt.Sprintf("path not found, path: %s", path),
|
||||
"msg": opt.Msg500,
|
||||
"status": 404,
|
||||
})
|
||||
|
||||
return string(bs)
|
||||
}
|
||||
|
||||
func (a *App) Invoke(path string, req string) (res string) {
|
||||
log.Info("app invoke: path = %s, req = %s", path, req)
|
||||
handler, ok := api.Resolve(path)
|
||||
if !ok {
|
||||
log.Warn("app invoke: path not found, path = %s", path)
|
||||
return handleNotFound(path)
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
ctx := ndh.NewCtx(tool.TimeoutCtx(a.ctx), strings.NewReader(req), &buf)
|
||||
|
||||
if err := handler(ctx); err != nil {
|
||||
return handleError(err)
|
||||
}
|
||||
|
||||
return buf.String()
|
||||
}
|
Reference in New Issue
Block a user