-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
99 lines (80 loc) · 2.45 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
94
95
96
97
98
99
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/do4-2022/grobuzin/database"
"github.com/do4-2022/grobuzin/objectStorage"
"github.com/do4-2022/grobuzin/routes"
"github.com/do4-2022/grobuzin/scheduler"
"github.com/caarlos0/env/v10"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
_ "github.com/joho/godotenv/autoload"
)
type Config struct {
// rootFsStorageDSN string `env:"ROOT_FS_STORAGE_DSN,notEmpty"`
LambdoHost string `env:"LAMBDO_HOST,notEmpty"`
LambdoPort int `env:"LAMBDO_PORT,notEmpty"`
VMStateURL string `env:"VM_STATE_URL,notEmpty"`
FuntionStateStorageDSN string `env:"FUNCTION_STATE_STORAGE_DSN,notEmpty" envDefault:"host=localhost user=postgres password=postgres dbname=postgres port=5432 sslmode=disable TimeZone=Asia/Shanghai"`
JWTSecret string `env:"JWT_SECRET,notEmpty"`
BuilderEndpoint string `env:"BUILDER_ENDPOINT,notEmpty"`
MinioEndpoint string `env:"MINIO_ENDPOINT,notEmpty"`
MinioAccessKey string `env:"MINIO_ACCESS_KEY,notEmpty"`
MinioSecretKey string `env:"MINIO_SECRET_KEY,notEmpty"`
MinioSecure bool `env:"MINIO_SECURE" envDefault:"false"`
}
func main() {
cfg := Config{}
if err := env.Parse(&cfg); err != nil {
log.Fatalf("%+v\n", err)
}
ctx := context.Background()
redis := database.InitRedis(cfg.VMStateURL)
bucketPrefix := "http://"
if cfg.MinioSecure {
bucketPrefix = "https://"
}
s := &scheduler.Scheduler{
Redis: redis,
Context: &ctx,
Lambdo: &scheduler.LambdoService{
Host: cfg.LambdoHost,
Port: cfg.LambdoPort,
BucketURL: fmt.Sprint(
bucketPrefix,
cfg.MinioEndpoint,
"/",
objectStorage.BucketName,
),
},
}
//Now inject the scheduler into the routes that need it!
go func() {
// every 24 hours we check for
for {
time.Sleep(time.Hour * 6)
log.Println("Ran instance pruning at", time.Now().UTC())
s.FindAndDestroyUnsused(24)
}
}()
db := database.Init(cfg.FuntionStateStorageDSN)
r := routes.GetRoutes(db, cfg.JWTSecret, cfg.BuilderEndpoint, getMinioClient(cfg), *s)
err := r.Run()
if err != nil {
panic(err)
}
}
func getMinioClient(cfg Config) *minio.Client {
// Initialize minio client object.
minioClient, err := minio.New(cfg.MinioEndpoint, &minio.Options{
Creds: credentials.NewStaticV4(cfg.MinioAccessKey, cfg.MinioSecretKey, ""),
Secure: cfg.MinioSecure,
})
if err != nil {
log.Fatalln(err)
}
return minioClient
}