forked from ipfs-search/ipfs-search
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
132 lines (103 loc) · 2.26 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
Search engine for IPFS using Elasticsearch, RabbitMQ and Tika.
*/
package main
import (
"context"
"fmt"
"github.com/ipfs-search/ipfs-search/commands"
"github.com/ipfs-search/ipfs-search/config"
"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.Flags = []cli.Flag{
cli.StringFlag{
Name: "config, c",
Usage: "Load configuration from `FILE`",
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
func getConfig(c *cli.Context) (*config.Config, error) {
configFile := c.GlobalString("config")
cfg, err := config.Get(configFile)
if err != nil {
return nil, err
}
return cfg, nil
}
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)
cfg, err := getConfig(c)
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
fmt.Printf("Adding hash '%s' to queue\n", hash)
err = commands.AddHash(cfg, 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.Println("Starting worker")
ctx, cancel := context.WithCancel(context.Background())
// Allow SIGTERM / Control-C quit through context
onSigTerm(cancel)
cfg, err := getConfig(c)
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
err = commands.Crawl(ctx, cfg)
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
return nil
}