46 lines
836 B
Go
46 lines
836 B
Go
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()
|
|
}
|