Compare commits
No commits in common. "c0e643f29945773ec43b7c4bae29d611fb841f2b" and "416145dfaa07fcb49560212ad6d04d5cea51444b" have entirely different histories.
c0e643f299
...
416145dfaa
9 changed files with 12 additions and 112 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -1,3 +1,2 @@
|
||||||
*.db
|
*.db
|
||||||
data
|
beretta*
|
||||||
bin
|
|
||||||
|
|
|
@ -1,16 +0,0 @@
|
||||||
FROM docker.io/golang:1.24 AS builder
|
|
||||||
WORKDIR /app
|
|
||||||
COPY go.mod go.sum ./
|
|
||||||
RUN go mod download
|
|
||||||
COPY src src
|
|
||||||
RUN GOOS=linux go build -o beretta src/*.go
|
|
||||||
# FROM docker.io/alpine:3
|
|
||||||
FROM docker.io/debian:bookworm-slim
|
|
||||||
|
|
||||||
RUN apt-get update && apt-get install -y \
|
|
||||||
ca-certificates \
|
|
||||||
sqlite3 \
|
|
||||||
&& rm -rf /var/lib/apt/lists/*
|
|
||||||
|
|
||||||
COPY --from=builder /app/beretta /beretta
|
|
||||||
ENTRYPOINT ["/beretta"]
|
|
7
Makefile
7
Makefile
|
@ -1,11 +1,8 @@
|
||||||
build:
|
|
||||||
go build -o bin/beretta src/*.go
|
|
||||||
|
|
||||||
run:
|
run:
|
||||||
go run src/*.go
|
go run src/*.go
|
||||||
|
|
||||||
build-container:
|
build:
|
||||||
podman build -t beretta .
|
go build -o beretta src/*.go
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm beretta
|
rm beretta
|
||||||
|
|
|
@ -10,6 +10,6 @@
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"interval": 1200,
|
"interval": 1200,
|
||||||
"ntfy_topic": "{{.TopicToken}}"
|
"ntfy_topic": "hee4waeNguch"
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,14 +0,0 @@
|
||||||
[Unit]
|
|
||||||
Description=Beretta Service
|
|
||||||
After=network.target
|
|
||||||
|
|
||||||
[Service]
|
|
||||||
Type=simple
|
|
||||||
ExecStart=/usr/local/bin/beretta
|
|
||||||
Restart=on-failure
|
|
||||||
RestartSec=5
|
|
||||||
User=beretta
|
|
||||||
WorkingDirectory=/usr/local/bin
|
|
||||||
|
|
||||||
[Install]
|
|
||||||
WantedBy=multi-user.target
|
|
|
@ -1,11 +0,0 @@
|
||||||
services:
|
|
||||||
beretta:
|
|
||||||
image: beretta:latest
|
|
||||||
name: beretta-server
|
|
||||||
build:
|
|
||||||
context: .
|
|
||||||
command: >
|
|
||||||
-config /data/beretta.json
|
|
||||||
-db /data/cache.db
|
|
||||||
volumes:
|
|
||||||
- ./data:/data:Z
|
|
48
src/main.go
48
src/main.go
|
@ -1,18 +1,12 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
_ "embed"
|
|
||||||
"flag"
|
"flag"
|
||||||
"log"
|
"log"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"os"
|
|
||||||
"text/template"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:embed beretta.json
|
|
||||||
var defconfig string
|
|
||||||
|
|
||||||
type RepoUpdate struct {
|
type RepoUpdate struct {
|
||||||
repo Repo
|
repo Repo
|
||||||
Tag string
|
Tag string
|
||||||
|
@ -28,7 +22,7 @@ func notifierThread(c chan RepoUpdate, n Notifier) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func repoThread(c chan RepoUpdate, repo Repo, avgInterval int, db Db) {
|
func repoThread(c chan RepoUpdate, repo Repo, avgInterval int, db Db) {
|
||||||
log.Println("Checking: ", repo.IdTuple())
|
log.Println("Checking", repo.IdTuple())
|
||||||
tag, err := repo.GetLatestTag()
|
tag, err := repo.GetLatestTag()
|
||||||
isNewVersion, err_2 := db.CheckAndStore(repo.IdTuple(), tag.Name)
|
isNewVersion, err_2 := db.CheckAndStore(repo.IdTuple(), tag.Name)
|
||||||
|
|
||||||
|
@ -39,7 +33,6 @@ func repoThread(c chan RepoUpdate, repo Repo, avgInterval int, db Db) {
|
||||||
} else if isNewVersion {
|
} else if isNewVersion {
|
||||||
c <- RepoUpdate{repo, tag.Name}
|
c <- RepoUpdate{repo, tag.Name}
|
||||||
}
|
}
|
||||||
log.Println("Checking done: ", repo.IdTuple())
|
|
||||||
|
|
||||||
sleepTime := time.Duration(rand.Intn(avgInterval)) * time.Second
|
sleepTime := time.Duration(rand.Intn(avgInterval)) * time.Second
|
||||||
time.Sleep(sleepTime)
|
time.Sleep(sleepTime)
|
||||||
|
@ -48,54 +41,25 @@ func repoThread(c chan RepoUpdate, repo Repo, avgInterval int, db Db) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
conf_path := flag.String("config", "beretta.json", "The path to the config file")
|
|
||||||
db_path := flag.String("db", "cache.db", "The path to the cache database")
|
conf := flag.String("config", "./beretta.json", "The path to the config file")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
tmpl, err := template.New("config").Parse(defconfig)
|
log.Printf("Using config path: %s", *conf)
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
randtk, err := randToken(8)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
data := map[string]any{
|
|
||||||
"TopicToken": randtk,
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := os.Stat(*conf_path); os.IsNotExist(err) {
|
|
||||||
log.Printf("Writing new config file: %s", *conf_path)
|
|
||||||
file, err := os.Create(*conf_path)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
err = tmpl.Execute(file, data)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("Error writing config file: %s", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Printf("Using config path: %s", *conf_path)
|
|
||||||
log.Printf("Using db path: %s", *db_path)
|
|
||||||
|
|
||||||
// Load configuration
|
// Load configuration
|
||||||
config, err := loadConfig(*conf_path)
|
config, err := loadConfig(*conf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to load configuration: %v", err)
|
log.Fatalf("Failed to load configuration: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create database
|
// Create database
|
||||||
db, err := NewSQLiteDb(*db_path)
|
db, err := NewSQLiteDb("tags.db")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to create database: %v", err)
|
log.Fatalf("Failed to create database: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
notifier := NtfyNotifier{Topic: config.NtfyTopic}
|
notifier := NtfyNotifier{Topic: config.NtfyTopic}
|
||||||
log.Printf("Sending notifications to: %s", notifier.Url())
|
|
||||||
|
|
||||||
c := make(chan RepoUpdate)
|
c := make(chan RepoUpdate)
|
||||||
|
|
||||||
|
|
|
@ -1,14 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"crypto/rand"
|
|
||||||
"encoding/hex"
|
|
||||||
)
|
|
||||||
|
|
||||||
func randToken(n int) (string, error) {
|
|
||||||
bytes := make([]byte, n)
|
|
||||||
if _, err := rand.Read(bytes); err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
return hex.EncodeToString(bytes), nil
|
|
||||||
}
|
|
|
@ -39,7 +39,6 @@ func loadConfig(file string) (Config, error) {
|
||||||
|
|
||||||
type Notifier interface {
|
type Notifier interface {
|
||||||
Notify(repo Repo, tag string) bool
|
Notify(repo Repo, tag string) bool
|
||||||
Url() string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type NtfyNotifier struct {
|
type NtfyNotifier struct {
|
||||||
|
@ -47,10 +46,10 @@ type NtfyNotifier struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n NtfyNotifier) Notify(repo Repo, tag string) bool {
|
func (n NtfyNotifier) Notify(repo Repo, tag string) bool {
|
||||||
// ntfyURL := fmt.Sprintf("https://ntfy.sh/%s", n.Topic)
|
ntfyURL := fmt.Sprintf("https://ntfy.sh/%s", n.Topic)
|
||||||
message := fmt.Sprintf("New release for %s/%s: %s", repo.Owner(), repo.Name(), tag)
|
message := fmt.Sprintf("New release for %s/%s: %s", repo.Owner(), repo.Name(), tag)
|
||||||
|
|
||||||
_, err := http.Post(n.Url(), "text/plain", bytes.NewBuffer([]byte(message)))
|
_, err := http.Post(ntfyURL, "text/plain", bytes.NewBuffer([]byte(message)))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Failed to send notification: %v", err)
|
log.Printf("Failed to send notification: %v", err)
|
||||||
return false
|
return false
|
||||||
|
@ -58,7 +57,3 @@ func (n NtfyNotifier) Notify(repo Repo, tag string) bool {
|
||||||
log.Printf("Notification sent: %s", message)
|
log.Printf("Notification sent: %s", message)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n NtfyNotifier) Url() string {
|
|
||||||
return fmt.Sprintf("https://ntfy.sh/%s", n.Topic)
|
|
||||||
}
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue