This repository has been archived by the owner on Oct 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
103 lines (90 loc) · 2.16 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package main
import (
"context"
_ "embed"
"fmt"
"os"
"strings"
"time"
"github.com/urfave/cli/v2"
)
//go:embed VERSION
var rawVersion string
var version string
func init() {
version = rawVersion
if idx := strings.Index(version, "\n"); idx > -1 {
version = version[:idx]
}
}
func main() {
ctx := context.Background()
if err := app.RunContext(ctx, os.Args); err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
}
const appName = "ipfsfiled"
var app = &cli.App{
Name: appName,
Usage: "watches a filesystem and serves its files over ipfs",
Version: version,
Flags: flagSet(
flags,
loggingFlags,
diagnosticsFlags,
),
Before: configure,
Action: func(cctx *cli.Context) error {
ctx := cctx.Context
p, err := NewPeer(&PeerConfig{
ListenAddr: config.listenAddr,
DatastorePath: config.datastorePath,
FileSystemPath: config.fileSystemPath,
Libp2pKeyFile: config.libp2pKeyfile,
ManifestPath: config.manifestPath,
Offline: config.offline,
})
if err != nil {
return fmt.Errorf("new ipfs peer: %w", err)
}
defer p.Close()
var syncChan <-chan time.Time
if config.syncInterval > 0 {
syncScheduler := time.NewTicker(config.syncInterval)
syncChan = syncScheduler.C
} else {
logger.Info("scheduled filesystem sync is disabled")
}
var gcChan <-chan time.Time
if config.garbageCollectionInterval > 0 {
gcScheduler := time.NewTicker(config.garbageCollectionInterval)
gcChan = gcScheduler.C
} else {
logger.Info("scheduled garbage collection is disabled")
}
// Perform initial sync
if err := p.Sync(ctx); err != nil {
logger.Errorf("sync failure: %v", err)
}
// Register all existing files with reprovider
if err := p.ProvideExistingFiles(ctx); err != nil {
logger.Errorf("provide failure: %v", err)
}
// TODO: fsnotify support to augment polling
for {
select {
case <-cctx.Context.Done():
return nil
case <-syncChan:
if err := p.Sync(ctx); err != nil {
logger.Errorf("sync failure: %v", err)
}
case <-gcChan:
if err := p.GarbageCollect(ctx); err != nil {
logger.Errorf("garbage collection failure: %v", err)
}
}
}
},
}