@@ -4,20 +4,24 @@ import (
4
4
"context"
5
5
"encoding/binary"
6
6
"encoding/json"
7
- "flag"
8
7
"fmt"
8
+ "html/template"
9
9
"log"
10
10
"net/http"
11
+ "strings"
11
12
12
13
"github.com/docker/docker/api/types"
13
14
"github.com/docker/docker/client"
14
15
"github.com/gobuffalo/packr"
16
+ "github.com/gorilla/mux"
15
17
"github.com/gorilla/websocket"
18
+ flag "github.com/spf13/pflag"
16
19
)
17
20
18
21
var (
19
22
cli * client.Client
20
23
addr = flag .String ("addr" , ":8080" , "http service address" )
24
+ base = flag .String ("base" , "/" , "base address of the application to mount" )
21
25
upgrader = websocket.Upgrader {}
22
26
version = "dev"
23
27
commit = "none"
@@ -34,21 +38,30 @@ func init() {
34
38
}
35
39
36
40
func main () {
41
+ r := mux .NewRouter ()
42
+
43
+ if * base != "/" {
44
+ r .HandleFunc (* base , http .HandlerFunc (func (w http.ResponseWriter , req * http.Request ) {
45
+ http .Redirect (w , req , * base + "/" , http .StatusMovedPermanently )
46
+ }))
47
+ }
48
+
49
+ s := r .PathPrefix (* base ).Subrouter ()
37
50
box := packr .NewBox ("./static" )
38
- http .HandleFunc ("/api/containers.json" , listContainers )
39
- http .HandleFunc ("/api/logs" , logs )
40
- http .HandleFunc ("/version" , versionHandler )
41
- http .Handle ("/" , http .HandlerFunc (func (w http.ResponseWriter , req * http.Request ) {
51
+
52
+ s .HandleFunc ("/api/containers.json" , listContainers )
53
+ s .HandleFunc ("/api/logs" , logs )
54
+ s .HandleFunc ("/version" , versionHandler )
55
+ s .PathPrefix ("/" ).Handler (http .StripPrefix (* base , http .HandlerFunc (func (w http.ResponseWriter , req * http.Request ) {
42
56
fileServer := http .FileServer (box )
43
- if box .Has (req .URL .Path ) {
57
+ if box .Has (req .URL .Path ) && req . URL . Path != "" && req . URL . Path != "/" {
44
58
fileServer .ServeHTTP (w , req )
45
59
} else {
46
- bytes , _ := box .Find ("index.html" )
47
- w .Write (bytes )
60
+ handleIndex (box , w )
48
61
}
49
- }))
62
+ })))
50
63
51
- log .Fatal (http .ListenAndServe (* addr , nil ))
64
+ log .Fatal (http .ListenAndServe (* addr , r ))
52
65
}
53
66
54
67
func versionHandler (w http.ResponseWriter , r * http.Request ) {
@@ -65,6 +78,25 @@ func listContainers(w http.ResponseWriter, r *http.Request) {
65
78
json .NewEncoder (w ).Encode (containers )
66
79
}
67
80
81
+ func handleIndex (box packr.Box , w http.ResponseWriter ) {
82
+ text , _ := box .FindString ("index.html" )
83
+ text = strings .Replace (text , "__BASE__" , "{{ .Base }}" , - 1 )
84
+ tmpl , err := template .New ("index.html" ).Parse (text )
85
+ if err != nil {
86
+ panic (err )
87
+ }
88
+
89
+ path := ""
90
+ if * base != "/" {
91
+ path = * base
92
+ }
93
+ data := struct { Base string }{Base : path }
94
+ err = tmpl .Execute (w , data )
95
+ if err != nil {
96
+ panic (err )
97
+ }
98
+ }
99
+
68
100
func logs (w http.ResponseWriter , r * http.Request ) {
69
101
id := r .URL .Query ().Get ("id" )
70
102
c , err := upgrader .Upgrade (w , r , nil )
@@ -86,7 +118,7 @@ func logs(w http.ResponseWriter, r *http.Request) {
86
118
for {
87
119
_ , err := reader .Read (hdr )
88
120
if err != nil {
89
- log .Panicln (err )
121
+ log .Panicln (err )
90
122
}
91
123
count := binary .BigEndian .Uint32 (hdr [4 :])
92
124
n , err := reader .Read (content [:count ])
0 commit comments