🎉 搭完基本框架
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()
|
||||
}
|
Reference in New Issue
Block a user