-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
main.go
105 lines (90 loc) · 2.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
package main
import (
"context"
"flag"
"log"
"math/rand"
"net/http"
"os"
"strings"
"time"
"github.com/guregu/intertube/storage"
"github.com/guregu/intertube/tube"
"github.com/guregu/intertube/web"
)
var (
domainFlag = flag.String("domain", "", "domain")
bindFlag = flag.String("addr", ":8000", "addr to bind on")
cfgFlag = flag.String("cfg", "config.toml", "configuration file location")
)
func init() {
rand.Seed(time.Now().UnixNano())
}
func main() {
flag.Parse()
if *cfgFlag != "" {
cfg, err := readConfig(*cfgFlag)
if err != nil {
log.Fatalln("Failed to read config file:", *cfgFlag, "error:", err)
}
web.Domain = cfg.Domain
tube.Init(cfg.DB.Region, cfg.DB.Prefix, cfg.DB.Endpoint, cfg.DB.Debug)
storageCfg := storage.Config{
Type: storage.StorageType(cfg.Storage.Type),
FilesBucket: cfg.Storage.FilesBucket,
UploadsBucket: cfg.Storage.UploadsBucket,
CacheBucket: cfg.Storage.CacheBucket,
AccessKeyID: cfg.Storage.AccessKeyID,
AccessKeySecret: cfg.Storage.AccessKeySecret,
Region: cfg.Storage.Region,
Endpoint: cfg.Storage.Endpoint,
CFAccountID: cfg.Storage.CloudflareAccount,
SQSURL: cfg.Queue.SQS,
SQSRegion: cfg.Queue.Region,
}
storage.Init(storageCfg)
}
if os.Getenv("LAMBDA_TASK_ROOT") != "" {
// TODO: split these into separate binaries maybe
mode := os.Getenv("MODE")
log.Println("Lambda mode", mode)
switch mode {
case "WEB":
// web server
log.Println("deploy time:", web.Deployed)
web.Load()
startLambda()
case "CHANGE", "FILE":
startEventLambda(mode)
}
return
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
if err := tube.CreateTables(ctx); err != nil {
log.Fatalln("Failed to create tables:", err)
}
cancel()
if *domainFlag != "" {
web.Domain = *domainFlag
}
// web.MIGRATE_MAKEDUMPS()
// os.Exit(0)
// local server for dev
log.Println("Build date:", web.Deployed)
web.DebugMode = true
web.Load()
log.Println("Starting up local webserver at:", bindAddr())
closeWatch := web.WatchFiles()
if err := http.ListenAndServe(*bindFlag, nil); err != nil {
panic(err)
}
closeWatch()
}
func bindAddr() string {
addr := "http://"
if strings.HasPrefix(*bindFlag, ":") {
addr += "localhost"
}
addr += *bindFlag
return addr
}