forked from ipfs-search/ipfs-search
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
101 lines (77 loc) · 1.69 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
/*
Search engine for IPFS using Elasticsearch, RabbitMQ and Tika.
*/
package main
import (
"context"
"fmt"
"github.com/ipfs-search/ipfs-search/commands"
"gopkg.in/urfave/cli.v1"
"log"
"os"
"os/signal"
"syscall"
)
func main() {
// Prefix logging with filename and line number: "d.go:23"
// log.SetFlags(log.Lshortfile)
// Logging w/o prefix
log.SetFlags(0)
app := cli.NewApp()
app.Name = "ipfs-search"
app.Usage = "IPFS search engine."
app.Commands = []cli.Command{
{
Name: "add",
Aliases: []string{"a"},
Usage: "add `HASH` to crawler queue",
Action: add,
},
{
Name: "crawl",
Aliases: []string{"c"},
Usage: "start crawler",
Action: crawl,
},
}
app.Run(os.Args)
}
func add(c *cli.Context) error {
if c.NArg() != 1 {
return cli.NewExitError("Please supply one hash as argument.", 1)
}
hash := c.Args().Get(0)
fmt.Printf("Adding hash '%s' to queue\n", hash)
err := commands.AddHash(hash)
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
return nil
}
// onSigTerm calls f() when SIGTERM (control-C) is received
func onSigTerm(f func()) {
sigChan := make(chan os.Signal, 2)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
var fail = func() {
<-sigChan
os.Exit(1)
}
var quit = func() {
<-sigChan
go fail()
fmt.Println("Received SIGTERM, quitting... One more SIGTERM and we'll abort!")
f()
}
go quit()
}
func crawl(c *cli.Context) error {
fmt.Printf("Starting worker\n")
ctx, cancel := context.WithCancel(context.Background())
// Allow SIGTERM / Control-C quit through context
onSigTerm(cancel)
err := commands.Crawl(ctx)
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
return nil
}