Compare commits

...

79 Commits

Author SHA1 Message Date
Amir Raminfar 3eec6cdd14 1.3.2 2018-11-26 16:10:13 -08:00
Amir Raminfar 3b9adf8260 Makes aside scrollable 2018-11-26 16:09:59 -08:00
Amir Raminfar dde707a97a Removes tips 2018-11-26 16:06:17 -08:00
Amir Raminfar 50ccf6311b 1.3.1 2018-11-26 14:53:38 -08:00
Amir Raminfar 705a339e49 Adds sorting and cleans up ids and names 2018-11-26 14:52:20 -08:00
Amir Raminfar f81c240a47 1.3.0 2018-11-26 11:15:53 -08:00
Amir Raminfar 262095e5bb Update README.md 2018-11-26 11:15:32 -08:00
Amir Raminfar ba900c4374 Uses EventSource instead of websockets (#8)
* Replaces websockets with event-stream

* Adds base_path

* Removes SSL

* Adds event listener for events

* Adds more logging

* Adds event listener to home page
2018-11-26 11:05:16 -08:00
Amir Raminfar 9dc6b3790d 1.2.8 2018-11-25 16:15:59 -08:00
Amir Raminfar b96785f2be Adds title for pages 2018-11-25 16:14:55 -08:00
Amir Raminfar dd6f4b1e31 1.2.7 2018-11-25 12:24:15 -08:00
Amir Raminfar 651291ecad Removes hostname from title 2018-11-25 12:23:29 -08:00
Amir Raminfar a5bcec68cb 1.2.6 2018-11-25 12:14:12 -08:00
Amir Raminfar 77d6a22122 Disables source maps 2018-11-25 12:14:00 -08:00
Amir Raminfar 40f97073e8 1.2.5 2018-11-25 12:10:55 -08:00
Amir Raminfar 75339ffba1 Docker hostname added 2018-11-25 12:10:46 -08:00
Amir Raminfar 2fdfba5a42 Updates packages in node 2018-11-25 12:06:49 -08:00
Amir Raminfar 5987330cdc 1.2.4 2018-11-24 17:48:24 -08:00
Amir Raminfar 14c7c21f9f Fixes bug on ipad 2018-11-24 17:48:18 -08:00
Amir Raminfar fc9fdaf8b6 1.2.3 2018-11-24 17:34:20 -08:00
Amir Raminfar 5979a6d0e5 Adds responsiveness for menu on left 2018-11-24 17:32:22 -08:00
Amir Raminfar ca2c46ffce 1.2.2 2018-11-20 09:20:42 -08:00
Amir Raminfar 1cc7e92466 Update README.md 2018-11-20 09:20:31 -08:00
Amir Raminfar cfc3e81820 Fixes typo #6 2018-11-20 07:45:24 -08:00
Amir Raminfar 67ab2ab170 1.2.1 2018-11-20 07:13:15 -08:00
Amir Raminfar e1ce378421 Adds SSL support for wss:// (#7)
* 1.2.0

* Adds SSL support
2018-11-20 07:12:13 -08:00
Amir Raminfar f083ea028d Fixes error in readme 2018-11-19 17:25:59 -08:00
Amir Raminfar 063a82198c Updates demo 2018-11-19 17:22:17 -08:00
Amir Raminfar d03c3440de 1.2.0 2018-11-19 13:04:25 -08:00
Amir Raminfar f7b28ad1e0 Adds base option (#5)
* Adds base option

* Removes base unused

* Adds readme

* Adds redirect
2018-11-19 13:04:06 -08:00
Amir Raminfar 52a95757ce Adds clean and release in npm 2018-11-19 07:52:34 -08:00
Amir Raminfar efc725dadc 1.1.2 2018-11-19 07:50:07 -08:00
Amir Raminfar ff8c539829 Updates readme 2018-11-19 07:49:57 -08:00
Amir Raminfar 875e17717e Deletes static files first 2018-11-19 07:48:55 -08:00
Amir Raminfar 929f8c19f8 1.1.1 2018-11-19 07:36:12 -08:00
Amir Raminfar bdcc856071 1.1.0 2018-11-18 19:25:40 -08:00
Amir Raminfar 2325881bd8 Ui redesign (#3)
* Adds a different layout

* Adds new layout and uses sass to get bulma

* Adds new layout and uses sass to get bulma

* Adds title

* Adds ellipses

* Adds tooltip

* Updates packages

* Fixes / page
2018-11-18 19:25:18 -08:00
Amir Raminfar 067fea2b7a Adds prettier 2018-11-18 08:43:32 -08:00
Amir Raminfar 8ac689ca57 1.0.16 2018-11-17 17:42:11 -08:00
Amir Raminfar 5e9ffe7fcf Fixes bug in safari 2018-11-17 17:42:07 -08:00
Amir Raminfar 3b3ba92d27 1.0.15 2018-11-17 16:33:15 -08:00
Amir Raminfar df2834fd81 Adds new font. Adds scrollboar notification 2018-11-17 16:32:55 -08:00
Amir Raminfar 2ecfefb35f fixes spacing 2018-11-17 11:12:48 -08:00
Amir Raminfar d18d3f800b Edtiroconfig 2018-11-17 11:02:35 -08:00
Amir Raminfar 9d7fd4eaf0 1.0.14 2018-11-14 07:41:53 -08:00
Amir Raminfar 032ebfd307 Makes background dark 2018-11-14 07:40:24 -08:00
Amir Raminfar 7f74a0f551 1.0.13 2018-11-13 16:46:18 -08:00
Amir Raminfar dc42180339 Fixes scroll 2018-11-13 16:45:12 -08:00
Amir Raminfar 972cbb8b2e Adds more vuejs 2018-11-13 16:38:49 -08:00
Amir Raminfar 5ee895357d Adds close 2018-11-13 16:38:14 -08:00
Amir Raminfar d7cfe64273 Updates babel 2018-11-13 16:38:14 -08:00
Amir Raminfar f06354f909 1.0.12 2018-11-13 16:38:14 -08:00
Amir Raminfar 0416fd541c Update README.md 2018-11-13 15:30:58 -08:00
Amir Raminfar 98701b1c7c 1.0.11 2018-11-01 12:00:11 -07:00
Amir Raminfar 17e08c02bb Adds version 2018-11-01 12:00:03 -07:00
Amir Raminfar 22108a2782 1.0.10 2018-11-01 10:12:22 -07:00
Amir Raminfar 742056bbef Adds latest npm 2018-11-01 10:12:13 -07:00
Amir Raminfar 2556dd07b3 1.0.9 2018-11-01 10:05:54 -07:00
Amir Raminfar e43879b69c Adds packr 2018-11-01 10:05:43 -07:00
Amir Raminfar 239bd874b2 1.0.8 2018-11-01 10:02:03 -07:00
Amir Raminfar c9b8b3f95a Adds npm i 2018-11-01 10:01:59 -07:00
Amir Raminfar 3454e907d3 Adds node 2018-11-01 10:00:03 -07:00
Amir Raminfar d33376e03b 1.0.7 2018-11-01 09:50:00 -07:00
Amir Raminfar f045f8bc95 Updates babel 2018-11-01 09:49:42 -07:00
Amir Raminfar 3e5f174f6e browserslist back 2018-11-01 09:09:06 -07:00
Amir Raminfar 34a21463b5 Change go to 1.11 2018-11-01 08:31:58 -07:00
Amir Raminfar 02fc893d4b Adds travis 2018-11-01 08:26:26 -07:00
Amir Raminfar 7c342e17a1 Moves to devDep 2018-10-31 15:31:11 -07:00
Amir Raminfar 8e23f61220 Adds husky 2018-10-31 15:20:29 -07:00
Amir Raminfar 4a65020cbf Moves to 6 colmns 2018-10-31 14:50:26 -07:00
Amir Raminfar f59aa6bfaa 1.0.6 2018-10-31 11:16:52 -07:00
Amir Raminfar ef78f94cb5 Fixes some spacing issues 2018-10-31 11:16:23 -07:00
Amir Raminfar 1ed4dde60e Adds readme 2018-10-31 08:58:57 -07:00
Amir Raminfar 959966d3eb 1.0.5 2018-10-31 08:00:52 -07:00
Amir Raminfar 07ffc18770 Adds new javascript and makes it prettier 2018-10-31 08:00:38 -07:00
Amir Raminfar 9773d39655 1.0.4 2018-10-30 18:06:44 -07:00
Amir Raminfar b41f755c08 Removes rpm and dep 2018-10-30 18:06:33 -07:00
Amir Raminfar 226c5bce54 1.0.3 2018-10-30 18:04:10 -07:00
Amir Raminfar 13dd12d737 Adds different colors 2018-10-30 18:04:00 -07:00
19 changed files with 1921 additions and 1055 deletions
+9 -3
View File
@@ -1,10 +1,16 @@
{
"presets": ["env"],
"presets": [
[
"@babel/preset-env",
{
"modules": false
}
]
],
"plugins": [
[
"transform-runtime",
"@babel/plugin-transform-runtime",
{
"polyfill": false,
"regenerator": true
}
]
+19
View File
@@ -0,0 +1,19 @@
root = true
[*]
charset = utf-8
end_of_line = lf
indent_style = space
indent_size = 2
insert_final_newline = true
trim_trailing_whitespace = true
max_line_length = 120
[*.go]
indent_size = 4
[Makefile]
indent_style = tab
[package.json]
indent_size = 1
+1 -19
View File
@@ -1,5 +1,6 @@
before:
hooks:
- npm run clean
- npm run build
- packr
builds:
@@ -30,25 +31,6 @@ changelog:
exclude:
- "^docs:"
- "^test:"
nfpm:
name_template: "{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}"
replacements:
amd64: 64-bit
386: 32-bit
arm64: ARM_64-bit
arm: ARM_32-bit
linux: Linux
darwin: macOS
vendor: Amir Raminfar
homepage: https://github.com/amir20/dozzle/
maintainer: Amir Raminfar <findamir@gmail.com>
license: MIT
formats:
- deb
- rpm
dockers:
- image_templates:
- "amir20/dozzle:{{ .Tag }}"
+3
View File
@@ -0,0 +1,3 @@
{
"printWidth": 120
}
+26
View File
@@ -0,0 +1,26 @@
language: go
go:
- "1.11"
services:
- docker
before_install:
- nvm install --lts
- npm i -g npm
- npm ci
- go get -u github.com/gobuffalo/packr/packr
after_success:
# docker login is required if you want to push docker images.
# DOCKER_PASSWORD should be a secret in your .travis.yml configuration.
# - test -n "$TRAVIS_TAG" && docker login -u=myuser -p="$DOCKER_PASSWORD"
deploy:
- provider: script
skip_cleanup: true
script: curl -sL https://git.io/goreleaser | bash
on:
tags: true
condition: $TRAVIS_OS_NAME = linux
+57
View File
@@ -0,0 +1,57 @@
# dozzle
Dozzle is a log viewer for Docker. It's free. It's small. And it's right in your browser. Oh, did I mention it is also real-time?
While dozzle should work for most, it is not meant to be a full logging solution. For enterprise use, I recommend you look at [Loggly](https://www.loggly.com), [Papertrail](https://papertrailapp.com) or [Kibana](https://www.elastic.co/products/kibana).
But if you don't want to pay for those services, then you are in luck! Dozzle will be able to capture all logs from your containers and send them in real-time to your browser. Installation is also very easy.
![Image](demo.gif)
## Getting dozzle
Dozzle is a very small Docker container (4 MB compressed). Pull the latest release from the index:
$ docker pull amir20/dozzle:latest
## Using dozzle
The simplest way to use dozzle is to run the docker container. Also, mount the Docker Unix socket with `-volume` to `/var/run/docker.sock`:
$ docker run --name dozzle -d --volume=/var/run/docker.sock:/var/run/docker.sock -p 8888:8080 amir20/dozzle:latest
dozzle will be available at [http://localhost:8888/](http://localhost:8888/). You can change `-p 8888:8080` to any port. For example, if you want to view dozzle over port 4040 then you would do `-p 4040:8080`.
#### Security
dozzle doesn't support authentication out of the box. You can control the device dozzle binds to by passing `--addr` parameter. For example,
$ docker run --volume=/var/run/docker.sock:/var/run/docker.sock -p 8888:1224 amir20/dozzle:latest --addr localhost:1224
will bind to `localhost` on port `1224`. You can then use a reverse proxy to control who can see dozzle.
#### Changing base URL
dozzle by default mounts to "/". If you want to control the base path you can use the `--base` option. For example, if you want to mount at "/foobar",
then you can override by using `--base /foobar`.
$ docker run --volume=/var/run/docker.sock:/var/run/docker.sock -p 8080:8080 amir20/dozzle:latest --base /foobar
dozzle will be available at [http://localhost:8080/foobar/](http://localhost:8080/foobar/).
#### Environment variable, DOCKER_API_VERSION
If you see
2018/10/31 08:53:17 Error response from daemon: client version 1.40 is too new. Maximum supported API version is 1.38
Then you need to modify `DOCKER_API_VERSION` to let dozzle know which version of the API is supported. By default, `DOCKER_API_VERSION=1.38` and you can change it by passing `-e` flag. For example, this would change the `DOCKER_API_VERSION` to `1.20`
$ docker run --volume=/var/run/docker.sock:/var/run/docker.sock -e DOCKER_API_VERSION=1.20 -p 8888:8080 amir20/dozzle:latest
If you are not sure what to set `DOCKER_API_VERSION` then run `docker version` which will show supported API version.
## License
[MIT](LICENSE)
+98 -6
View File
@@ -1,15 +1,107 @@
<template lang="html">
<router-view></router-view>
<div class="columns is-marginless">
<aside class="column menu is-2-desktop is-3-tablet">
<a
role="button"
class="navbar-burger burger is-white is-hidden-tablet is-pulled-right"
@click="showNav = !showNav;"
:class="{ 'is-active': showNav }"
>
<span></span> <span></span> <span></span>
</a>
<h1 class="title has-text-warning is-marginless">Dozzle</h1>
<p class="menu-label is-hidden-mobile" :class="{ 'is-active': showNav }">Containers</p>
<ul class="menu-list is-hidden-mobile" :class="{ 'is-active': showNav }">
<li v-for="item in containers">
<router-link :to="{ name: 'container', params: { id: item.id, name: item.name } }" active-class="is-active">
<div class="hide-overflow">{{ item.name }}</div>
</router-link>
</li>
</ul>
</aside>
<div class="column is-offset-2-desktop is-offset-3-tablet"><router-view></router-view></div>
<vue-headful :title="title" />
</div>
</template>
<script>
let es;
export default {
name: "App"
name: "App",
data() {
return {
title: "Dozzle",
containers: [],
showNav: false
};
},
async created() {
await this.fetchContainerList();
this.title = `${this.containers.length} containers - Dozzle`;
es = new EventSource(`${BASE_PATH}/api/events/stream`);
es.addEventListener("containers-changed", e => setTimeout(this.fetchContainerList, 1000), false);
},
beforeDestroy() {
if (es) {
es.close();
es = null;
}
},
methods: {
async fetchContainerList() {
this.containers = await (await fetch(`${BASE_PATH}/api/containers.json`)).json();
}
}
};
</script>
<style lang="css">
.section.is-fullwidth {
padding: 0 !important;
<style scoped lang="scss">
.is-hidden-mobile.is-active {
display: block !important;
}
</style>
.navbar-burger {
height: 2.35rem;
}
aside {
position: fixed;
z-index: 2;
padding: 1em;
@media screen and (min-width: 769px) {
& {
height: 100vh;
overflow: auto;
}
}
@media screen and (max-width: 768px) {
& {
position: sticky;
top: 0;
left: 0;
right: 0;
background: #222;
}
.tooltip::after,
.tooltip::before {
display: none !important;
}
.menu-label {
margin-top: 1em;
}
}
}
.hide-overflow {
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
}
.burger.is-white {
color: #fff;
}
</style>
@@ -0,0 +1,71 @@
<template lang="html">
<transition name="fade">
<button
class="button scroll-notification"
:class="hasNew ? 'is-warning' : 'is-primary'"
@click="scrollToBottom"
v-show="visible"
>
<span class="icon large"> <i class="fas fa-chevron-down"></i> </span>
</button>
</transition>
</template>
<script>
export default {
props: ["messages"],
data() {
return {
visible: false,
hasNew: false
};
},
mounted() {
document.addEventListener("scroll", this.onScroll, { passive: true });
setTimeout(() => this.scrollToBottom(), 500);
},
beforeDestroy() {
document.removeEventListener("scroll", this.onScroll);
},
methods: {
scrollToBottom() {
this.visible = false;
window.scrollTo(0, document.documentElement.scrollHeight || document.body.scrollHeight);
},
onScroll() {
const scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
const scrollBottom =
(document.documentElement.scrollHeight || document.body.scrollHeight) - document.documentElement.clientHeight;
const diff = Math.abs(scrollTop - scrollBottom);
this.visible = diff > 50;
if (!this.visible) {
this.hasNew = false;
}
}
},
watch: {
messages(newValue, oldValue) {
if (this.visible) {
this.hasNew = true;
} else {
this.scrollToBottom();
}
}
}
};
</script>
<style scoped>
.scroll-notification {
position: fixed;
right: 40px;
bottom: 30px;
}
.fade-enter-active,
.fade-leave-active {
transition: opacity 0.15s ease-in;
}
.fade-enter,
.fade-leave-to {
opacity: 0;
}
</style>
+12 -9
View File
@@ -1,16 +1,19 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Dozzle!</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.2/css/bulma.min.css">
<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>
<body>
<section class="section is-fullwidth">
<body class="is-dark">
<div id="app"></div>
</section>
<script src="/main.js"></script>
<script src="main.js"></script>
</body>
</html>
</html>
+11 -4
View File
@@ -1,13 +1,19 @@
import Vue from "vue";
import VueRouter from "vue-router";
Vue.use(VueRouter);
import vueHeadful from "vue-headful";
import App from "./App.vue";
import Index from "./pages/Index.vue";
import Container from "./pages/Container.vue";
import Index from "./pages/Index.vue";
Vue.use(VueRouter);
Vue.component("vue-headful", vueHeadful);
const routes = [
{ path: "/", component: Index },
{
path: "/",
component: Index,
name: "default"
},
{
path: "/container/:id",
component: Container,
@@ -18,6 +24,7 @@ const routes = [
const router = new VueRouter({
mode: "history",
base: BASE_PATH + "/",
routes
});
+83 -17
View File
@@ -1,28 +1,94 @@
<template lang="html">
<pre ref="logs">
</pre>
<div class="is-fullheight">
<ul ref="events" class="events">
<li v-for="item in messages" class="event" :key="item.key">
<span class="date">{{ item.dateRelative }}</span> <span class="text">{{ item.message }}</span>
</li>
</ul>
<scrollbar-notification :messages="messages"></scrollbar-notification>
<vue-headful :title="title" />
</div>
</template>
<script>
let ws;
import { formatRelative } from "date-fns";
import ScrollbarNotification from "../components/ScrollbarNotification";
let es = null;
let nextId = 0;
const parseMessage = data => {
const date = new Date(data.substring(0, 30));
const dateRelative = formatRelative(date, new Date());
const message = data.substring(30);
const key = nextId++;
return {
key,
date,
dateRelative,
message
};
};
export default {
props: ["id"],
props: ["id", "name"],
name: "Container",
mounted() {
ws = new WebSocket(`ws://${window.location.host}/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);
ws.onmessage = e => {
const parent = this.$refs.logs;
const item = document.createTextNode(e.data);
parent.appendChild(item);
parent.scrollIntoView({block: "end"});
components: {
ScrollbarNotification
},
data() {
return {
messages: [],
title: ""
};
},
created() {
this.loadLogs(this.id);
},
beforeDestroy() {
if (es) {
es.close();
es = null;
}
},
watch: {
id(newValue, oldValue) {
if (oldValue !== newValue) {
this.loadLogs(newValue);
}
}
},
methods: {
loadLogs(id) {
if (es) {
es.close();
es = null;
this.messages = [];
}
es = new EventSource(`${BASE_PATH}/api/logs/stream?id=${id}`);
es.onmessage = e => this.messages.push(parseMessage(e.data));
this.title = `${this.name} - Dozzle`;
}
}
};
</script>
<style>
<style scoped>
.events {
padding: 10px;
font-family: "Roboto Mono", monaco, monospace;
}
</style>
.event {
font-size: 13px;
line-height: 16px;
word-wrap: break-word;
}
.date {
background-color: #262626;
color: #258ccd;
}
.is-fullheight {
min-height: 100vh;
}
</style>
+13 -29
View File
@@ -1,38 +1,22 @@
<template lang="html">
<div class="container">
<div class="content">
<section class="section">
<ul class="is-marginless is-paddless">
<li v-for="item in containers" class="columns unstyled box">
<div class="column is-4">
<router-link :to="{name: 'container', params: {id: item.Id}}">{{ item.Names[0] }}</router-link>
</div>
<div class="column is-4">
<code>{{ item.Image }}</code>
</div>
<div class="column is-4">
<span class="subtitle is-7">{{ item.Status}}</span>
</div>
</li>
</ul>
</section>
<div class="hero is-fullheight is-dark">
<div class="hero-body">
<div class="container has-text-centered">
<h1 class="title">Please choose a container from the list to view the logs</h1>
</div>
</div>
</div>
</template>
<script>
export default {
name: "Index",
data() {
return {
containers: []
};
},
async created() {
this.containers = await (await fetch(`/api/containers.json`)).json();
}
props: [],
name: "Default"
};
</script>
<style lang="css">
</style>
<style scoped>
.hero.is-dark {
color: #ddd;
background-color: #111;
}
</style>
+19
View File
@@ -0,0 +1,19 @@
@charset "utf-8";
$menu-item-active-background-color: hsl(171, 100%, 41%);
$menu-item-color: hsl(0, 6%, 87%);
@import "../node_modules/bulma/bulma.sass";
.is-dark {
color: #ddd;
background-color: #111;
}
body {
font-family: "Roboto", sans-serif;
}
h1.title {
font-family: "Gafata", sans-serif;
}
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 24 MiB

+69
View File
@@ -0,0 +1,69 @@
package docker
import (
"context"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/events"
"github.com/docker/docker/client"
"io"
"log"
"sort"
"strings"
)
type dockerClient struct {
cli *client.Client
}
type DockerClient interface {
ListContainers() ([]Container, error)
ContainerLogs(ctx context.Context, id string) (io.ReadCloser, error)
Events(ctx context.Context) (<-chan events.Message, <-chan error)
}
func NewDockerClient() DockerClient {
cli, err := client.NewClientWithOpts(client.FromEnv)
if err != nil {
log.Fatal(err)
}
return &dockerClient{cli}
}
func (d *dockerClient) ListContainers() ([]Container, error) {
list, err := d.cli.ContainerList(context.Background(), types.ContainerListOptions{})
if err != nil {
return nil, err
}
var containers []Container
for _, c := range list {
container := Container{
ID: c.ID[:12],
Names: c.Names,
Name: strings.TrimPrefix(c.Names[0], "/"),
Image: c.Image,
ImageID: c.ImageID,
Command: c.Command,
Created: c.Created,
State: c.State,
Status: c.Status,
}
containers = append(containers, container)
}
sort.Slice(containers, func(i, j int) bool {
return containers[i].Name < containers[j].Name
})
return containers, nil
}
func (d *dockerClient) ContainerLogs(ctx context.Context, id string) (io.ReadCloser, error) {
options := types.ContainerLogsOptions{ShowStdout: true, ShowStderr: true, Follow: true, Tail: "300", Timestamps: true}
return d.cli.ContainerLogs(ctx, id, options)
}
func (d *dockerClient) Events(ctx context.Context) (<-chan events.Message, <-chan error) {
return d.cli.Events(ctx, types.EventsOptions{})
}
+13
View File
@@ -0,0 +1,13 @@
package docker
type Container struct {
ID string `json:"id"`
Names []string `json:"names"`
Name string `json:"name"`
Image string `json:"image"`
ImageID string `json:"imageId"`
Command string `json:"command"`
Created int64 `json:"created"`
State string `json:"state"`
Status string `json:"status"`
}
+169 -74
View File
@@ -1,95 +1,190 @@
package main
import (
"context"
"encoding/binary"
"encoding/json"
"flag"
"log"
"net/http"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/gobuffalo/packr"
"github.com/gorilla/websocket"
"context"
"encoding/binary"
"encoding/json"
"fmt"
"github.com/amir20/dozzle/docker"
"github.com/gobuffalo/packr"
"github.com/gorilla/mux"
flag "github.com/spf13/pflag"
"html/template"
"log"
"net/http"
"strings"
)
var (
cli *client.Client
addr = flag.String("addr", ":8080", "http service address")
upgrader = websocket.Upgrader{}
version = "dev"
commit = "none"
date = "unknown"
dockerClient docker.DockerClient
addr = ""
base = "/"
version = "dev"
commit = "none"
date = "unknown"
)
func init() {
var err error
cli, err = client.NewClientWithOpts(client.FromEnv)
if err != nil {
log.Fatal(err)
}
flag.Parse()
flag.StringVar(&addr, "addr", ":8080", "http service address")
flag.StringVar(&base, "base", "/", "base address of the application to mount")
dockerClient = docker.NewDockerClient()
flag.Parse()
}
func main() {
box := packr.NewBox("./static")
http.HandleFunc("/api/containers.json", listContainers)
http.HandleFunc("/api/logs", logs)
http.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
fileServer := http.FileServer(box)
if box.Has(req.URL.Path) {
fileServer.ServeHTTP(w, req)
} else {
bytes, _ := box.Find("index.html")
w.Write(bytes)
}
}))
r := mux.NewRouter()
log.Fatal(http.ListenAndServe(*addr, nil))
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()
box := packr.NewBox("./static")
s.HandleFunc("/api/containers.json", listContainers)
s.HandleFunc("/api/logs/stream", streamLogs)
s.HandleFunc("/api/events/stream", streamEvents)
s.HandleFunc("/version", versionHandler)
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)
} else {
handleIndex(box, w)
}
})))
log.Printf("Accepting connections on %s", addr)
log.Fatal(http.ListenAndServe(addr, r))
}
func versionHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, version)
fmt.Fprintln(w, commit)
fmt.Fprintln(w, date)
}
func listContainers(w http.ResponseWriter, r *http.Request) {
containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{})
if err != nil {
log.Fatal(err)
}
json.NewEncoder(w).Encode(containers)
containers, err := dockerClient.ListContainers()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = json.NewEncoder(w).Encode(containers)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func logs(w http.ResponseWriter, r *http.Request) {
id := r.URL.Query().Get("id")
c, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Fatal(err)
return
}
defer c.Close()
func handleIndex(box packr.Box, w http.ResponseWriter) {
text, _ := box.FindString("index.html")
text = strings.Replace(text, "__BASE__", "{{ .Base }}", -1)
tmpl, err := template.New("index.html").Parse(text)
if err != nil {
panic(err)
}
options := types.ContainerLogsOptions{ShowStdout: true, ShowStderr: true, Follow: true, Tail: "40"}
reader, err := cli.ContainerLogs(context.Background(), id, options)
defer reader.Close()
if err != nil {
log.Fatal(err)
}
path := ""
if base != "/" {
path = base
}
hdr := make([]byte, 8)
content := make([]byte, 1024, 1024*1024)
for {
_, err := reader.Read(hdr)
if err != nil {
panic(err)
}
count := binary.BigEndian.Uint32(hdr[4:])
n, err := reader.Read(content[:count])
if err != nil {
log.Println(err)
break
}
err = c.WriteMessage(websocket.TextMessage, content[:n])
if err != nil {
log.Println(err)
break
}
}
data := struct{ Base string }{path}
err = tmpl.Execute(w, data)
if err != nil {
panic(err)
}
}
func streamLogs(w http.ResponseWriter, r *http.Request) {
id := r.URL.Query().Get("id")
if id == "" {
http.Error(w, "id is required", http.StatusBadRequest)
return
}
f, ok := w.(http.Flusher)
if !ok {
http.Error(w, "Streaming unsupported!", http.StatusInternalServerError)
return
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
reader, err := dockerClient.ContainerLogs(ctx, id)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer reader.Close()
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
w.Header().Set("Transfer-Encoding", "chunked")
hdr := make([]byte, 8)
content := make([]byte, 1024, 1024*1024)
for {
_, err := reader.Read(hdr)
if err != nil {
log.Panicln(err)
}
count := binary.BigEndian.Uint32(hdr[4:])
n, err := reader.Read(content[:count])
if err != nil {
log.Println(err)
break
}
_, err = fmt.Fprintf(w, "data: %s\n\n", content[:n])
if err != nil {
log.Println(err)
break
}
f.Flush()
}
}
func streamEvents(w http.ResponseWriter, r *http.Request) {
f, ok := w.(http.Flusher)
if !ok {
http.Error(w, "Streaming unsupported!", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
w.Header().Set("Transfer-Encoding", "chunked")
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
messages, _ := dockerClient.Events(ctx)
for message := range messages {
switch message.Action {
case "connect":
fallthrough
case "disconnect":
fallthrough
case "create":
fallthrough
case "destroy":
_, err := fmt.Fprintf(w, "event: containers-changed\n")
_, err = fmt.Fprintf(w, "data: %s\n\n", message.Action)
if err != nil {
log.Println(err)
break
}
f.Flush()
default:
// Do nothing
}
}
}
+1192 -857
View File
File diff suppressed because it is too large Load Diff
+56 -37
View File
@@ -1,40 +1,59 @@
{
"name": "dozzle",
"version": "1.0.2",
"description": "",
"main": "index.js",
"scripts": {
"start": "concurrently 'go run main.go' 'npm run watch-assets'",
"watch-assets": "parcel watch assets/index.html -d static",
"build": "parcel build assets/index.html -d static",
"clean": "rm -rf static"
},
"repository": {
"type": "git",
"url": "git+https://github.com/amir20/dozzle.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/amir20/dozzle/issues"
},
"homepage": "https://github.com/amir20/dozzle#readme",
"dependencies": {
"vue": "^2.5.17",
"vue-router": "^3.0.1"
},
"devDependencies": {
"@vue/component-compiler-utils": "^2.3.0",
"babel-core": "^6.26.3",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-env": "^1.7.0",
"babel-runtime": "^6.26.0",
"parcel-bundler": "^1.10.3",
"concurrently": "^4.0.1",
"vue-hot-reload-api": "^2.3.1",
"vue-template-compiler": "^2.5.17"
},
"browserslist": [
">5%"
"name": "dozzle",
"version": "1.3.2",
"description": "",
"main": "index.js",
"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 --no-source-maps --public-url '__BASE__' assets/index.html -d static",
"clean": "rm -rf static",
"release": "goreleaser --rm-dist"
},
"repository": {
"type": "git",
"url": "git+https://github.com/amir20/dozzle.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/amir20/dozzle/issues"
},
"homepage": "https://github.com/amir20/dozzle#readme",
"dependencies": {
"bulma": "^0.7.2",
"date-fns": "^2.0.0-alpha.25",
"vue": "^2.5.17",
"vue-headful": "^2.0.1",
"vue-router": "^3.0.2"
},
"devDependencies": {
"@babel/core": "^7.1.6",
"@babel/plugin-transform-runtime": "^7.1.0",
"@vue/component-compiler-utils": "^2.3.0",
"concurrently": "^4.1.0",
"husky": "^1.2.0",
"lint-staged": "^8.1.0",
"parcel-bundler": "^1.10.3",
"prettier": "^1.15.2",
"sass": "^1.15.1",
"vue-hot-reload-api": "^2.3.1",
"vue-template-compiler": "^2.5.17"
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"*.{js,vue,css}": [
"prettier --write",
"git add"
]
},
"browserslist": [
">5%",
"not ie <= 8"
]
}