🎉 开始项目
feat: 完成基础界面; 列表展示 todo: uplevel button function todo: download/upload
This commit is contained in:
38
internal/controller/app.go
Normal file
38
internal/controller/app.go
Normal file
@ -0,0 +1,38 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/loveuer/nf-disk/internal/api"
|
||||
"github.com/loveuer/nf-disk/internal/db"
|
||||
"github.com/loveuer/nf-disk/internal/manager"
|
||||
"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"
|
||||
)
|
||||
|
||||
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) Init(ctx context.Context) {
|
||||
log.Info("app init!!!")
|
||||
|
||||
a.ctx = ctx
|
||||
|
||||
tool.Must(db.Init(ctx, "sqlite::memory", db.OptSqliteByMem(nil)))
|
||||
tool.Must(model.Init(db.Default.Session()))
|
||||
tool.Must(manager.Init(ctx))
|
||||
tool.Must(api.Init(ctx))
|
||||
}
|
||||
|
||||
func (a *App) Startup(ctx context.Context) {
|
||||
log.Info("app startup!!!")
|
||||
}
|
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