Skip to content

Commit b814301

Browse files
committed
- fix: fixed watcher error that causes sending alert on every check
- feat: added proper rest server url on start
1 parent 3c47ded commit b814301

File tree

6 files changed

+36
-23
lines changed

6 files changed

+36
-23
lines changed
Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,16 @@ jobs:
1313
- name: Checkout repository
1414
uses: actions/checkout@v4
1515

16+
- name: Set up QEMU
17+
uses: docker/setup-qemu-action@v3
18+
19+
- name: Set up Docker Buildx
20+
uses: docker/setup-buildx-action@v3
21+
1622
- name: Log in to Docker Hub
1723
uses: docker/login-action@v3
1824
with:
19-
username: ${{ secrets.DOCKERHUB_USERNAME }}
25+
username: ${{ github.repository_owner }}
2026
password: ${{ secrets.DOCKERHUB_TOKEN }}
2127

2228
- name: Log in to GitHub Container Registry
@@ -34,14 +40,18 @@ jobs:
3440
uses: docker/build-push-action@v5
3541
with:
3642
push: true
43+
platforms: linux/amd64,linux/arm64
44+
build-args: VERSION=${{ env.TAG }}
3745
tags: |
38-
${{ secrets.DOCKERHUB_USERNAME }}/jam:${{ env.TAG }}
39-
${{ secrets.DOCKERHUB_USERNAME }}/jam:latest
46+
${{ github.repository_owner }}/jam:${{ env.TAG }}
47+
${{ github.repository_owner }}/jam:latest
4048
4149
- name: Build and push Docker image to GitHub Container Registry
4250
uses: docker/build-push-action@v5
4351
with:
4452
push: true
53+
platforms: linux/amd64,linux/arm64
54+
build-args: VERSION=${{ env.TAG }}
4555
tags: |
4656
ghcr.io/${{ github.repository_owner }}/jam:${{ env.TAG }}
4757
ghcr.io/${{ github.repository_owner }}/jam:latest
@@ -63,7 +73,7 @@ jobs:
6373
uses: softprops/action-gh-release@v1
6474
with:
6575
tag_name: ${{ env.TAG }}
66-
name: Release ${{ env.TAG }}
76+
name: ${{ env.TAG }}
6777
body: ${{ env.CHANGELOG }}
6878
draft: false
6979
prerelease: false

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM exelban/baseimage:golang-latest as build-app
1+
FROM exelban/baseimage:golang-latest AS build-app
22

33
ARG VERSION
44

@@ -17,4 +17,4 @@ FROM exelban/baseimage:alpine-latest
1717
EXPOSE 8822
1818
WORKDIR /app
1919
COPY --from=build-app /app/bin/main /app/main
20-
ENTRYPOINT ./main
20+
ENTRYPOINT ["./main"]

api/server.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ func (s *Server) Run(router http.Handler) error {
4242
s.IdleTimeout = 60 * time.Second
4343
}
4444

45-
log.Printf("[INFO] http rest server on %s:%d", s.Address, s.Port)
45+
addr := "http://localhost"
46+
if s.Address != "" {
47+
addr = s.Address
48+
}
49+
log.Printf("[INFO] http rest server on %s:%d", addr, s.Port)
4650

4751
s.mu.Lock()
4852
s.srv = &http.Server{

main.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,6 @@ func main() {
8686
}
8787

8888
func create(ctx context.Context, args arguments) (*app, error) {
89-
log.Printf("[DEBUG] %+v", args)
90-
9189
cfg, err := types.NewConfig(ctx, args.ConfigPath)
9290
if err != nil {
9391
return nil, fmt.Errorf("new config: %w", err)

pkg/monitor/watcher.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func (w *watcher) validate(status bool) {
8989
w.failureCount = 0
9090
if w.host.SuccessThreshold == nil || w.successCount >= *w.host.SuccessThreshold {
9191
newStatus := types.UP
92-
if w.status != types.Unknown {
92+
if w.status != types.Unknown && w.status != types.UP {
9393
if err := w.notify.Set(w.host.Alerts, newStatus, w.host.String()); err != nil {
9494
log.Print(err)
9595
}
@@ -108,7 +108,7 @@ func (w *watcher) validate(status bool) {
108108
w.successCount = 0
109109
if w.host.FailureThreshold == nil || w.failureCount >= *w.host.FailureThreshold {
110110
newStatus := types.DOWN
111-
if w.status != types.Unknown {
111+
if w.status != types.Unknown && w.status != types.DOWN {
112112
if err := w.notify.Set(w.host.Alerts, newStatus, w.host.String()); err != nil {
113113
log.Print(err)
114114
}

types/host_test.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ package types
22

33
import (
44
"crypto/md5"
5-
"encoding/hex"
6-
"fmt"
5+
"encoding/base64"
76
"github.com/stretchr/testify/require"
87
"testing"
98
)
@@ -55,26 +54,28 @@ func TestHost_String(t *testing.T) {
5554
func TestHost_GenerateID(t *testing.T) {
5655
url := "url"
5756
group := "group"
58-
hasher := md5.New()
59-
60-
hasher.Write([]byte(url))
61-
hash := hex.EncodeToString(hasher.Sum(nil))
62-
63-
hasher.Reset()
64-
hasher.Write([]byte(fmt.Sprintf("%s%s", url, group)))
65-
groupHash := hex.EncodeToString(hasher.Sum(nil))
6657

6758
t.Run("url only", func(t *testing.T) {
6859
h := Host{
6960
URL: url,
7061
}
71-
require.Equal(t, hash, h.GenerateID())
62+
hasher := md5.New()
63+
hasher.Write([]byte(url))
64+
hash := hasher.Sum(nil)
65+
expected := base64.URLEncoding.EncodeToString(hash)[:6]
66+
require.Equal(t, expected, h.GenerateID())
7267
})
68+
7369
t.Run("url and group", func(t *testing.T) {
7470
h := Host{
7571
URL: url,
7672
Group: &group,
7773
}
78-
require.Equal(t, groupHash, h.GenerateID())
74+
hasher := md5.New()
75+
input := append([]byte(url), []byte(group)...)
76+
hasher.Write(input)
77+
hash := hasher.Sum(nil)
78+
expected := base64.URLEncoding.EncodeToString(hash)[:6]
79+
require.Equal(t, expected, h.GenerateID())
7980
})
8081
}

0 commit comments

Comments
 (0)