First step

This commit is contained in:
nemunaire 2024-05-29 12:09:31 +02:00
parent d1600c38c8
commit 36f5dde838
4 changed files with 123 additions and 0 deletions

32
curl.go Normal file
View File

@ -0,0 +1,32 @@
package main
import (
"fmt"
"io"
"net"
"net/http"
)
func curl(socket, method, path string) (io.ReadCloser, error) {
socketDial := func(proto, addr string) (conn net.Conn, err error) {
return net.Dial("unix", socket)
}
tr := &http.Transport{
Dial: socketDial,
}
client := &http.Client{
Transport: tr,
}
req, err := http.NewRequest(method, path, nil)
if err != nil {
return nil, fmt.Errorf("Error creating request: %w\n", err)
}
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("Error performing request: %w\n", err)
}
return resp.Body, nil
}

38
index.html Normal file
View File

@ -0,0 +1,38 @@
<!DOCTYPE html>
<html>
<head>
<title>Meetup Go</title>
<link href="/style.css" type="text/css" rel="stylesheet">
</head>
<body>
<h1>Meetup Go&nbsp;!</h1>
<table>
<thead>
<tr>
<th>CONTAINER ID</th>
<th>IMAGE</th>
<th>COMMAND</th>
<th>CREATED</th>
<th>STATUS</th>
<th>PORTS</th>
<th>NAMES</th>
</tr>
</thead>
<tbody id="containers">
</tbody>
</table>
<script>
fetch('/containers').then(
(res) => res.json()
).then(
(containers) => {
document.getElementById('containers').innerHTML = '';
for (const container of containers) {
let div = document.createElement('tr');
div.innerHTML = `<td>${container.Id.substring(0, 8)}</td><td>${container.Image}</td><td>${container.Command}</td><td>${container.Created}</td><td>${container.Status}</td><td>${container.Ports.map((port) => port.PrivatePort + '/' + port.Type).join(" ")}</td><td>${container.Names.join(" ")}</td>`;
document.getElementById('containers').appendChild(div);
}
})
</script>
</body>
</html>

22
main.go Normal file
View File

@ -0,0 +1,22 @@
package main
import (
"io"
"log"
"net/http"
)
func main() {
http.HandleFunc("GET /containers", func(w http.ResponseWriter, r *http.Request) {
rd, err := curl("/var/run/docker.sock", "GET", "http://d/v1.43/containers/json?all=true")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
} else {
io.Copy(w, rd)
}
})
http.Handle("GET /", http.FileServer(http.Dir(".")))
log.Fatal(http.ListenAndServe(":8080", nil))
}

31
style.css Normal file
View File

@ -0,0 +1,31 @@
body {
display: flex;
flex-direction: column;
justify-content: center;
gap: .5rem;
}
h1 {
border-bottom: 5px double forestgreen;
flex-grow: 1;
text-align: center;
padding-bottom: .66rem;
}
table {
border-spacing:0;
border-collapse: collapse;
}
td, th {
padding: .5rem 0;
text-align: center;
}
tbody > tr {
border-top: 1px solid #222;
}
tbody > tr:nth-child(odd) {
background: #bbb;
}
tbody > tr:nth-child(even) {
background: #ddd;
}