Skip to content

Commit 349b5ae

Browse files
committed
add options for time, id and app name
1 parent 1aaa749 commit 349b5ae

File tree

4 files changed

+31
-23
lines changed

4 files changed

+31
-23
lines changed

README.md

+8-5
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,34 @@ go build
1919
-p, --password= BasicAuth password
2020
-l, --log-buffer-size= Log buffer size (default: 200)
2121
-i, --interval= PM2 process-list update interval in seconds (default: 10)
22-
--actions Whether to show start, stop and restart buttons or not
22+
--time Show log time
23+
--app-id Show app id
24+
--app-name Show app name
25+
--actions Show start, stop and restart buttons
2326
```
2427

2528
## Example
2629

2730
### Run without authentication:
2831

2932
```
30-
./pm2-web localhost:3030
33+
./pm2-web --time --app-name --actions localhost:3030
3134
```
3235

3336
**or using PM2:**
3437
```
35-
pm2 start --name pm2-web ./pm2-web -- localhost:3030
38+
pm2 start --name pm2-web ./pm2-web -- --time --app-name --actions localhost:3030
3639
```
3740

3841
### Run with authentication:
3942

4043
```
41-
./pm2-web -u admin -p 1234 localhost:3030
44+
./pm2-web -u admin -p 1234 --time --app-name --actions localhost:3030
4245
```
4346

4447
**or using PM2:**
4548
```
46-
pm2 start --name pm2-web ./pm2-web -- -u admin -p 1234 localhost:3030
49+
pm2 start --name pm2-web ./pm2-web -- -u admin -p 1234 --time --app-name --actions localhost:3030
4750
```
4851

4952
### Run behind reverse proxy:

main.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,19 @@ import (
1010
"github.com/jessevdk/go-flags"
1111
)
1212

13-
var opts struct {
13+
type Options struct {
1414
Username string `short:"u" long:"username" description:"BasicAuth username" required:"false" default:""`
1515
Password string `short:"p" long:"password" description:"BasicAuth password" required:"false" default:""`
1616
LogBufferSize int `short:"l" long:"log-buffer-size" description:"Log buffer size" required:"false" default:"200"`
1717
Interval int `short:"i" long:"interval" description:"PM2 process-list update interval in seconds" required:"false" default:"10"`
18-
ActionsEnabled bool `long:"actions" description:"Whether to show start, stop and restart buttons or not" required:"false"`
18+
TimeEnabled bool `long:"time" description:"Show log time" required:"false"`
19+
IdEnabled bool `long:"app-id" description:"Show app id" required:"false"`
20+
AppNameEnabled bool `long:"app-name" description:"Show app name" required:"false"`
21+
ActionsEnabled bool `long:"actions" description:"Show start, stop and restart buttons"`
1922
}
2023

24+
var opts Options
25+
2126
type LogData struct {
2227
Type string
2328
Data interface{}
@@ -67,7 +72,7 @@ func main() {
6772
}()
6873

6974
pm2 := NewPM2(time.Duration(opts.Interval)*time.Second, opts.LogBufferSize).Run()
70-
if err := NewHTTPServer(args[0], opts.Username, opts.Password, opts.ActionsEnabled, pm2).Run(); err != nil {
75+
if err := NewHTTPServer(args[0], &opts, pm2).Run(); err != nil {
7176
log.Fatalln(err)
7277
}
7378
}

server.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,18 @@ type HttpServer struct {
1616
upgrader websocket.Upgrader
1717
}
1818

19-
func NewHTTPServer(addr, username, password string, actionsEnabled bool, pm2 *PM2) *HttpServer {
19+
func NewHTTPServer(addr string, options *Options, pm2 *PM2) *HttpServer {
2020
return (&HttpServer{
2121
upgrader: websocket.Upgrader{
2222
ReadBufferSize: 1024,
2323
WriteBufferSize: 1024,
2424
CheckOrigin: func(r *http.Request) bool { return true },
2525
},
2626
Addr: addr,
27-
}).init(username, password, actionsEnabled, pm2)
27+
}).init(options, pm2)
2828
}
2929

30-
func (s *HttpServer) init(username, password string, actionsEnabled bool, pm2 *PM2) *HttpServer {
30+
func (s *HttpServer) init(options *Options, pm2 *PM2) *HttpServer {
3131
staticHandler := http.FileServer(http.Dir("./static"))
3232

3333
jsHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@@ -37,7 +37,7 @@ func (s *HttpServer) init(username, password string, actionsEnabled bool, pm2 *P
3737
return
3838
}
3939
w.Header().Add("Content-Type", "text/javascript")
40-
err = templ.Execute(w, actionsEnabled)
40+
err = templ.Execute(w, options)
4141
if err != nil {
4242
fmt.Println(err)
4343
}
@@ -131,19 +131,19 @@ func (s *HttpServer) init(username, password string, actionsEnabled bool, pm2 *P
131131
}
132132
})
133133

134-
if username == "" {
134+
if options.Username == "" {
135135
http.Handle("/", staticHandler)
136136
http.Handle("/script.js", jsHandler)
137137
http.Handle("/logs", logsHandler)
138-
if actionsEnabled {
138+
if options.ActionsEnabled {
139139
http.Handle("/action", actionHandler)
140140
}
141141
} else {
142-
http.Handle("/", httpauth.SimpleBasicAuth(username, password)(staticHandler))
143-
http.Handle("/script.js", httpauth.SimpleBasicAuth(username, password)(jsHandler))
144-
http.Handle("/logs", httpauth.SimpleBasicAuth(username, password)(logsHandler))
145-
if actionsEnabled {
146-
http.Handle("/action", httpauth.SimpleBasicAuth(username, password)(actionHandler))
142+
http.Handle("/", httpauth.SimpleBasicAuth(options.Username, options.Password)(staticHandler))
143+
http.Handle("/script.js", httpauth.SimpleBasicAuth(options.Username, options.Password)(jsHandler))
144+
http.Handle("/logs", httpauth.SimpleBasicAuth(options.Username, options.Password)(logsHandler))
145+
if options.ActionsEnabled {
146+
http.Handle("/action", httpauth.SimpleBasicAuth(options.Username, options.Password)(actionHandler))
147147
}
148148
}
149149

static/script.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
const SHOW_ACTIONS = {{.}};
2-
const SHOW_TIME = true;
3-
const SHOW_ID = false;
4-
const SHOW_APP_NAME = true;
1+
const SHOW_ACTIONS = {{.ActionsEnabled}};
2+
const SHOW_TIME = {{.TimeEnabled}};
3+
const SHOW_ID = {{.IdEnabled}};
4+
const SHOW_APP_NAME = {{.AppNameEnabled}};
55

66
let host = window.document.location.host.replace(/:.*/, '');
77
let pathname = window.location.pathname

0 commit comments

Comments
 (0)