1
0
mirror of https://github.com/amir20/dozzle.git synced 2025-12-24 22:39:18 +01:00

Adds SSL support for wss:// (#7)

* 1.2.0

* Adds SSL support
This commit is contained in:
Amir Raminfar
2018-11-20 07:12:13 -08:00
committed by GitHub
parent f083ea028d
commit e1ce378421
4 changed files with 38 additions and 27 deletions

View File

@@ -1,19 +1,20 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Dozzle</title>
<link href="https://fonts.googleapis.com/css?family=Roboto|Roboto+Mono|Gafata" rel="stylesheet">
<link href="styles.scss" rel="stylesheet">
<script>
window["BASE_PATH"] = "{{ .Base }}";
</script>
<script defer src="https://use.fontawesome.com/releases/v5.3.1/js/all.js"></script>
</head>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Dozzle</title>
<link href="https://fonts.googleapis.com/css?family=Roboto|Roboto+Mono|Gafata" rel="stylesheet" />
<link href="styles.scss" rel="stylesheet" />
<script>
window["BASE_PATH"] = "{{ .Base }}";
window["SSL_ENABLED"] = "{{ .SSL }}".toLowerCase() == "true" ? true : false;
</script>
<script defer src="https://use.fontawesome.com/releases/v5.3.1/js/all.js"></script>
</head>
<body class="is-dark">
<div id="app"></div>
<script src="main.js"></script>
</body>
<body class="is-dark">
<div id="app"></div>
<script src="main.js"></script>
</body>
</html>

View File

@@ -60,7 +60,8 @@ export default {
ws = null;
this.messages = [];
}
ws = new WebSocket(`ws://${window.location.host}${BASE_PATH}/api/logs?id=${this.id}`);
const protocol = SSL_ENABLED ? "wws" : "ws";
ws = new WebSocket(`${protocol}://${window.location.host}${BASE_PATH}/api/logs?id=${this.id}`);
ws.onopen = e => console.log("Connection opened.");
ws.onclose = e => console.log("Connection closed.");
ws.onerror = e => console.error("Connection error: " + e.data);

30
main.go
View File

@@ -20,8 +20,9 @@ import (
var (
cli *client.Client
addr = flag.String("addr", ":8080", "http service address")
base = flag.String("base", "/", "base address of the application to mount")
addr = ""
ssl = false
base = "/"
upgrader = websocket.Upgrader{}
version = "dev"
commit = "none"
@@ -29,6 +30,10 @@ var (
)
func init() {
flag.StringVar(&addr, "addr", ":8080", "http service address")
flag.StringVar(&base, "base", "/", "base address of the application to mount")
flag.BoolVarP(&ssl, "ssl", "s", false, "Uses websockets over ssl if enabled")
var err error
cli, err = client.NewClientWithOpts(client.FromEnv)
if err != nil {
@@ -40,19 +45,19 @@ func init() {
func main() {
r := mux.NewRouter()
if *base != "/" {
r.HandleFunc(*base, http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
http.Redirect(w, req, *base+"/", http.StatusMovedPermanently)
if base != "/" {
r.HandleFunc(base, http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
http.Redirect(w, req, base+"/", http.StatusMovedPermanently)
}))
}
s := r.PathPrefix(*base).Subrouter()
s := r.PathPrefix(base).Subrouter()
box := packr.NewBox("./static")
s.HandleFunc("/api/containers.json", listContainers)
s.HandleFunc("/api/logs", logs)
s.HandleFunc("/version", versionHandler)
s.PathPrefix("/").Handler(http.StripPrefix(*base, http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
s.PathPrefix("/").Handler(http.StripPrefix(base, http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
fileServer := http.FileServer(box)
if box.Has(req.URL.Path) && req.URL.Path != "" && req.URL.Path != "/" {
fileServer.ServeHTTP(w, req)
@@ -61,7 +66,7 @@ func main() {
}
})))
log.Fatal(http.ListenAndServe(*addr, r))
log.Fatal(http.ListenAndServe(addr, r))
}
func versionHandler(w http.ResponseWriter, r *http.Request) {
@@ -87,10 +92,13 @@ func handleIndex(box packr.Box, w http.ResponseWriter) {
}
path := ""
if *base != "/" {
path = *base
if base != "/" {
path = base
}
data := struct{ Base string }{Base: path}
data := struct {
Base string
SSL bool
}{path, ssl}
err = tmpl.Execute(w, data)
if err != nil {
panic(err)

View File

@@ -6,6 +6,7 @@
"scripts": {
"start": "concurrently 'go run main.go' 'npm run watch-assets'",
"watch-assets": "parcel watch --public-url '__BASE__' assets/index.html -d static",
"prebuild": "npm run clean",
"build": "parcel build --public-url '__BASE__' assets/index.html -d static",
"clean": "rm -rf static",
"release": "goreleaser --rm-dist"