-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathplugin.go
More file actions
164 lines (138 loc) · 4.06 KB
/
plugin.go
File metadata and controls
164 lines (138 loc) · 4.06 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// 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"
"github.com/launchrctl/web/server"
)
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
//go:embed ui-schema.default.yaml
var defaultUISchema []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 {
action.WithLogger
action.WithTerm
Port int
IsPortSet bool
ProxyClient string
PluginDir string
FrontendCustomize server.FrontendCustomize
DefaultUISchema []byte
}
// 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"),
ProxyClient: input.Opt("proxy-client").(string),
FrontendCustomize: server.FrontendCustomize{
Variables: make(map[string]any),
ExcludedActions: make(map[string]bool),
},
DefaultUISchema: defaultUISchema,
}
// Retrieve a list of excluded actions from config.
var excludedActions []string
err := p.cfg.Get("web.excluded_actions", &excludedActions)
if err != nil {
return err
}
for _, ea := range excludedActions {
webRunFlags.FrontendCustomize.ExcludedActions[ea] = true
}
var variables map[string]any
err = p.cfg.Get("web.variables", &variables)
if err != nil {
return err
}
if variables != nil {
webRunFlags.FrontendCustomize.Variables = variables
}
// Set action logger. Fallback to default launchr logger.
log := launchr.Log()
if rt, ok := a.Runtime().(action.RuntimeLoggerAware); ok {
log = rt.LogWith()
}
webRunFlags.SetLogger(log)
// Set action term. Fallback to default launchr term.
term := launchr.Term()
if rt, ok := a.Runtime().(action.RuntimeTermAware); ok {
term = rt.Term()
}
webRunFlags.SetTerm(term)
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))
}
swaggerUI := input.Opt("swagger-ui").(string)
if swaggerUI != "" {
path := launchr.MustAbs(swaggerUI)
_, err := os.Stat(path)
if os.IsNotExist(err) {
return fmt.Errorf("swagger UI is not available on path: %s", path)
}
SetSwaggerUIAssetsFS(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)
}
url, err := getExistingWeb(webPidFile, webRunFlags.PluginDir)
if err != nil {
launchr.Log().Debug("error on getting server run info", "error", err)
}
if 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
}