-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
157 lines (129 loc) · 3.86 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"context"
"flag"
"github.com/gorilla/mux"
"github.com/kelseyhightower/envconfig"
log "github.com/sirupsen/logrus"
"golang.org/x/oauth2"
"github.com/gorilla/sessions"
"github.com/gorilla/securecookie"
"net/http"
"os"
"fmt"
"os/signal"
"time"
"encoding/json"
"io/ioutil"
)
var config EnvVars
var authRules AuthRules
var oauthConf *oauth2.Config
var store *sessions.CookieStore
var sessionTokenName string
var awsListBuckets []string
var gcpListBuckets []string
var semaphoreAws chan struct{}
var semaphoreGcp chan struct{}
func logInit() {
log.SetFormatter(&log.JSONFormatter{
DisableHTMLEscape: true,
PrettyPrint: true,
})
log.SetOutput(os.Stdout)
log.SetLevel(log.InfoLevel)
log.SetReportCaller(true)
}
func main() {
logInit()
err := envconfig.Process("", &config)
if err != nil {
log.Error("Fail to parse Env variables, ", err)
os.Exit(1)
}
level, err := log.ParseLevel(config.Log)
if err != nil {
log.Error(err)
os.Exit(1)
}
log.SetLevel(level)
log.Info("Log level set to ", level)
jsonFile, err := os.Open(config.AuthFile)
defer jsonFile.Close()
if err != nil {
log.Error("Fail to parse Auth file ", err)
os.Exit(1)
}
byteValue, _ := ioutil.ReadAll(jsonFile)
json.Unmarshal(byteValue, &authRules)
err = sortAndValidateAuthRules(authRules.AuthRules)
if err != nil {
log.Error(err)
os.Exit(1)
}
log.Info(authRules)
sessionTokenName = "s3-web-client-token"
authInit(config.ClientID, config.ClientSecret, config.RedirectURL)
store = sessions.NewCookieStore(securecookie.GenerateRandomKey(64),securecookie.GenerateRandomKey(32))
store.Options = &sessions.Options{
MaxAge: 60 * 60, // 1 hour to match google oauth token
HttpOnly: true,
}
// Initialize all semaphores
semaphoreGcp = make(chan struct{}, 1)
semaphoreAws = make(chan struct{}, 1)
semaphoreCachedBucketObjects = make(chan struct{}, 1)
// List Buckets
getAllBuckets ()
ticker := time.NewTicker(time.Duration(config.TimeoutCache) * time.Second)
done := make(chan bool)
go func() {
for {
select {
case <-done:
break
case t := <-ticker.C:
log.Trace(t)
getAllBuckets()
}
}
log.Info("getAllBuckets goroutine stopped")
} ()
var wait time.Duration
flag.DurationVar(&wait, "graceful-timeout", time.Second*10, "the duration for which the server gracefully wait for existing connections to finish")
flag.Parse()
r := mux.NewRouter()
r.PathPrefix("/css").Handler(http.StripPrefix("/css", http.FileServer(http.Dir("./static/css"))))
r.PathPrefix("/js").Handler(http.StripPrefix("/js", http.FileServer(http.Dir("./static/js"))))
r.HandleFunc("/login", loginHandler).Methods("GET")
r.HandleFunc("/logout", logoutHandler).Methods("GET")
r.HandleFunc("/auth", authHandler).Methods("GET")
r.HandleFunc("/main/{bucket}", bucketHandler).Methods("GET")
r.HandleFunc("/health", healthHandler).Methods("GET")
log.Info("Starting Server with host ",config.Host, " and port ", config.Port)
srv := &http.Server{
Addr: fmt.Sprintf("%s:%s", config.Host, config.Port),
WriteTimeout: time.Second * 15,
ReadTimeout: time.Second * 15,
IdleTimeout: time.Second * 60,
Handler: r,
}
go func() {
err := srv.ListenAndServe()
if err != nil {
log.Error(err)
}
}()
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
// Block until we receive our signal.
<-c
// Create a deadline to wait for.
ticker.Stop()
done <- true
ctx, cancel := context.WithTimeout(context.Background(), wait)
defer cancel()
srv.Shutdown(ctx)
log.Info("shutting down")
os.Exit(0)
}