forked from nytimes/gcs-helper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
60 lines (54 loc) · 1.35 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
package main
import (
"context"
"flag"
"fmt"
"log"
"net"
"net/http"
"os"
"cloud.google.com/go/storage"
"github.com/NYTimes/gcs-helper/v3/handlers"
"github.com/google/gops/agent"
"google.golang.org/api/option"
)
const version = "3.2.2"
func main() {
handleFlags()
err := agent.Listen(agent.Options{})
if err != nil {
log.Fatalf("could not start gops agent: %v", err)
}
defer agent.Close()
config, err := handlers.LoadConfig()
if err != nil {
log.Fatal(err)
}
logger := config.Logger()
hc, err := config.Client.HTTPClient()
if err != nil {
logger.WithError(err).Fatal("failed to initialize http client")
}
client, err := storage.NewClient(context.Background(), option.WithHTTPClient(hc))
if err != nil {
logger.WithError(err).Fatal("failed to create storage client instance")
}
handler := getHandler(config, client, hc)
listener, err := net.Listen("tcp", config.Listen)
if err != nil {
logger.WithField("listenAddr", config.Listen).WithError(err).Fatal("failed to start listener")
}
logger.Infof("Listening on %s...", listener.Addr())
err = http.Serve(listener, handler)
if err != nil {
logger.WithError(err).Fatal("failed to start server")
}
}
func handleFlags() {
printVersion := flag.Bool("version", false, "print version and exit")
flag.Parse()
if *printVersion {
fmt.Printf("gcs-helper %s\n", version)
os.Exit(0)
}
}