This repository has been archived by the owner on Jun 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
92 lines (73 loc) · 1.78 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
package main
import (
"fmt"
"net/http"
"sync"
"time"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/pengsrc/go-shared/check"
"github.com/spf13/cobra"
"github.com/Xuanwo/xuanzang/config"
"github.com/Xuanwo/xuanzang/constants"
"github.com/Xuanwo/xuanzang/contexts"
"github.com/Xuanwo/xuanzang/index"
"github.com/Xuanwo/xuanzang/search"
)
var application = &cobra.Command{
Use: constants.Name,
Short: constants.Name,
Long: constants.Name,
Run: func(cmd *cobra.Command, args []string) {
if flagVersion {
fmt.Printf("%s version %s\n", constants.Name, constants.Version)
return
}
// Initialize config.
c := config.New()
check.ErrorForExit(constants.Name, c.LoadFromFilePath(flagConfig))
check.ErrorForExit(constants.Name, c.Check())
// Setup contexts.
check.ErrorForExit(constants.Name, contexts.SetupContexts(c))
defer contexts.Searcher.Close()
defer contexts.DB.Close()
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
// Do index firstly.
go index.Index()
// Execute index for every Source.Duration seconds.
ticker := time.NewTicker(time.Duration(c.Source.Duration) * time.Second)
for _ = range ticker.C {
go index.Index()
}
}()
wg.Add(1)
go func() {
defer wg.Done()
r := chi.NewRouter()
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
r.Get("/", search.Search)
addr := fmt.Sprintf("%s:%d", *c.Host, *c.Port)
http.ListenAndServe(addr, r)
}()
wg.Wait()
},
}
var (
flagVersion bool
flagConfig string
)
func init() {
application.Flags().BoolVarP(
&flagVersion, "version", "v", false, "Show version",
)
application.Flags().StringVarP(
&flagConfig, "config", "c", "xuanzang.yaml", "Specify config file path",
)
}
func main() {
application.Execute()
}