Skip to content

Commit

Permalink
plugin && tools
Browse files Browse the repository at this point in the history
  • Loading branch information
northseadl committed Oct 11, 2023
1 parent ff22649 commit f985811
Show file tree
Hide file tree
Showing 36 changed files with 2,862 additions and 239 deletions.
28 changes: 10 additions & 18 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,31 +1,23 @@
.idea
.idea/
*.iml
.DS_Store
**/.DS_Store

*.log
.logs/
.var
.run

resource/static
temp

wechat.log


.build
*.log
logs/


resource/static
etc/powerx-*
etc/powerx.yaml
/scrm.mk
/app1
/ctl
.deploy/*
.build
powerx.go
powerxctl
powerx
/data/*

!internal/uc/powerx
!cmd/server/powerx.go

/deploy/
/powerx.go
/plugins
11 changes: 11 additions & 0 deletions api/health.api
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
syntax = "v1"

service PowerX {
@doc "健康检查接口"
@handler HealthCheck
get /health (HealthCheckRequest) returns (HealthCheckReply)
}

type HealthCheckRequest {}

type HealthCheckReply {}
71 changes: 71 additions & 0 deletions api/plugin.api
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
syntax = "v1"

service PowerX {
@doc "插件接口"
@handler RegisterPlugin
post /plugin/v1/plugins (RegisterPluginRequest) returns (RegisterPluginReply)

@doc "插件列表拉取"
@handler ListPlugin
get /plugin/v1/plugins (ListPluginRequest) returns (ListPluginReply)

@doc "插件路由拉取"
@handler ListPluginFrontendRoutes
get /plugin/v1/frontend-routes returns (ListPluginFrontendRoutesReply)
}

type (
Route struct {
Method string `json:"method"`
Path string `json:"path"`
}

RegisterPluginRequest {
Name string `json:"name"`
Routes []Route `json:"routes"`
Addr string `json:"addr"`
}

RegisterPluginReply {
Name string `json:"name"`
etc map[string]interface{} `json:"etc"`
}
)

type (
PluginWebRouteMeta {
Locale string `json:"locale"`
Icon string `json:"icon"`
RequestAuth bool `json:"requestAuth"`
}

PluginWebRoutes struct {
Name string `json:"name"`
Path string `json:"path"`
Meta PluginWebRouteMeta `json:"meta"`
Children []PluginWebRoutes `json:"children"`
}

ListPluginRequest {
// Scope ['routes']
Socpe []string `form:"scope"`
}

PluginWebInfo struct {
Name string `json:"name"`
Desc string `json:"desc"`
Verison string `json:"version"`
IsEnabled bool `json:"isEnabled"`
Routes []Route `json:"routes"`
}

ListPluginReply {
Plugins []PluginWebInfo `json:"plugins"`
}
)

type (
ListPluginFrontendRoutesReply {
Routes []PluginWebRoutes `json:"routes"`
}
)
1 change: 0 additions & 1 deletion api/plugin/README.md

This file was deleted.

1 change: 1 addition & 0 deletions api/powerx.api
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import "web.api"

import "custom.api"
import "plugin.api"
import "health.api"

service PowerX {
@doc "根目录访问"
Expand Down
81 changes: 81 additions & 0 deletions cmd/devctl/devctl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package main

import (
"PowerX/cmd/devctl/plugin"
"PowerX/pkg/pluginx"
"fmt"
"github.com/urfave/cli/v2"
"github.com/zeromicro/go-zero/tools/goctl/api/parser"
"log"
"os"
)

func main() {
app := &cli.App{
Name: "powerd",
Usage: "Manage plugins for PowerXDashboard",
Commands: []*cli.Command{
{
Name: "plugin",
Usage: "plugin usage",
Subcommands: []*cli.Command{
{
Name: "build",
Usage: "build plugin frontend",
Flags: []cli.Flag{
&cli.StringFlag{Name: "dir", Aliases: []string{"d"}, Value: "./plugins", Usage: "file dir"},
&cli.StringFlag{Name: "api", Aliases: []string{"a"}, Value: "/api/plugin", Usage: "api base url"},
},
Action: func(c *cli.Context) error {
loader := pluginx.NewLoader(c.String("dir"), &pluginx.BuildLoaderConfig{
MainAPIEndpoint: c.String("api"),
})
err := loader.CheckEnvDependency()
if err != nil {
return err
}
err = loader.UnArchives()
if err != nil {
return err
}
err = loader.BuildPluginFrontend(pluginx.BuildPluginFrontendOptions{
ReDownload: true,
})
if err != nil {
return err
}
return nil
},
},
{
Name: "gen-client-api",
Usage: "generate client api",
Flags: []cli.Flag{
&cli.StringFlag{Name: "dir", Aliases: []string{"d"}, Value: "./plugin-client", Usage: "target dir"},
},
Action: func(c *cli.Context) error {
filePath := "api/powerx.api"
api, err := parser.Parse(filePath)
if err != nil {
fmt.Println("Error parsing .api file:", err)
return err
}

err = plugin.GenerateClientCode(api, c.String("dir"))
if err != nil {
fmt.Println("Error generating client code:", err)
return err
}
return nil
},
},
},
},
},
}

err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
27 changes: 27 additions & 0 deletions cmd/devctl/plugin/client.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package {{.PackageName}}

import (
{{if .HasTypesImport}}"{{.TypesImportPath}}"{{end}}
"context"
{{if .HasFmtImport}}"fmt"{{end}}
{{if .HasNetHttpImport}}"net/http"{{end}}
)

type {{.GroupName}} struct {
*PowerX
}

{{range .Routes}}
func (c *{{$.GroupName}}) {{.Handler}}(ctx context.Context, {{if ne .RequestTypeName ""}}req *powerxtypes.{{.RequestTypeName}}{{end}}) (*powerxtypes.{{.ResponseTypeName}}, error) {
res := &powerxtypes.{{.ResponseTypeName}}{}
err := c.H.Df().Method(http.Method{{CapFirst .Method}}).
WithContext(ctx).
Uri({{FormatPath .Path .Handler $.PathParamsMap}}).
{{if and (ne (ToUpper .Method) "GET") (ne .RequestTypeName "")}}Json(req).{{else if and (eq (ToUpper .Method) "GET") (ne .RequestTypeName "")}}BindQuery(req).{{end}}
Result(res)
if err != nil {
return nil, err
}
return res, nil
}
{{end}}
Loading

0 comments on commit f985811

Please sign in to comment.