forked from cloudfoundry/go-fetcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
115 lines (93 loc) · 2.74 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
package main
import (
"flag"
"fmt"
"log"
"net/http"
"net/url"
"os"
"golang.org/x/oauth2"
"github.com/cloudfoundry/go-fetcher/cache"
"github.com/cloudfoundry/go-fetcher/config"
"github.com/cloudfoundry/go-fetcher/handlers"
"github.com/cloudfoundry/go-fetcher/util"
"github.com/google/go-github/github"
"github.com/tedsuo/ifrit"
"github.com/tedsuo/ifrit/grouper"
"github.com/tedsuo/ifrit/http_server"
"github.com/tedsuo/ifrit/sigmon"
"github.com/pivotal-golang/clock"
"github.com/pivotal-golang/lager"
)
var generateConfig = flag.Bool(
"generateConfig",
false,
"Generate deployment configurations",
)
func main() {
// if the flag `generate_config` is set to true, run the code to generate
// config.json and manifest.yml from the provided templates
flag.Parse()
if *generateConfig {
templateFile := os.Getenv("ROOT_DIR") + "/util/config.json.template"
configFile := os.Getenv("ROOT_DIR") + "/config.json"
err := util.GenerateConfig(templateFile, configFile)
if err != nil {
log.Fatal(err)
}
templateFile = os.Getenv("ROOT_DIR") + "/util/manifest.yml.template"
configFile = os.Getenv("ROOT_DIR") + "/manifest.yml"
err = util.GenerateManifest(templateFile, configFile)
if err != nil {
log.Fatal(err)
}
return
}
configFile := os.Getenv("CONFIG")
config, err := config.Parse(configFile)
if err != nil {
panic("config file error: " + err.Error())
}
logger := lager.NewLogger("go-fetcher")
sink := lager.NewReconfigurableSink(lager.NewWriterSink(os.Stdout, lager.DEBUG), config.GetLogLevel())
logger.RegisterSink(sink)
port := os.Getenv("PORT")
if port == "" {
logger.Error("server.failed", fmt.Errorf("$PORT must be set"))
}
clock := clock.NewClock()
locationCache := cache.NewLocationCache(logger.Session("cache"), clock)
handler := handlers.NewHandler(logger, *config, locationCache)
http.HandleFunc("/", handler.GetMeta)
var tc *http.Client
if config.GithubAPIKey != "" {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: config.GithubAPIKey},
)
tc = oauth2.NewClient(oauth2.NoContext, ts)
}
client := github.NewClient(tc)
githubURL, err := url.Parse(config.GithubURL)
if err != nil {
log.Fatal(err)
}
client.BaseURL = githubURL
httpServer := http_server.New(":"+port, http.DefaultServeMux)
cacheLoader := cache.NewCacheLoader(
logger.Session("cache-loader"),
config.GithubURL, config.OrgList, locationCache, client.Repositories, clock,
)
members := grouper.Members{
{"cache-loader", cacheLoader},
{"http-server", httpServer},
}
group := grouper.NewOrdered(os.Interrupt, members)
monitor := ifrit.Invoke(sigmon.New(group))
logger.Info("started")
err = <-monitor.Wait()
if err != nil {
logger.Error("exited-with-failure", err)
os.Exit(1)
}
logger.Info("exited")
}