-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
92 lines (77 loc) · 2.49 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"
"github.com/jplu/visual-search-backend/cmd"
"github.com/jplu/visual-search-backend/implem/ann.faiss"
"github.com/jplu/visual-search-backend/implem/features.image"
"github.com/jplu/visual-search-backend/implem/router.gin"
"github.com/jplu/visual-search-backend/infra"
"github.com/jplu/visual-search-backend/uc"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// Build number and versions injected at compile time, set yours
var (
Version = "unknown"
Build = "unknown"
)
// Main command that will be run when no other command is provided on the
// command-line
var rootCmd = &cobra.Command{
Use: "visual-search-backend",
Short: "visual-search-backend",
Run: func(cmd *cobra.Command, args []string) { run() }, // nolint: unparam
}
// Version command that will display the build number and version (if any)
var versionCmd = &cobra.Command{
Use: "version",
Short: "Show build and version",
Run: func(cmd *cobra.Command, args []string) { fmt.Printf("Build: %s\nVersion: %s\n", Build, Version) }, // nolint: unparam
}
func main() {
// Initialize Cobra and Viper
cobra.OnInitialize(cmd.Initialize)
cmd.AddAllFlags(rootCmd)
rootCmd.AddCommand(versionCmd)
// Run the command
if err := rootCmd.Execute(); err != nil {
logrus.WithError(err).Fatal("Couldn't start")
}
}
func run() {
logrus.WithField("version", Version).WithField("build", Build).Info("Starting")
// Setup the server with cors and stuff
s := infra.NewServer(
viper.GetString("server.host"),
viper.GetInt("server.port"),
viper.GetString("server.mode"),
infra.NewCorsConfig(
viper.GetBool("server.cors.disabled"),
viper.GetBool("server.cors.all"),
viper.GetStringSlice("server.cors.origins"),
viper.GetStringSlice("server.cors.methods"),
viper.GetStringSlice("server.cors.headers"),
viper.GetStringSlice("server.cors.expose"),
),
)
var err error
var imageModelClient uc.Image
if imageModelClient, err = image.NewImageModelClient(viper.GetString("img.host"), viper.GetInt("img.port")); imageModelClient == nil {
logrus.WithError(err).Fatal("Couldn't connect to the Image Model gRPC server")
}
var faissClient uc.Ann
if faissClient, err = faiss.NewFaissClient(viper.GetString("ann.host"), viper.GetInt("ann.port")); faissClient == nil {
logrus.WithError(err).Fatal("Couldn't connect to the ANN gRPC server")
}
i := uc.NewInteractor(
imageModelClient,
faissClient,
)
r := router.NewRouter(
i,
s.Router,
)
r.SetRoutes()
s.Start()
}