Skip to content

Commit e1ce378

Browse files
authored
Adds SSL support for wss:// (#7)
* 1.2.0 * Adds SSL support
1 parent f083ea0 commit e1ce378

File tree

4 files changed

+38
-27
lines changed

4 files changed

+38
-27
lines changed

assets/index.html

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
<!DOCTYPE html>
22
<html>
3-
<head>
4-
<meta charset="utf-8" />
5-
<meta name="viewport" content="width=device-width, initial-scale=1" />
6-
<title>Dozzle</title>
7-
<link href="https://fonts.googleapis.com/css?family=Roboto|Roboto+Mono|Gafata" rel="stylesheet">
8-
<link href="styles.scss" rel="stylesheet">
9-
<script>
10-
window["BASE_PATH"] = "{{ .Base }}";
11-
</script>
12-
<script defer src="https://use.fontawesome.com/releases/v5.3.1/js/all.js"></script>
13-
</head>
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1" />
6+
<title>Dozzle</title>
7+
<link href="https://fonts.googleapis.com/css?family=Roboto|Roboto+Mono|Gafata" rel="stylesheet" />
8+
<link href="styles.scss" rel="stylesheet" />
9+
<script>
10+
window["BASE_PATH"] = "{{ .Base }}";
11+
window["SSL_ENABLED"] = "{{ .SSL }}".toLowerCase() == "true" ? true : false;
12+
</script>
13+
<script defer src="https://use.fontawesome.com/releases/v5.3.1/js/all.js"></script>
14+
</head>
1415

15-
<body class="is-dark">
16-
<div id="app"></div>
17-
<script src="main.js"></script>
18-
</body>
16+
<body class="is-dark">
17+
<div id="app"></div>
18+
<script src="main.js"></script>
19+
</body>
1920
</html>

assets/pages/Container.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ export default {
6060
ws = null;
6161
this.messages = [];
6262
}
63-
ws = new WebSocket(`ws://${window.location.host}${BASE_PATH}/api/logs?id=${this.id}`);
63+
const protocol = SSL_ENABLED ? "wws" : "ws";
64+
ws = new WebSocket(`${protocol}://${window.location.host}${BASE_PATH}/api/logs?id=${this.id}`);
6465
ws.onopen = e => console.log("Connection opened.");
6566
ws.onclose = e => console.log("Connection closed.");
6667
ws.onerror = e => console.error("Connection error: " + e.data);

main.go

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,20 @@ import (
2020

2121
var (
2222
cli *client.Client
23-
addr = flag.String("addr", ":8080", "http service address")
24-
base = flag.String("base", "/", "base address of the application to mount")
23+
addr = ""
24+
ssl = false
25+
base = "/"
2526
upgrader = websocket.Upgrader{}
2627
version = "dev"
2728
commit = "none"
2829
date = "unknown"
2930
)
3031

3132
func init() {
33+
flag.StringVar(&addr, "addr", ":8080", "http service address")
34+
flag.StringVar(&base, "base", "/", "base address of the application to mount")
35+
flag.BoolVarP(&ssl, "ssl", "s", false, "Uses websockets over ssl if enabled")
36+
3237
var err error
3338
cli, err = client.NewClientWithOpts(client.FromEnv)
3439
if err != nil {
@@ -40,19 +45,19 @@ func init() {
4045
func main() {
4146
r := mux.NewRouter()
4247

43-
if *base != "/" {
44-
r.HandleFunc(*base, http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
45-
http.Redirect(w, req, *base+"/", http.StatusMovedPermanently)
48+
if base != "/" {
49+
r.HandleFunc(base, http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
50+
http.Redirect(w, req, base+"/", http.StatusMovedPermanently)
4651
}))
4752
}
4853

49-
s := r.PathPrefix(*base).Subrouter()
54+
s := r.PathPrefix(base).Subrouter()
5055
box := packr.NewBox("./static")
5156

5257
s.HandleFunc("/api/containers.json", listContainers)
5358
s.HandleFunc("/api/logs", logs)
5459
s.HandleFunc("/version", versionHandler)
55-
s.PathPrefix("/").Handler(http.StripPrefix(*base, http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
60+
s.PathPrefix("/").Handler(http.StripPrefix(base, http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
5661
fileServer := http.FileServer(box)
5762
if box.Has(req.URL.Path) && req.URL.Path != "" && req.URL.Path != "/" {
5863
fileServer.ServeHTTP(w, req)
@@ -61,7 +66,7 @@ func main() {
6166
}
6267
})))
6368

64-
log.Fatal(http.ListenAndServe(*addr, r))
69+
log.Fatal(http.ListenAndServe(addr, r))
6570
}
6671

6772
func versionHandler(w http.ResponseWriter, r *http.Request) {
@@ -87,10 +92,13 @@ func handleIndex(box packr.Box, w http.ResponseWriter) {
8792
}
8893

8994
path := ""
90-
if *base != "/" {
91-
path = *base
95+
if base != "/" {
96+
path = base
9297
}
93-
data := struct{ Base string }{Base: path}
98+
data := struct {
99+
Base string
100+
SSL bool
101+
}{path, ssl}
94102
err = tmpl.Execute(w, data)
95103
if err != nil {
96104
panic(err)

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"scripts": {
77
"start": "concurrently 'go run main.go' 'npm run watch-assets'",
88
"watch-assets": "parcel watch --public-url '__BASE__' assets/index.html -d static",
9+
"prebuild": "npm run clean",
910
"build": "parcel build --public-url '__BASE__' assets/index.html -d static",
1011
"clean": "rm -rf static",
1112
"release": "goreleaser --rm-dist"

0 commit comments

Comments
 (0)