-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathdirsearch.go
71 lines (54 loc) · 1.22 KB
/
dirsearch.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
package main
import (
"encoding/json"
"fmt"
"github.com/jordic/fuzzyfs"
"log"
"net/http"
"time"
)
var dirlist *fuzzyfs.DirList
//@todo change to an init function...
/*func init() {
dirlist = fuzzyfs.NewDirList()
dirlist.MaxDepth = 5
dirlist.PathSelect = fuzzyfs.DirsAndSymlinksAsDirs
}*/
// Build index, starts when app start...
func Build_index(path string) {
if dirlist == nil {
dirlist = fuzzyfs.NewDirList()
dirlist.MaxDepth = depth
dirlist.PathSelect = fuzzyfs.DirsAndSymlinksAsDirs
}
startTime := time.Now()
log.Printf("Building index .. %s, depth: %d", path, depth)
err := dirlist.Populate(path, nil)
if err != nil {
panic(err)
}
endTime := time.Now()
log.Printf("%d entries. time index .. %s", dirlist.Length, endTime.Sub(startTime))
}
func SearchHandle(w http.ResponseWriter, r *http.Request) {
var query string
query = r.FormValue("q")
if len(query) < 3 {
fmt.Fprint(w, "[]")
return
}
res := dirlist.Query(query, 2)
//fmt.Printf("%#v", res)
cut := len(res)
if cut > 10 {
cut = 9
}
out, err := json.Marshal(res[0:cut])
if err != nil {
log.Printf("error encoding results... ", err)
}
w.Header().Set("Content-Type", "application/json")
//fmt.Fprint(w, out)
w.Write(out)
return
}