feat: migrate pocketbase to v0.23
This commit is contained in:
41
internal/rest/handlers/notify.go
Normal file
41
internal/rest/handlers/notify.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/pocketbase/pocketbase/core"
|
||||
"github.com/pocketbase/pocketbase/tools/router"
|
||||
|
||||
"github.com/usual2970/certimate/internal/domain"
|
||||
"github.com/usual2970/certimate/internal/rest/resp"
|
||||
)
|
||||
|
||||
type notifyService interface {
|
||||
Test(ctx context.Context, req *domain.NotifyTestPushReq) error
|
||||
}
|
||||
|
||||
type NotifyHandler struct {
|
||||
service notifyService
|
||||
}
|
||||
|
||||
func NewNotifyHandler(router *router.RouterGroup[*core.RequestEvent], service notifyService) {
|
||||
handler := &NotifyHandler{
|
||||
service: service,
|
||||
}
|
||||
|
||||
group := router.Group("/notify")
|
||||
group.POST("/test", handler.test)
|
||||
}
|
||||
|
||||
func (handler *NotifyHandler) test(e *core.RequestEvent) error {
|
||||
req := &domain.NotifyTestPushReq{}
|
||||
if err := e.BindBody(req); err != nil {
|
||||
return resp.Err(e, err)
|
||||
}
|
||||
|
||||
if err := handler.service.Test(e.Request.Context(), req); err != nil {
|
||||
return resp.Err(e, err)
|
||||
}
|
||||
|
||||
return resp.Ok(e, nil)
|
||||
}
|
||||
36
internal/rest/handlers/statistics.go
Normal file
36
internal/rest/handlers/statistics.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/pocketbase/pocketbase/core"
|
||||
"github.com/pocketbase/pocketbase/tools/router"
|
||||
|
||||
"github.com/usual2970/certimate/internal/domain"
|
||||
"github.com/usual2970/certimate/internal/rest/resp"
|
||||
)
|
||||
|
||||
type statisticsService interface {
|
||||
Get(ctx context.Context) (*domain.Statistics, error)
|
||||
}
|
||||
|
||||
type StatisticsHandler struct {
|
||||
service statisticsService
|
||||
}
|
||||
|
||||
func NewStatisticsHandler(router *router.RouterGroup[*core.RequestEvent], service statisticsService) {
|
||||
handler := &StatisticsHandler{
|
||||
service: service,
|
||||
}
|
||||
|
||||
group := router.Group("/statistics")
|
||||
group.GET("/get", handler.get)
|
||||
}
|
||||
|
||||
func (handler *StatisticsHandler) get(e *core.RequestEvent) error {
|
||||
if statistics, err := handler.service.Get(e.Request.Context()); err != nil {
|
||||
return resp.Err(e, err)
|
||||
} else {
|
||||
return resp.Ok(e, statistics)
|
||||
}
|
||||
}
|
||||
42
internal/rest/handlers/workflow.go
Normal file
42
internal/rest/handlers/workflow.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/pocketbase/pocketbase/core"
|
||||
"github.com/pocketbase/pocketbase/tools/router"
|
||||
|
||||
"github.com/usual2970/certimate/internal/domain"
|
||||
"github.com/usual2970/certimate/internal/rest/resp"
|
||||
)
|
||||
|
||||
type workflowService interface {
|
||||
Run(ctx context.Context, req *domain.WorkflowRunReq) error
|
||||
Stop(ctx context.Context)
|
||||
}
|
||||
|
||||
type WorkflowHandler struct {
|
||||
service workflowService
|
||||
}
|
||||
|
||||
func NewWorkflowHandler(router *router.RouterGroup[*core.RequestEvent], service workflowService) {
|
||||
handler := &WorkflowHandler{
|
||||
service: service,
|
||||
}
|
||||
|
||||
group := router.Group("/workflow")
|
||||
group.POST("/run", handler.run)
|
||||
}
|
||||
|
||||
func (handler *WorkflowHandler) run(e *core.RequestEvent) error {
|
||||
req := &domain.WorkflowRunReq{}
|
||||
if err := e.BindBody(req); err != nil {
|
||||
return resp.Err(e, err)
|
||||
}
|
||||
|
||||
if err := handler.service.Run(e.Request.Context(), req); err != nil {
|
||||
return resp.Err(e, err)
|
||||
}
|
||||
|
||||
return resp.Ok(e, nil)
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
package rest
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/usual2970/certimate/internal/domain"
|
||||
"github.com/usual2970/certimate/internal/rest/resp"
|
||||
|
||||
"github.com/labstack/echo/v5"
|
||||
)
|
||||
|
||||
type notifyService interface {
|
||||
Test(ctx context.Context, req *domain.NotifyTestPushReq) error
|
||||
}
|
||||
|
||||
type NotifyHandler struct {
|
||||
service notifyService
|
||||
}
|
||||
|
||||
func NewNotifyHandler(route *echo.Group, service notifyService) {
|
||||
handler := &NotifyHandler{
|
||||
service: service,
|
||||
}
|
||||
|
||||
group := route.Group("/notify")
|
||||
group.POST("/test", handler.test)
|
||||
}
|
||||
|
||||
func (handler *NotifyHandler) test(c echo.Context) error {
|
||||
req := &domain.NotifyTestPushReq{}
|
||||
if err := c.Bind(req); err != nil {
|
||||
return resp.Err(c, err)
|
||||
}
|
||||
|
||||
if err := handler.service.Test(c.Request().Context(), req); err != nil {
|
||||
return resp.Err(c, err)
|
||||
}
|
||||
|
||||
return resp.Ok(c, nil)
|
||||
}
|
||||
@@ -3,7 +3,7 @@ package resp
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/labstack/echo/v5"
|
||||
"github.com/pocketbase/pocketbase/core"
|
||||
|
||||
"github.com/usual2970/certimate/internal/domain"
|
||||
)
|
||||
@@ -14,7 +14,7 @@ type Response struct {
|
||||
Data interface{} `json:"data"`
|
||||
}
|
||||
|
||||
func Ok(e echo.Context, data interface{}) error {
|
||||
func Ok(e *core.RequestEvent, data interface{}) error {
|
||||
rs := &Response{
|
||||
Code: 0,
|
||||
Msg: "success",
|
||||
@@ -23,7 +23,7 @@ func Ok(e echo.Context, data interface{}) error {
|
||||
return e.JSON(http.StatusOK, rs)
|
||||
}
|
||||
|
||||
func Err(e echo.Context, err error) error {
|
||||
func Err(e *core.RequestEvent, err error) error {
|
||||
code := 500
|
||||
|
||||
xerr, ok := err.(*domain.Error)
|
||||
|
||||
44
internal/rest/routes/routes.go
Normal file
44
internal/rest/routes/routes.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/pocketbase/pocketbase/apis"
|
||||
"github.com/pocketbase/pocketbase/core"
|
||||
"github.com/pocketbase/pocketbase/tools/router"
|
||||
|
||||
"github.com/usual2970/certimate/internal/notify"
|
||||
"github.com/usual2970/certimate/internal/repository"
|
||||
"github.com/usual2970/certimate/internal/rest/handlers"
|
||||
"github.com/usual2970/certimate/internal/statistics"
|
||||
"github.com/usual2970/certimate/internal/workflow"
|
||||
)
|
||||
|
||||
var (
|
||||
notifySvc *notify.NotifyService
|
||||
workflowSvc *workflow.WorkflowService
|
||||
statisticsSvc *statistics.StatisticsService
|
||||
)
|
||||
|
||||
func Register(router *router.Router[*core.RequestEvent]) {
|
||||
notifyRepo := repository.NewSettingsRepository()
|
||||
notifySvc = notify.NewNotifyService(notifyRepo)
|
||||
|
||||
workflowRepo := repository.NewWorkflowRepository()
|
||||
workflowSvc = workflow.NewWorkflowService(workflowRepo)
|
||||
|
||||
statisticsRepo := repository.NewStatisticsRepository()
|
||||
statisticsSvc = statistics.NewStatisticsService(statisticsRepo)
|
||||
|
||||
group := router.Group("/api")
|
||||
group.Bind(apis.RequireSuperuserAuth())
|
||||
handlers.NewWorkflowHandler(group, workflowSvc)
|
||||
handlers.NewNotifyHandler(group, notifySvc)
|
||||
handlers.NewStatisticsHandler(group, statisticsSvc)
|
||||
}
|
||||
|
||||
func Unregister() {
|
||||
if workflowSvc != nil {
|
||||
workflowSvc.Stop(context.Background())
|
||||
}
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
package rest
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/labstack/echo/v5"
|
||||
"github.com/usual2970/certimate/internal/domain"
|
||||
"github.com/usual2970/certimate/internal/rest/resp"
|
||||
)
|
||||
|
||||
type statisticsService interface {
|
||||
Get(ctx context.Context) (*domain.Statistics, error)
|
||||
}
|
||||
|
||||
type StatisticsHandler struct {
|
||||
service statisticsService
|
||||
}
|
||||
|
||||
func NewStatisticsHandler(route *echo.Group, service statisticsService) {
|
||||
handler := &StatisticsHandler{
|
||||
service: service,
|
||||
}
|
||||
|
||||
group := route.Group("/statistics")
|
||||
group.GET("/get", handler.get)
|
||||
}
|
||||
|
||||
func (handler *StatisticsHandler) get(c echo.Context) error {
|
||||
if statistics, err := handler.service.Get(c.Request().Context()); err != nil {
|
||||
return resp.Err(c, err)
|
||||
} else {
|
||||
return resp.Ok(c, statistics)
|
||||
}
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
package rest
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/labstack/echo/v5"
|
||||
"github.com/usual2970/certimate/internal/domain"
|
||||
"github.com/usual2970/certimate/internal/rest/resp"
|
||||
)
|
||||
|
||||
type workflowService interface {
|
||||
Run(ctx context.Context, req *domain.WorkflowRunReq) error
|
||||
Stop(ctx context.Context)
|
||||
}
|
||||
|
||||
type WorkflowHandler struct {
|
||||
service workflowService
|
||||
}
|
||||
|
||||
func NewWorkflowHandler(route *echo.Group, service workflowService) {
|
||||
handler := &WorkflowHandler{
|
||||
service: service,
|
||||
}
|
||||
|
||||
group := route.Group("/workflow")
|
||||
group.POST("/run", handler.run)
|
||||
}
|
||||
|
||||
func (handler *WorkflowHandler) run(c echo.Context) error {
|
||||
req := &domain.WorkflowRunReq{}
|
||||
if err := c.Bind(req); err != nil {
|
||||
return resp.Err(c, err)
|
||||
}
|
||||
|
||||
if err := handler.service.Run(c.Request().Context(), req); err != nil {
|
||||
return resp.Err(c, err)
|
||||
}
|
||||
|
||||
return resp.Ok(c, nil)
|
||||
}
|
||||
Reference in New Issue
Block a user