-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
120 lines (104 loc) · 3.31 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"log"
"os"
"strconv"
"github.com/joho/godotenv"
"github.com/ystv/web-api/controllers/v1/clapper"
"github.com/ystv/web-api/controllers/v1/creator"
encoderPackage "github.com/ystv/web-api/controllers/v1/encoder"
"github.com/ystv/web-api/controllers/v1/misc"
"github.com/ystv/web-api/controllers/v1/people"
"github.com/ystv/web-api/controllers/v1/public"
"github.com/ystv/web-api/services/encoder"
"github.com/ystv/web-api/utils"
)
//go:generate swag init -o swagger/
// Version returns web-api's current version
var Version = "dev (0.8.0)"
// Commit returns latest commit hash
var Commit = "unknown"
func main() {
err := godotenv.Load()
if err != nil {
log.Printf("Failed to load global env file: %v", err)
} // Load .env file for production
err = godotenv.Load(".env.local") // Load .env.local for developing
if err != nil {
log.Printf("Failed to load env file, using global env: %v\n", err)
}
log.Printf("web-api version: %s, commit: %s\n", Version, Commit)
// Check if debugging
debug, err := strconv.ParseBool(os.Getenv("WAPI_DEBUG"))
if err != nil {
debug = false
err = os.Setenv("DEBUG", "false")
if err != nil {
log.Printf("failed to set env: %v", err)
}
}
if debug {
log.Println("Debug Mode - Disabled auth - don't run in production")
}
// Initialise backend connections
// Database
dbConfig := utils.DatabaseConfig{
Host: os.Getenv("WAPI_DB_HOST"),
Port: os.Getenv("WAPI_DB_PORT"),
SSLMode: os.Getenv("WAPI_DB_SSLMODE"),
Name: os.Getenv("WAPI_DB_NAME"),
Username: os.Getenv("WAPI_DB_USER"),
Password: os.Getenv("WAPI_DB_PASS"),
}
db, err := utils.NewDB(dbConfig)
if err != nil {
log.Fatalf("failed to connect DB: %+v", err)
}
log.Printf("Connected to DB: %s@%s", dbConfig.Username, dbConfig.Host)
// CDN
cdnConfig := utils.CDNConfig{
Endpoint: os.Getenv("WAPI_CDN_ENDPOINT"),
Region: os.Getenv("WAPI_CDN_REGION"),
AccessKeyID: os.Getenv("WAPI_CDN_ACCESSKEYID"),
SecretAccessKey: os.Getenv("WAPI_CDN_SECRETACCESSKEY"),
}
cdn, err := utils.NewCDN(cdnConfig)
if err != nil {
log.Fatalf("Unable to connect to CDN: %v", err)
}
log.Printf("Connected to CDN: %s@%s", cdnConfig.AccessKeyID, cdnConfig.Endpoint)
bucketConf := struct {
IngestBucket string
ServeBucket string
}{
IngestBucket: os.Getenv("WAPI_BUCKET_VOD_INGEST"),
ServeBucket: os.Getenv("WAPI_BUCKET_VOD_SERVE"),
}
access := utils.NewAccesser(utils.Config{
AccessCookieName: "token",
SecurityBaseURL: os.Getenv("WAPI_SECURITY_BASEURL"),
SigningKey: []byte(os.Getenv("WAPI_SIGNING_KEY")),
})
creatorConfig := &creator.Config{
IngestBucket: bucketConf.IngestBucket,
ServeBucket: bucketConf.ServeBucket,
}
encoderConfig := &encoder.Config{
VTEndpoint: os.Getenv("WAPI_VT_ENDPOINT"),
ServeBucket: bucketConf.ServeBucket,
}
enc := encoder.NewEncoder(db, cdn, encoderConfig)
New(&NewRouter{
Version: Version,
Commit: Commit,
DomainName: os.Getenv("WAPI_DOMAIN_NAME"),
Debug: debug,
Access: access,
Clapper: clapper.NewRepos(db, access),
Creator: creator.NewRepos(db, cdn, enc, access, creatorConfig),
Encoder: encoderPackage.NewEncoderController(enc, access),
Misc: misc.NewRepos(db, access),
People: people.NewRepo(db, access),
Public: public.NewRepos(db),
}).Start()
}