forked from kataras/iris
/
editor.go
218 lines (179 loc) · 5.45 KB
/
editor.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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package editor
/* Package editor provides alm-tools cloud editor automation for the iris web framework.
Usage:
import "github.com/kataras/iris/typescript/editor"
[...]
app := iris.New()
e := editor.New(editor.Config{})
e.Run(app.Logger().Infof)
[...]
app.Run(iris.Addr(":8080"))
e.Stop()
General notes for authentication
The Authorization specifies the authentication mechanism (in this case Basic) followed by the username and password.
Although, the string aHR0cHdhdGNoOmY= may look encrypted it is simply a base64 encoded version of <username>:<password>.
Would be readily available to anyone who could intercept the HTTP request.
*/
import (
"bufio"
"io"
"os"
"path/filepath"
"strconv"
"github.com/kataras/iris/typescript/npm"
)
type (
// Editor is the alm-tools adaptor.
//
// It holds a logger from the iris' station
// username,password for basic auth
// directory which the client side code is
// keyfile,certfile for TLS listening
// and a host which is listening for
Editor struct {
config *Config
log func(format string, a ...interface{})
enabled bool // default true
// after alm started
process *os.Process
debugOutput io.Writer
}
)
var (
// NoOpLogger can be used as the logger argument, it prints nothing.
NoOpLogger = func(string, ...interface{}) {}
)
// New creates and returns an Editor Plugin instance
func New(cfg ...Config) *Editor {
c := DefaultConfig()
if len(cfg) > 0 {
c = cfg[0]
}
c.WorkingDir = validateWorkingDir(c.WorkingDir) // add "/" if not exists
return &Editor{
enabled: true,
config: &c,
}
}
// User set a user, accepts two parameters: username (string), string (string)
func (e *Editor) User(username string, password string) *Editor {
e.config.Username = username
e.config.Password = password
return e
}
func validateWorkingDir(workingDir string) string {
l := workingDir[len(workingDir)-1]
if l != '/' && l != os.PathSeparator {
workingDir += "/"
}
return workingDir
}
// Dir sets the directory which the client side source code alive
func (e *Editor) Dir(workingDir string) *Editor {
e.config.WorkingDir = validateWorkingDir(workingDir)
return e
}
// Port sets the port (int) for the editor adaptor's standalone server
func (e *Editor) Port(port int) *Editor {
e.config.Port = port
return e
}
// SetEnable if true enables the editor adaptor, otherwise disables it
func (e *Editor) SetEnable(enable bool) {
e.enabled = enable
}
// DisableOutput call that if you don't care about alm-tools' messages
// they are useful because that the default configuration shows them
func (e *Editor) DisableOutput() {
e.config.DisableOutput = true
}
// GetDescription EditorPlugin is a bridge between iris and the alm-tools, the browser-based IDE for client-side sources.
func (e *Editor) GetDescription() string {
return "A bridge between iris and the alm-tools, the browser-based IDE."
}
// we use that editorWriter to prefix the editor's output with "Editor Adaptor: "
type editorWriter struct {
underline io.Writer
}
// Run starts the editor's server.
//
// Developers should call the `Stop` to shutdown the editor's server when main server will be closed.
func (e *Editor) Run(logger func(format string, a ...interface{})) {
if logger == nil {
logger = NoOpLogger
}
e.log = logger
if e.config.Hostname == "" {
e.config.Hostname = "0.0.0.0"
}
if e.config.Port <= 0 {
e.config.Port = DefaultPort
}
if s, err := filepath.Abs(e.config.WorkingDir); err == nil {
e.config.WorkingDir = s
}
e.start()
}
// Stop kills the editor's server.
func (e *Editor) Stop() {
if e.process != nil {
err := e.process.Kill()
if err != nil {
e.log("error while trying to terminate the Editor,please kill this process by yourself, process id: %d", e.process.Pid)
}
}
}
// start starts the job
func (e *Editor) start() {
if e.config.Username == "" || e.config.Password == "" {
e.log("error before running alm-tools. You have to set username & password for security reasons, otherwise this adaptor won't run.")
return
}
if !npm.NodeModuleExists("alm/bin/alm") {
e.log("installing alm-tools, please wait...")
res := npm.NodeModuleInstall("alm")
if res.Error != nil {
e.log(res.Error.Error())
return
}
e.log(res.Message)
}
cmd := npm.CommandBuilder("node", npm.NodeModuleAbs("alm/src/server.js"))
cmd.AppendArguments("-a", e.config.Username+":"+e.config.Password,
"-h", e.config.Hostname, "-t", strconv.Itoa(e.config.Port), "-d", e.config.WorkingDir)
// for auto-start in the browser: cmd.AppendArguments("-o")
if e.config.KeyFile != "" && e.config.CertFile != "" {
cmd.AppendArguments("--httpskey", e.config.KeyFile, "--httpscert", e.config.CertFile)
}
prefix := ""
// when debug is not disabled
// show any messages to the user( they are useful here)
// to the io.Writer that iris' user is defined from configuration
if !e.config.DisableOutput {
outputReader, err := cmd.StdoutPipe()
if err == nil {
outputScanner := bufio.NewScanner(outputReader)
go func() {
for outputScanner.Scan() {
e.log(prefix + outputScanner.Text())
}
}()
errReader, err := cmd.StderrPipe()
if err == nil {
errScanner := bufio.NewScanner(errReader)
go func() {
for errScanner.Scan() {
e.log(prefix + errScanner.Text())
}
}()
}
}
}
err := cmd.Start()
if err != nil {
e.log(prefix + err.Error())
return
}
// no need, alm-tools post these
// e.logger.Printf("Editor is running at %s:%d | %s", e.config.Hostname, e.config.Port, e.config.WorkingDir)
}