Skip to content

Commit 54be038

Browse files
authored
Merge pull request #2 from CodeShellDev/dev
Dev
2 parents 4bea9eb + 5329f20 commit 54be038

6 files changed

Lines changed: 109 additions & 35 deletions

File tree

go.mod

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,31 @@ module github.com/codeshelldev/wol-dockerized
22

33
go 1.25.5
44

5-
require github.com/codeshelldev/gotl v0.0.4
5+
require (
6+
github.com/codeshelldev/gotl v0.0.5
7+
github.com/moby/moby/api v1.52.0
8+
)
69

710
require (
811
github.com/Microsoft/go-winio v0.6.2 // indirect
12+
github.com/cespare/xxhash/v2 v2.3.0 // indirect
913
github.com/containerd/errdefs v1.0.0 // indirect
1014
github.com/containerd/errdefs/pkg v0.3.0 // indirect
1115
github.com/distribution/reference v0.6.0 // indirect
1216
github.com/docker/go-connections v0.6.0 // indirect
1317
github.com/docker/go-units v0.5.0 // indirect
1418
github.com/felixge/httpsnoop v1.0.4 // indirect
15-
github.com/go-logr/logr v1.4.2 // indirect
19+
github.com/go-logr/logr v1.4.3 // indirect
1620
github.com/go-logr/stdr v1.2.2 // indirect
1721
github.com/moby/docker-image-spec v1.3.1 // indirect
18-
github.com/moby/moby/api v1.52.0 // indirect
1922
github.com/opencontainers/go-digest v1.0.0 // indirect
2023
github.com/opencontainers/image-spec v1.1.1 // indirect
21-
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
22-
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.60.0 // indirect
23-
go.opentelemetry.io/otel v1.35.0 // indirect
24-
go.opentelemetry.io/otel/metric v1.35.0 // indirect
25-
go.opentelemetry.io/otel/trace v1.35.0 // indirect
26-
golang.org/x/sys v0.38.0 // indirect
24+
go.opentelemetry.io/auto/sdk v1.2.1 // indirect
25+
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.64.0 // indirect
26+
go.opentelemetry.io/otel v1.39.0 // indirect
27+
go.opentelemetry.io/otel/metric v1.39.0 // indirect
28+
go.opentelemetry.io/otel/trace v1.39.0 // indirect
29+
golang.org/x/sys v0.39.0 // indirect
2730
)
2831

2932
require (

go.sum

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY=
22
github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU=
3+
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
4+
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
35
github.com/codeshelldev/gotl v0.0.4 h1:W2cup8Pw9LzFLxmS5QUzY+NSE3ZgiRSUM7FiGd6qJrI=
46
github.com/codeshelldev/gotl v0.0.4/go.mod h1:Mfb+Lb+DV3DUXdA1sixJb2pLawaJGGFFeC29gUZQLcg=
7+
github.com/codeshelldev/gotl v0.0.5 h1:Qs6Pb9iOuXqyRPHmf6yOn6mVD3tl1dr/qMul6gYS1Mg=
8+
github.com/codeshelldev/gotl v0.0.5/go.mod h1:Mfb+Lb+DV3DUXdA1sixJb2pLawaJGGFFeC29gUZQLcg=
59
github.com/containerd/errdefs v1.0.0 h1:tg5yIfIlQIrxYtu9ajqY42W3lpS19XqdxRQeEwYG8PI=
610
github.com/containerd/errdefs v1.0.0/go.mod h1:+YBYIdtsnF4Iw6nWZhJcqGSg/dwvV7tyJ/kCkyJ2k+M=
711
github.com/containerd/errdefs/pkg v0.3.0 h1:9IKJ06FvyNlexW690DXuQNx2KA2cUJXx151Xdx3ZPPE=
812
github.com/containerd/errdefs/pkg v0.3.0/go.mod h1:NJw6s9HwNuRhnjJhM7pylWwMyAkmCQvQ4GpJHEqRLVk=
13+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
14+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
915
github.com/distribution/reference v0.6.0 h1:0IXCQ5g4/QMHHkarYzh5l+u8T3t73zM5QvfrDyIgxBk=
1016
github.com/distribution/reference v0.6.0/go.mod h1:BbU0aIcezP1/5jX/8MP0YiH4SdvB5Y4f/wlDRiLyi3E=
1117
github.com/docker/go-connections v0.6.0 h1:LlMG9azAe1TqfR7sO+NJttz1gy6KO7VJBh+pMmjSD94=
@@ -17,8 +23,12 @@ github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSw
1723
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
1824
github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
1925
github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
26+
github.com/go-logr/logr v1.4.3 h1:CjnDlHq8ikf6E492q6eKboGOC0T8CDaOvkHCIg8idEI=
27+
github.com/go-logr/logr v1.4.3/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
2028
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
2129
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
30+
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
31+
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
2232
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
2333
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
2434
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
@@ -33,19 +43,49 @@ github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8
3343
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
3444
github.com/opencontainers/image-spec v1.1.1 h1:y0fUlFfIZhPF1W537XOLg0/fcx6zcHCJwooC2xJA040=
3545
github.com/opencontainers/image-spec v1.1.1/go.mod h1:qpqAh3Dmcf36wStyyWU+kCeDgrGnAve2nCC8+7h8Q0M=
46+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
47+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
48+
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
49+
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
3650
go.opentelemetry.io/auto/sdk v1.1.0 h1:cH53jehLUN6UFLY71z+NDOiNJqDdPRaXzTel0sJySYA=
3751
go.opentelemetry.io/auto/sdk v1.1.0/go.mod h1:3wSPjt5PWp2RhlCcmmOial7AvC4DQqZb7a7wCow3W8A=
52+
go.opentelemetry.io/auto/sdk v1.2.1 h1:jXsnJ4Lmnqd11kwkBV2LgLoFMZKizbCi5fNZ/ipaZ64=
53+
go.opentelemetry.io/auto/sdk v1.2.1/go.mod h1:KRTj+aOaElaLi+wW1kO/DZRXwkF4C5xPbEe3ZiIhN7Y=
3854
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.60.0 h1:sbiXRNDSWJOTobXh5HyQKjq6wUC5tNybqjIqDpAY4CU=
3955
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.60.0/go.mod h1:69uWxva0WgAA/4bu2Yy70SLDBwZXuQ6PbBpbsa5iZrQ=
56+
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.64.0 h1:ssfIgGNANqpVFCndZvcuyKbl0g+UAVcbBcqGkG28H0Y=
57+
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.64.0/go.mod h1:GQ/474YrbE4Jx8gZ4q5I4hrhUzM6UPzyrqJYV2AqPoQ=
4058
go.opentelemetry.io/otel v1.35.0 h1:xKWKPxrxB6OtMCbmMY021CqC45J+3Onta9MqjhnusiQ=
4159
go.opentelemetry.io/otel v1.35.0/go.mod h1:UEqy8Zp11hpkUrL73gSlELM0DupHoiq72dR+Zqel/+Y=
60+
go.opentelemetry.io/otel v1.39.0 h1:8yPrr/S0ND9QEfTfdP9V+SiwT4E0G7Y5MO7p85nis48=
61+
go.opentelemetry.io/otel v1.39.0/go.mod h1:kLlFTywNWrFyEdH0oj2xK0bFYZtHRYUdv1NklR/tgc8=
4262
go.opentelemetry.io/otel/metric v1.35.0 h1:0znxYu2SNyuMSQT4Y9WDWej0VpcsxkuklLa4/siN90M=
4363
go.opentelemetry.io/otel/metric v1.35.0/go.mod h1:nKVFgxBZ2fReX6IlyW28MgZojkoAkJGaE8CpgeAU3oE=
64+
go.opentelemetry.io/otel/metric v1.39.0 h1:d1UzonvEZriVfpNKEVmHXbdf909uGTOQjA0HF0Ls5Q0=
65+
go.opentelemetry.io/otel/metric v1.39.0/go.mod h1:jrZSWL33sD7bBxg1xjrqyDjnuzTUB0x1nBERXd7Ftcs=
66+
go.opentelemetry.io/otel/sdk v1.35.0 h1:iPctf8iprVySXSKJffSS79eOjl9pvxV9ZqOWT0QejKY=
67+
go.opentelemetry.io/otel/sdk v1.35.0/go.mod h1:+ga1bZliga3DxJ3CQGg3updiaAJoNECOgJREo9KHGQg=
68+
go.opentelemetry.io/otel/sdk v1.39.0 h1:nMLYcjVsvdui1B/4FRkwjzoRVsMK8uL/cj0OyhKzt18=
69+
go.opentelemetry.io/otel/sdk/metric v1.35.0 h1:1RriWBmCKgkeHEhM7a2uMjMUfP7MsOF5JpUCaEqEI9o=
70+
go.opentelemetry.io/otel/sdk/metric v1.35.0/go.mod h1:is6XYCUMpcKi+ZsOvfluY5YstFnhW0BidkR+gL+qN+w=
71+
go.opentelemetry.io/otel/sdk/metric v1.39.0 h1:cXMVVFVgsIf2YL6QkRF4Urbr/aMInf+2WKg+sEJTtB8=
4472
go.opentelemetry.io/otel/trace v1.35.0 h1:dPpEfJu1sDIqruz7BHFG3c7528f6ddfSWfFDVt/xgMs=
4573
go.opentelemetry.io/otel/trace v1.35.0/go.mod h1:WUk7DtFp1Aw2MkvqGdwiXYDZZNvA/1J8o6xRXLrIkyc=
74+
go.opentelemetry.io/otel/trace v1.39.0 h1:2d2vfpEDmCJ5zVYz7ijaJdOF59xLomrvj7bjt6/qCJI=
75+
go.opentelemetry.io/otel/trace v1.39.0/go.mod h1:88w4/PnZSazkGzz/w84VHpQafiU4EtqqlVdxWy+rNOA=
76+
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
77+
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
4678
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
4779
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
4880
go.uber.org/zap v1.27.1 h1:08RqriUEv8+ArZRYSTXy1LeBScaMpVSTBhCeaZYfMYc=
4981
go.uber.org/zap v1.27.1/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
5082
golang.org/x/sys v0.38.0 h1:3yZWxaJjBmCWXqhN1qh02AkOnCQ1poK6oF+a7xWL6Gc=
5183
golang.org/x/sys v0.38.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
84+
golang.org/x/sys v0.39.0 h1:CvCKL8MeisomCi6qNZ+wbb0DN9E5AATixKsvNtMoMFk=
85+
golang.org/x/sys v0.39.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
86+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
87+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
88+
gotest.tools/v3 v3.5.2 h1:7koQfIKdy+I8UTetycgUqXWSDwpgv193Ka+qRsmBY8Q=
89+
gotest.tools/v3 v3.5.2/go.mod h1:LtdLGcnqToBH83WByAAi/wiwSFCArdFIUV/xxN4pcjA=
90+
pgregory.net/rapid v1.2.0 h1:keKAYRcjm+e1F0oAuU5F5+YPAWcyxNNRK2wud503Gnk=
91+
pgregory.net/rapid v1.2.0/go.mod h1:PY5XlDGj0+V1FCq0o192FdRhpKHGTRIWBgqjDBTrq04=

internals/server/http.go

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@ import (
55
"net/http"
66
"net/url"
77
"strconv"
8-
"text/template"
8+
"strings"
99
"time"
1010

1111
"github.com/codeshelldev/gotl/pkg/logger"
12-
"github.com/codeshelldev/gotl/pkg/templating"
1312
"github.com/codeshelldev/wol-dockerized/internals/config"
1413
"github.com/codeshelldev/wol-dockerized/internals/wol"
1514
)
@@ -68,10 +67,12 @@ func wakeHandler(w http.ResponseWriter, req *http.Request) {
6867
err = wol.WakeContainers(body.Query)
6968

7069
if err != nil {
70+
logger.Error("Could not start container with ", body.Query, ": ", err.Error())
71+
7172
sendToClient(client, map[string]any{
7273
"success": false,
7374
"error": true,
74-
"message": "Could not start containers",
75+
"message": err.Error(),
7576
})
7677

7778
closeClient(client)
@@ -80,8 +81,8 @@ func wakeHandler(w http.ResponseWriter, req *http.Request) {
8081

8182
sendToClient(client, map[string]any{
8283
"success": true,
83-
"error": true,
84-
"message": "Started containers",
84+
"error": false,
85+
"message": "Started containers.",
8586
})
8687

8788
closeClient(client)
@@ -101,25 +102,28 @@ func activityHandler(w http.ResponseWriter, req *http.Request) {
101102
return
102103
}
103104

104-
variables := map[string]any{
105+
logger.Debug("Activity on ", urlStr, "detected")
106+
107+
variables := map[string]string{
105108
"HOSTNAME": URL.Hostname(),
106109
"HOST": URL.Host,
107-
"PORT": URL.Port,
110+
"PORT": URL.Port(),
108111
"PROTOCOL": URL.Scheme,
109112
"PATH": URL.Path,
110113
}
111114

112-
templt := templating.CreateTemplateWithFunc("query", template.FuncMap{})
113-
templt.Delims("{", "}")
114-
115-
query, err := templating.ParseTemplate(templt, config.ENV.QUERY_PATTERN, variables)
116-
117-
if err != nil {
118-
logger.Error("Error building query: ", err.Error())
119-
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
120-
}
115+
query := buildQuery(config.ENV.QUERY_PATTERN, variables)
121116

122117
wol.OnActivity(query)
123118

124119
w.WriteHeader(http.StatusOK)
120+
}
121+
122+
func buildQuery(pattern string, context map[string]string) string {
123+
result := pattern
124+
for k, v := range context {
125+
placeholder := "{" + k + "}"
126+
result = strings.ReplaceAll(result, placeholder, v)
127+
}
128+
return result
125129
}

internals/server/websocket.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ var upgrader = websocket.Upgrader{
1414
CheckOrigin: func(r *http.Request) bool { return true },
1515
}
1616

17-
var waiters = make(map[string]chan *websocket.Conn)
17+
var waiters = map[string]chan *websocket.Conn{}
1818
var waitersMutex = &sync.Mutex{}
1919

20-
var clients = make(map[string]*websocket.Conn)
20+
var clients = map[string]*websocket.Conn{}
2121
var clientsMutex = &sync.Mutex{}
2222

2323
func websocketHandler(w http.ResponseWriter, req *http.Request) {

internals/wol/wol.go

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func OnActivity(query string) {
2828
}
2929

3030
func Monitor(threshold int) {
31-
logger.Dev("Performing activity check")
31+
logger.Debug("Performing activity check")
3232
if updateContainers() {
3333
doActivityCheck(threshold)
3434
}
@@ -48,19 +48,27 @@ func doActivityCheck(threshold int) {
4848
containerQueryMutex.RUnlock()
4949

5050
for query, lastTime := range lastActivities {
51-
if currentTime - lastTime > threshold64 {
52-
logger.Info("Containers with ", query, " have been flagged for Inactivity")
53-
logger.Debug("Stopping Containers with ", query)
51+
timePassed := currentTime - lastTime
52+
53+
logger.Dev("Last activity for ", query, " ", timePassed, "s ago")
54+
55+
if timePassed > threshold64 {
56+
logger.Info("Containers with ", query, " have been flagged for inactivity")
5457

5558
ids := containerIDs[query]
56-
resetLastActivity(query)
59+
removeLastActivity(query)
5760

5861
if len(ids) <= 0 {
5962
continue
6063
}
6164

65+
logger.Debug("Stopping containers with ", query)
66+
6267
for _, id := range ids {
63-
if getLabel(id, WOL_AUTOSTOP) == "true" {
68+
autostop := getLabel(id, WOL_AUTOSTOP)
69+
70+
if strings.ToLower(autostop) != "false" {
71+
logger.Dev("Stopping container ", id)
6472
_, err := docker.StopContainer(id, client.ContainerStopOptions{})
6573

6674
if err != nil {
@@ -75,7 +83,10 @@ func doActivityCheck(threshold int) {
7583
func WakeContainers(query string) error {
7684
query = strings.TrimSpace(query)
7785

86+
logger.Dev("Waking container with ", query)
87+
7888
containerQueryMutex.RLock()
89+
logger.Dev("Queries: ", containerQueries)
7990
containers, exists := containerQueries[query]
8091
containerQueryMutex.RUnlock()
8192

@@ -86,6 +97,12 @@ func WakeContainers(query string) error {
8697
logger.Debug("Found ", len(containers), " with query ", query)
8798

8899
for _, containerID := range containers {
100+
if logger.IsDebug() {
101+
logger.Debug("Starting container ", containerID, " with ", query)
102+
} else {
103+
logger.Info("Starting container with ", query)
104+
}
105+
89106
_, err := docker.StartContainer(containerID, client.ContainerStartOptions{})
90107

91108
if err != nil {
@@ -180,6 +197,12 @@ func resetLastActivity(query string) {
180197
queryLastActivityMutex.Unlock()
181198
}
182199

200+
func removeLastActivity(query string) {
201+
queryLastActivityMutex.Lock()
202+
delete(queryLastActivity, query)
203+
queryLastActivityMutex.Unlock()
204+
}
205+
183206
func getLabel(id, label string) string {
184207
container, err := docker.GetContainer(id, client.ContainerInspectOptions{})
185208

@@ -195,6 +218,7 @@ func getEnabledContainers() ([]container.Summary, error) {
195218
filters.Add("label", "wol.enable=true")
196219

197220
return docker.GetContainers(client.ContainerListOptions{
221+
All: true,
198222
Filters: filters,
199223
})
200224
}

main.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/codeshelldev/wol-dockerized/internals/wol"
1212
)
1313

14+
//s
1415
func main() {
1516
config.Load()
1617

@@ -35,11 +36,13 @@ func main() {
3536
}
3637

3738
go func() {
38-
ticker := time.NewTicker(time.Duration(config.ENV.MONITOR_INTERVAL) * time.Second)
39+
wol.Monitor(config.ENV.INACTIVITY_THRESHOLD)
40+
41+
ticker := time.NewTicker(time.Duration(config.ENV.INACTIVITY_THRESHOLD) * time.Second)
3942
defer ticker.Stop()
4043

4144
for range ticker.C {
42-
wol.Monitor(config.ENV.MONITOR_INTERVAL)
45+
wol.Monitor(config.ENV.INACTIVITY_THRESHOLD)
4346
}
4447
}()
4548

0 commit comments

Comments
 (0)