Skip to content

Commit a29f2b1

Browse files
committed
feat: changed default storage backend from memory to boltdb. Removed storage type configuration from config file and moved to the app argument.
1 parent e247678 commit a29f2b1

File tree

7 files changed

+89
-46
lines changed

7 files changed

+89
-46
lines changed

.dockerignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ mock_test.go
77
.run
88
example.yaml
99
release
10-
release.sh
10+
release.sh
11+
data

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ config.yaml
44
mock_test.go
55
*.db
66
.run
7-
release
7+
release
8+
data

README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# JAM
22

3+
<a href="https://github.com/exelban/JAM"><p align="center"><img src="https://github.com/exelban/JAM/raw/master/templates/static/icon.png" width="120"></p></a>
4+
35
[![JAM](https://serhiy.s3.eu-central-1.amazonaws.com/Github_repo/JAM/cover.png)](https://github.com/exelban/JAM)
46

57
Just Another Monitoring
@@ -28,7 +30,7 @@ Also you can build it from the source code or use the precompiled binaries. But
2830

2931
### Docker
3032
```bash
31-
docker run -d -v ./jam.yaml:/app/config.yaml exelban/jam:latest
33+
docker run -d -v ./jam.yaml:/app/config.yaml -v ./data/:/app/data exelban/jam:latest
3234
```
3335

3436
### Docker Compose
@@ -40,6 +42,7 @@ services:
4042
restart: unless-stopped
4143
volumes:
4244
- ./jam.yaml:/app/config.yaml
45+
- ./data:/app/data
4346
logging:
4447
driver: "json-file"
4548
options:

main.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@ import (
1919

2020
type arguments struct {
2121
ConfigPath string `long:"config-path" env:"CONFIG_PATH" default:"./config.yaml" description:"path to the configuration file"`
22-
Port int `long:"port" env:"PORT" default:"8822" description:"service rest port"`
23-
Debug bool `long:"debug" env:"DEBUG" description:"debug mode"`
22+
23+
StorageType string `long:"storage-type" env:"STORAGE_TYPE" default:"bolt" description:"storage type"`
24+
25+
Port int `long:"port" env:"PORT" default:"8822" description:"service rest port"`
26+
Debug bool `long:"debug" env:"DEBUG" description:"debug mode"`
2427
}
2528

2629
type app struct {
@@ -82,7 +85,7 @@ func create(ctx context.Context, args arguments) (*app, error) {
8285
return nil, fmt.Errorf("new config: %w", err)
8386
}
8487

85-
storage, err := store.New(ctx, cfg)
88+
storage, err := store.New(ctx, args.StorageType, cfg)
8689
if err != nil {
8790
return nil, fmt.Errorf("new store: %w", err)
8891
}

release.sh

+53-16
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,31 @@
22

33
set -eu
44

5-
version="v0.0.0"
5+
version="v0.0.1"
6+
latestOnly=false
7+
dockerOnly=false
8+
9+
while [[ $# -gt 0 ]]; do
10+
case $1 in
11+
-v|--version)
12+
version="$2"
13+
shift
14+
shift
15+
;;
16+
-l|--latest)
17+
latestOnly=true
18+
shift
19+
;;
20+
-d|--docker)
21+
dockerOnly=true
22+
shift
23+
;;
24+
-*|--*)
25+
echo "Unknown option $1"
26+
exit 1
27+
;;
28+
esac
29+
done
630

731
build_darwin() {
832
GOOS=darwin GOARCH=amd64 go build -ldflags "-X main.version=$version" -o bin/jam && tar -czf release/jam_"$version"_darwin_x86_64.tar.gz -C bin jam && rm bin/jam
@@ -18,30 +42,43 @@ build_windows() {
1842
}
1943

2044
build_docker_hub() {
21-
docker buildx build --push --platform=linux/amd64,linux/arm64 --tag=exelban/jam:"$version" .
45+
if [ "$latestOnly" = false ]; then
46+
docker buildx build --push --platform=linux/amd64,linux/arm64 --tag=exelban/jam:"$version" .
47+
fi
2248
docker buildx build --push --platform=linux/amd64,linux/arm64 --tag=exelban/jam:latest .
2349
}
2450
build_github_registry() {
25-
docker buildx build --push --platform=linux/amd64,linux/arm64 --tag=ghcr.io/exelban/jam:"$version" .
51+
if [ "$latestOnly" = false ]; then
52+
docker buildx build --push --platform=linux/amd64,linux/arm64 --tag=ghcr.io/exelban/jam:"$version" .
53+
fi
2654
docker buildx build --push --platform=linux/amd64,linux/arm64 --tag=ghcr.io/exelban/jam:latest .
2755
}
2856

29-
printf "\033[32;1m%s\033[0m\n" "Building docker image with version ${version}..."
57+
if [ "$latestOnly" = true ]; then
58+
printf "\033[32;1m%s\033[0m\n" "Building latest version only..."
59+
else
60+
printf "\033[32;1m%s\033[0m\n" "Building version ${version}..."
61+
fi
62+
3063
build_docker_hub
3164
build_github_registry
3265

33-
printf "\033[32;1m%s\033[0m\n" "Building precompiled binaries with version ${version}..."
66+
if [ "$dockerOnly" = false ]; then
67+
printf "\033[32;1m%s\033[0m\n" "Building precompiled binaries with version ${version}..."
68+
69+
rm -rf "bin" && rm -rf "release"
70+
mkdir -p "release"
3471

35-
rm -rf "bin" && rm -rf "release"
36-
mkdir -p "release"
72+
echo "Building darwin..."
73+
build_darwin
74+
echo "Building linux..."
75+
build_linux
76+
echo "Building windows..."
77+
build_windows
78+
rm -rf "bin"
3779

38-
echo "Building darwin..."
39-
build_darwin
40-
echo "Building linux..."
41-
build_linux
42-
echo "Building windows..."
43-
build_windows
44-
rm -rf "bin"
80+
printf "\033[32;1m%s\033[0m\n" "JAM ${version} was successfully build."
81+
open release
82+
fi
4583

46-
printf "\033[32;1m%s\033[0m\n" "JAM ${version} was successfully build."
47-
open release
84+
exit 0

store/store.go

+18-14
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/exelban/JAM/types"
77
"log"
88
"math/rand/v2"
9+
"os"
910
"time"
1011
)
1112

@@ -18,25 +19,28 @@ type Interface interface {
1819
Close() error
1920
}
2021

21-
func New(ctx context.Context, cfg *types.Cfg) (Interface, error) {
22+
func New(ctx context.Context, typ string, cfg *types.Cfg) (Interface, error) {
2223
var store Interface
2324

24-
if cfg != nil && cfg.Storage != nil {
25-
path := "./jam.db"
26-
if cfg.Storage.Path != nil {
27-
path = fmt.Sprintf("%s/jam.db", *cfg.Storage.Path)
25+
dbPath := "./data"
26+
if _, err := os.Stat(dbPath); os.IsNotExist(err) {
27+
if err := os.Mkdir(dbPath, 0755); err != nil {
28+
return nil, fmt.Errorf("failed to create data directory: %w", err)
2829
}
30+
}
31+
dbFilePath := fmt.Sprintf("%s/%s", dbPath, "jam.db")
2932

30-
switch cfg.Storage.Type {
31-
case "bolt":
32-
s, err := NewBolt(ctx, path)
33-
if err != nil {
34-
return nil, err
35-
}
36-
store = s
37-
}
38-
} else {
33+
switch typ {
34+
case "memory":
3935
store = NewMemory(ctx)
36+
log.Printf("[INFO] using memory storage")
37+
default:
38+
s, err := NewBolt(ctx, dbFilePath)
39+
if err != nil {
40+
return nil, err
41+
}
42+
store = s
43+
log.Printf("[INFO] using bolt storage")
4044
}
4145

4246
if err := Aggregate(ctx, store); err != nil {

types/config.go

+4-10
Original file line numberDiff line numberDiff line change
@@ -31,28 +31,22 @@ type Alerts struct {
3131
ShutdownMessage bool `json:"shutdownMessage" yaml:"shutdownMessage"`
3232
}
3333

34-
type Storage struct {
35-
Type string `json:"type" yaml:"type"`
36-
Path *string `json:"path,omitempty" yaml:"path,omitempty"`
37-
}
38-
3934
type Cfg struct {
4035
MaxConn int `json:"maxConn" yaml:"maxConn,omitempty"`
4136

4237
Interval time.Duration `json:"interval,omitempty" yaml:"interval,omitempty"`
4338
Timeout time.Duration `json:"timeout,omitempty" yaml:"timeout,omitempty"`
4439
InitialDelay *time.Duration `json:"initialDelay,omitempty" yaml:"initialDelay,omitempty"`
45-
40+
4641
SuccessThreshold int `json:"successThreshold" yaml:"successThreshold,omitempty"`
4742
FailureThreshold int `json:"failureThreshold" yaml:"failureThreshold,omitempty"`
4843

4944
Conditions *Success `json:"success" yaml:"success,omitempty"`
5045
Headers map[string]string `json:"headers" yaml:"headers,omitempty"`
5146

52-
Alerts Alerts `json:"alerts" yaml:"alerts,omitempty"`
53-
Storage *Storage `json:"storage" yaml:"storage,omitempty"`
54-
FileHosts []*Host `json:"hosts" yaml:"hosts"`
55-
Hosts []*Host `json:"-" yaml:"-"`
47+
Alerts Alerts `json:"alerts" yaml:"alerts,omitempty"`
48+
FileHosts []*Host `json:"hosts" yaml:"hosts"`
49+
Hosts []*Host `json:"-" yaml:"-"`
5650

5751
path string `yaml:"-"`
5852
initialized bool `yaml:"-"`

0 commit comments

Comments
 (0)