-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
64 lines (52 loc) · 1.27 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
package main
import (
"context"
"fmt"
"net/http"
"os"
"os/signal"
"time"
joonix "github.com/joonix/log"
"github.com/sirupsen/logrus"
log "github.com/sirupsen/logrus"
)
func init() {
// Define different logger for Google Cloud Run
if v := os.Getenv("K_SERVICE"); v != "" {
log.SetFormatter(joonix.NewFormatter())
}
log.SetLevel(logrus.DebugLevel)
}
func main() {
if os.Getenv("PUT_IO_TOKEN") == "" {
log.Fatalln("You must specify a PUT_IO_TOKEN. Please refer to https://app.put.io/settings/account/oauth/authorizations")
}
var port string = "8000"
if v := os.Getenv("PORT"); v != "" {
port = v
}
// Ticker for refresh in-memory users
ticker := time.NewTicker(5 * time.Minute)
defer ticker.Stop()
srv := &http.Server{
Addr: fmt.Sprintf(":%s", port),
WriteTimeout: time.Second * 15,
ReadTimeout: time.Second * 15,
IdleTimeout: time.Second * 60,
Handler: newRouter(ticker),
}
go func() {
log.Printf("Listening on %s", srv.Addr)
if err := srv.ListenAndServe(); err != nil {
log.Fatalln(err)
}
}()
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
<-c
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
_ = srv.Shutdown(ctx)
log.Println("Shutting down server")
os.Exit(0)
}