-
Notifications
You must be signed in to change notification settings - Fork 0
/
websearch.go
102 lines (97 loc) · 2.24 KB
/
websearch.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
package main
import (
"fmt"
"log"
"os"
"runtime/pprof"
"strings"
"github.com/flofriday/websearch/cmd"
_ "github.com/mattn/go-sqlite3"
"github.com/urfave/cli/v2"
)
func main() {
app := &cli.App{
Name: "websearch",
Usage: "A search engine for the web, just for fun 🥳",
Commands: []*cli.Command{
{
Name: "index",
Usage: "build an index",
Flags: []cli.Flag{
&cli.Int64Flag{
Name: "number",
Aliases: []string{"n"},
Value: 1000,
Usage: "The number of documents to index",
},
&cli.StringFlag{
Name: "sqlite",
Value: "./index.db",
Usage: "Path of the sqlite file",
},
&cli.BoolFlag{
Name: "profile",
Value: false,
Usage: "Start a cpu profile",
},
},
Action: func(cCtx *cli.Context) error {
if cCtx.Bool("profile") {
f, err := os.Create("cpu.prof")
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
cmd.CrawlAndIndex(cCtx.Int64("number"), cCtx.String("sqlite"))
return nil
},
},
{
Name: "server",
Usage: "search the index from the comfort of your browser",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "addr",
Value: ":8080",
Usage: "Port and IP to listen at",
},
&cli.StringFlag{
Name: "sqlite",
Value: "./index.db",
Usage: "Path of the sqlite file",
},
},
Action: func(cCtx *cli.Context) error {
cmd.Serve(cCtx.String("addr"), cCtx.String("sqlite"))
return nil
},
},
{
Name: "search",
Usage: "search the index from the command line",
ArgsUsage: "query",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "sqlite",
Value: "./index.db",
Usage: "Path of the sqlite file",
},
},
Action: func(cCtx *cli.Context) error {
if len(cCtx.Args().Slice()) == 0 {
fmt.Fprintln(os.Stderr, "usage: websearch search query")
fmt.Fprintln(os.Stderr, "Run 'websearch search --help' for more infos.")
return nil
}
cmd.Search(cCtx.String("sqlite"), strings.Join(cCtx.Args().Slice(), " "))
return nil
},
},
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}