-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplugin.go
102 lines (86 loc) · 2.51 KB
/
plugin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Package web provides a launchr plugin with Web UI for launchr.
package web
import (
"context"
_ "embed"
"fmt"
"os"
"path/filepath"
"github.com/launchrctl/launchr"
"github.com/launchrctl/launchr/pkg/action"
)
const (
pluginName = "web"
stopArg = "stop"
pidFile = "web.pid"
// APIPrefix is a default api prefix on the server.
APIPrefix = "/api"
)
//go:embed action.yaml
var actionYaml []byte
func init() {
launchr.RegisterPlugin(&Plugin{})
}
// Plugin is [launchr.Plugin] providing web ui.
type Plugin struct {
app launchr.App
cfg launchr.Config
}
// PluginInfo implements [launchr.Plugin] interface.
func (p *Plugin) PluginInfo() launchr.PluginInfo {
return launchr.PluginInfo{}
}
// OnAppInit implements [launchr.OnAppInitPlugin] interface.
func (p *Plugin) OnAppInit(app launchr.App) error {
app.GetService(&p.cfg)
p.app = app
return nil
}
type webFlags struct {
Port int
IsPortSet bool
ProxyClient string
UseSwaggerUI bool
PluginDir string
}
// DiscoverActions implements [launchr.ActionDiscoveryPlugin] interface.
func (p *Plugin) DiscoverActions(_ context.Context) ([]*action.Action, error) {
a := action.NewFromYAML("web", actionYaml)
a.SetRuntime(action.NewFnRuntime(func(ctx context.Context, a *action.Action) error {
pluginTmpDir := p.cfg.Path(pluginName)
webPidFile := filepath.Join(pluginTmpDir, pidFile)
input := a.Input()
webRunFlags := webFlags{
PluginDir: pluginTmpDir,
Port: input.Opt("port").(int),
IsPortSet: input.IsOptChanged("port"),
UseSwaggerUI: input.Opt("swagger-ui").(bool),
ProxyClient: input.Opt("proxy-client").(string),
}
foreground := input.Opt("foreground").(bool)
// Override client assets.
clientAssets := input.Opt("ui-assets").(string)
if clientAssets != "" {
path := launchr.MustAbs(clientAssets)
_, err := os.Stat(path)
if os.IsNotExist(err) {
return fmt.Errorf("ui assets are not available on path: %s", path)
}
SetClientAssetsFS(os.DirFS(path))
}
// If 'stop' arg passed, try to interrupt process and remove PID file.
op := input.Arg("op")
switch op {
case stopArg:
return stopWeb(webPidFile, webRunFlags.PluginDir)
}
if url, _ := getExistingWeb(webPidFile, webRunFlags.PluginDir); url != "" {
return fmt.Errorf("another web UI is already running at %s\nPlease stop the existing server before starting a new one", url)
}
if foreground {
return p.runWeb(ctx, webRunFlags)
}
return p.runBackgroundWeb(ctx, webRunFlags, webPidFile)
}))
return []*action.Action{a}, nil
}