Skip to content

Commit

Permalink
Merge pull request #50 from bsach64/watcher
Browse files Browse the repository at this point in the history
Added Watcher that watches over the chunk directory
  • Loading branch information
Unic-X authored Nov 23, 2024
2 parents c9e85d9 + 2d99baa commit e308f1f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 22 deletions.
21 changes: 12 additions & 9 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
FROM golang:1.23-alpine AS builder
# FROM golang:1.23-alpine AS builder

WORKDIR /app

COPY go.mod go.sum ./
RUN go mod download
# COPY go.mod go.sum ./
# RUN go mod download

COPY . .
RUN go build -o /goback main.go
# COPY . .
# RUN go build -o /goback main.go

FROM debian:stable-slim

FROM alpine:3.18
WORKDIR /app

COPY . .

RUN apk add --no-cache openssh sqlite && \
RUN apt-get intsall openssh && \
mkdir -p /app/private /app/.data && \
[ ! -f /app/private/id_rsa ] && ssh-keygen -t rsa -b 4096 -f /app/private/id_rsa -N "" || true

COPY --from=builder /goback /usr/local/bin/goback
COPY goback /usr/local/bin/goback

CMD ["goback"]
12 changes: 9 additions & 3 deletions server/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@ type Worker struct {
func (w *Worker) StartSFTPServer() {
wd, err := os.Getwd()
if err != nil {
log.Fatalf("Cannot get the working directory of %v", err)
log.Fatalf("Cannot get the working directory: %v", err)
}

rsaPath := wd + "/private/id_rsa"

err = utils.WatchDirectory("./.data/")
// Start directory watcher
watcher, err := utils.WatchDirectory(wd + "/.data")
if err != nil {
log.Fatalf("Error while creating watcher %v", err)
log.Fatalf("Error while creating watcher: %v", err)
}
defer watcher.Close() // Ensure the watcher is cleaned up on server shutdown

sftpServer := New(w.Ip, rsaPath, w.Port)
w.sftpServer = &sftpServer
Expand All @@ -38,4 +41,7 @@ func (w *Worker) StartSFTPServer() {
log.Fatalf("Worker SFTP server failed to listen: %v", err)
}
}()

// Keep the worker process alive
select {}
}
22 changes: 12 additions & 10 deletions utils/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,46 @@ package utils

import (
"fmt"
"log"

"github.com/charmbracelet/log"
"github.com/fsnotify/fsnotify"
)

func WatchDirectory(directory string) error {
func WatchDirectory(directory string) (*fsnotify.Watcher, error) {
watcher, err := fsnotify.NewWatcher()

if err != nil {
return fmt.Errorf("Error while file watcher")
return nil, fmt.Errorf("error while creating directory watcher: %v", err)
}

defer watcher.Close()
log.Info("Starting to watch directory:", directory)

go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
log.Error("Watcher events channel closed")
return
}
log.Println("event:", event)
log.Info("Event: ", event)
if event.Has(fsnotify.Write) {
log.Println("modified file:", event.Name)
log.Info("File modified: ", event.Name)
}
case err, ok := <-watcher.Errors:
if !ok {
log.Error("Watcher errors channel closed")
return
}
log.Println("error:", err)
log.Errorf("Watcher error: %v", err)
}
}
}()

err = watcher.Add(directory)
if err != nil {
log.Fatal(err)
watcher.Close() // Clean up if watcher.Add fails
return nil, fmt.Errorf("failed to add directory to watcher: %v", err)
}

return nil
return watcher, nil
}

0 comments on commit e308f1f

Please sign in to comment.