-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
93 lines (75 loc) · 2.17 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
package main
import (
"context"
"fmt"
"io/ioutil"
"os"
"time"
"github.com/by12380/Autocomplete/configs"
"github.com/by12380/Autocomplete/routers"
"github.com/by12380/Autocomplete/services/trie"
"github.com/caarlos0/env/v6"
"github.com/gin-gonic/gin"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
"github.com/sirupsen/logrus"
"github.com/tidwall/gjson"
"github.com/weekface/mgorus"
)
type config struct {
AssetsPath string `env:"ASSETS_PATH"`
MongoDBServiceHost string `env:"DEFAULT_MONGODB_SERVICE_HOST"`
MongoDBServicePort string `env:"DEFAULT_MONGODB_SERVICE_PORT"`
}
func main() {
cfg := config{}
env.Parse(&cfg)
mongoDBUrl := "mongodb://" + os.Getenv("DEFAULT_MONGODB_SERVICE_HOST") + ":" + os.Getenv("DEFAULT_MONGODB_SERVICE_PORT")
log := logrus.New()
hook, err := mgorus.NewHooker(mongoDBUrl, "autocomplete", "logs")
if err == nil {
log.Hooks.Add(hook)
} else {
fmt.Print(err)
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
endpoint := os.Getenv("DEFAULT_MINIO_SERVICE_HOST") + ":" + os.Getenv("DEFAULT_MINIO_SERVICE_PORT")
accessKeyID := os.Getenv("MINIO_ACCESS_KEY")
secretAccessKey := os.Getenv("MINIO_SECRET_KEY")
// Initialize minio client object.
minioClient, err := minio.New(endpoint, &minio.Options{
Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
})
if err != nil {
log.Fatalln(err)
}
r := gin.New()
r.Use(configs.Logger(log), gin.Recovery())
routers.InitAutocomplete(r.Group("/autocomplete"))
bucketName := "drive"
objectName := "trie-input.json"
filePath := cfg.AssetsPath + "/trie-input.json"
// Upload the zip file with FPutObject
err = minioClient.FGetObject(ctx, bucketName, objectName, filePath, minio.GetObjectOptions{})
if err != nil {
log.Fatalln(err)
return
}
bytes, err := ioutil.ReadFile(filePath)
if err != nil {
log.Fatalln(err)
return
}
result := gjson.GetBytes(bytes, "items")
// Build trie
t := trie.GetInstance()
for _, item := range result.Array() {
textItem := trie.TextItem{
Value: item.Get("value").String(),
Weight: int(item.Get("weight").Int()),
}
t.Add(&textItem)
}
r.Run()
}