-
Notifications
You must be signed in to change notification settings - Fork 0
/
codeactions.go
108 lines (92 loc) · 2.85 KB
/
codeactions.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
103
104
105
106
107
108
package codeactions
import (
"context"
"os"
"os/signal"
"time"
log "github.com/sirupsen/logrus"
"github.com/weni-ai/flows-code-actions/config"
"github.com/weni-ai/flows-code-actions/internal/codelib"
codelibRepoMongo "github.com/weni-ai/flows-code-actions/internal/codelib/mongodb"
"github.com/weni-ai/flows-code-actions/internal/db"
"github.com/weni-ai/flows-code-actions/internal/eventdriven/rabbitmq"
server "github.com/weni-ai/flows-code-actions/internal/http/echo"
"github.com/weni-ai/flows-code-actions/internal/http/echo/routes"
"github.com/weni-ai/flows-code-actions/internal/permission"
permRepoMongo "github.com/weni-ai/flows-code-actions/internal/permission/mongodb"
"github.com/weni-ai/flows-code-actions/internal/project"
projRepoMongo "github.com/weni-ai/flows-code-actions/internal/project/mongodb"
)
func Start(cfg *config.Config) {
codeactions := server.NewServer(cfg)
db, err := db.GetMongoDatabase(cfg)
if err != nil {
log.WithError(err).Fatal(err)
}
codeactions.DB = db
routes.Setup(codeactions)
if err := SetupLibs(codeactions); err != nil {
log.WithError(err).Fatal(err)
}
go func() {
err := codeactions.Start(cfg.HTTP.Port)
if err != nil {
log.WithError(err).Fatal(err)
}
}()
if cfg.EDA.RabbitmqURL != "" {
eda := rabbitmq.NewEDA(cfg.EDA.RabbitmqURL)
permissionService := permission.NewUserPermissionService(
permRepoMongo.NewUserRepository(db),
)
server.Permission = server.NewEchoPermissionHandler(permissionService)
projectConsumer := project.NewProjectConsumer(
project.NewProjectService(
projRepoMongo.NewProjectRepository(db),
),
permissionService,
cfg.EDA.ProjectExchangeName,
cfg.EDA.ProjectQueueName,
)
permissionConsumer := permission.NewPermissionConsumer(
permissionService,
cfg.EDA.PermissionExchangeName,
cfg.EDA.PermissionQueueName,
)
eda.AddConsumer(projectConsumer)
eda.AddConsumer(permissionConsumer)
if err := eda.StartConsumers(); err != nil {
log.WithError(err)
}
}
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
<-quit
log.Println("Signal to quit received")
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := codeactions.Stop(ctx); err != nil {
log.Fatalf("Stop failed: %v\n", err)
}
}
func SetupLibs(s *server.Server) error {
codelibRepo := codelibRepoMongo.NewCodeLibRepo(s.DB)
codelibService := codelib.NewCodeLibService(codelibRepo)
{ // setup py libs
langPy := codelib.LanguageType(codelib.TypePy)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*60)
defer cancel()
currentLibs, err := codelibService.List(ctx, &langPy)
if err != nil {
return err
}
libs := []string{}
for _, lib := range currentLibs {
libs = append(libs, lib.Name)
}
if err := codelib.InstallPythonLibs(libs); err != nil {
log.Fatal(err)
}
}
return nil
}