wip: alpha version
This commit is contained in:
60
internal/api/api.go
Normal file
60
internal/api/api.go
Normal file
@ -0,0 +1,60 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/loveuer/nf"
|
||||
"github.com/loveuer/nfflow/internal/handler"
|
||||
"github.com/loveuer/nfflow/internal/middleware/auth"
|
||||
"github.com/loveuer/nfflow/internal/middleware/front"
|
||||
"github.com/loveuer/nfflow/internal/middleware/oplog"
|
||||
"github.com/loveuer/nfflow/internal/middleware/privilege"
|
||||
"github.com/loveuer/nfflow/internal/model"
|
||||
"time"
|
||||
)
|
||||
|
||||
func initApp(ctx context.Context) *nf.App {
|
||||
engine := nf.New(nf.Config{})
|
||||
|
||||
app := engine.Group("/api")
|
||||
app.Get("/available", func(c *nf.Ctx) error {
|
||||
return c.JSON(nf.Map{"status": 200, "ok": true, "time": time.Now()})
|
||||
})
|
||||
|
||||
{
|
||||
api := app.Group("/user")
|
||||
api.Post("/auth/login", oplog.NewOpLog(ctx), handler.AuthLogin)
|
||||
api.Get("/auth/login", auth.NewAuth(), handler.AuthVerify)
|
||||
api.Post("/auth/logout", auth.NewAuth(), oplog.NewOpLog(ctx), handler.AuthLogout)
|
||||
|
||||
mng := api.Group("/manage")
|
||||
mng.Use(auth.NewAuth(), privilege.Verify(
|
||||
privilege.RelationAnd,
|
||||
model.PrivilegeUserManage,
|
||||
))
|
||||
|
||||
mng.Get("/user/list", handler.ManageUserList)
|
||||
mng.Post("/user/create", oplog.NewOpLog(ctx), handler.ManageUserCreate)
|
||||
mng.Post("/user/update", oplog.NewOpLog(ctx), handler.ManageUserUpdate)
|
||||
mng.Post("/user/delete", oplog.NewOpLog(ctx), handler.ManageUserDelete)
|
||||
}
|
||||
|
||||
{
|
||||
api := app.Group("/log")
|
||||
api.Use(auth.NewAuth(), privilege.Verify(privilege.RelationAnd, model.PrivilegeOpLog))
|
||||
api.Get("/category/list", handler.LogCategories())
|
||||
api.Get("/content/list", handler.LogList)
|
||||
}
|
||||
|
||||
{
|
||||
api := app.Group("/task")
|
||||
|
||||
api.Get("/list", handler.TaskList)
|
||||
api.Post("/create", handler.TaskCreate)
|
||||
api.Post("/input/create", handler.TaskInputCreate)
|
||||
api.Post("/input/update", handler.TaskInputUpdate)
|
||||
}
|
||||
|
||||
engine.Use(front.NewFront(&front.DefaultFront, "dist/front/browser"))
|
||||
|
||||
return engine
|
||||
}
|
40
internal/api/start.go
Normal file
40
internal/api/start.go
Normal file
@ -0,0 +1,40 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/loveuer/nfflow/internal/opt"
|
||||
"github.com/loveuer/nfflow/internal/util"
|
||||
"github.com/sirupsen/logrus"
|
||||
"net"
|
||||
)
|
||||
|
||||
func MustStart(ctx context.Context) {
|
||||
|
||||
app := initApp(ctx)
|
||||
ready := make(chan bool)
|
||||
|
||||
ln, err := net.Listen("tcp", opt.Cfg.Address)
|
||||
if err != nil {
|
||||
logrus.Panicf("api.MustStart: net listen tcp address=%v err=%v", opt.Cfg.Address, err)
|
||||
}
|
||||
|
||||
go func() {
|
||||
ready <- true
|
||||
|
||||
if err = app.RunListener(ln); err != nil {
|
||||
logrus.Panicf("api.MustStart: app run err=%v", err)
|
||||
}
|
||||
}()
|
||||
|
||||
<-ready
|
||||
|
||||
go func() {
|
||||
ready <- true
|
||||
<-ctx.Done()
|
||||
if err = app.Shutdown(util.Timeout(1)); err != nil {
|
||||
logrus.Errorf("api.MustStart: app shutdown err=%v", err)
|
||||
}
|
||||
}()
|
||||
|
||||
<-ready
|
||||
}
|
Reference in New Issue
Block a user