forked from git-lfs/lfs-test-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
118 lines (97 loc) · 2.6 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
package main
import (
"crypto/tls"
"fmt"
"net"
"os"
"os/signal"
"syscall"
"time"
)
const (
contentMediaType = "application/vnd.git-lfs"
metaMediaType = contentMediaType + "+json"
version = "0.4.0"
)
var (
logger = NewKVLogger(os.Stdout)
)
// tcpKeepAliveListener sets TCP keep-alive timeouts on accepted
// connections. It's used by ListenAndServe and ListenAndServeTLS so
// dead TCP connections (e.g. closing laptop mid-download) eventually
// go away.
type tcpKeepAliveListener struct {
*net.TCPListener
}
func (ln tcpKeepAliveListener) Accept() (c net.Conn, err error) {
tc, err := ln.AcceptTCP()
if err != nil {
return
}
tc.SetKeepAlive(true)
tc.SetKeepAlivePeriod(3 * time.Minute)
return tc, nil
}
func wrapHttps(l net.Listener, cert, key string) (net.Listener, error) {
var err error
config := &tls.Config{}
if config.NextProtos == nil {
config.NextProtos = []string{"http/1.1"}
}
config.Certificates = make([]tls.Certificate, 1)
config.Certificates[0], err = tls.LoadX509KeyPair(cert, key)
if err != nil {
return nil, err
}
netListener := l.(*TrackingListener).Listener
tlsListener := tls.NewListener(tcpKeepAliveListener{netListener.(*net.TCPListener)}, config)
return tlsListener, nil
}
func main() {
if len(os.Args) == 2 && os.Args[1] == "-v" {
fmt.Println(version)
os.Exit(0)
}
var listener net.Listener
tl, err := NewTrackingListener(Config.Listen)
if err != nil {
logger.Fatal(kv{"fn": "main", "err": "Could not create listener: " + err.Error()})
}
listener = tl
if Config.IsHTTPS() {
logger.Log(kv{"fn": "main", "msg": "Using https"})
listener, err = wrapHttps(tl, Config.Cert, Config.Key)
if err != nil {
logger.Fatal(kv{"fn": "main", "err": "Could not create https listener: " + err.Error()})
}
}
metaStore, err := NewMetaStore(Config.MetaDB)
if err != nil {
logger.Fatal(kv{"fn": "main", "err": "Could not open the meta store: " + err.Error()})
}
contentStore, err := NewContentStore(Config.ContentPath)
if err != nil {
logger.Fatal(kv{"fn": "main", "err": "Could not open the content store: " + err.Error()})
}
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGHUP)
go func(c chan os.Signal, listener net.Listener) {
for {
sig := <-c
switch sig {
case syscall.SIGHUP: // Graceful shutdown
tl.Close()
}
}
}(c, tl)
logger.Log(kv{"fn": "main", "msg": "listening", "pid": os.Getpid(), "addr": Config.Listen, "version": version})
app := NewApp(contentStore, metaStore)
if Config.IsUsingTus() {
tusServer.Start()
}
app.Serve(listener)
tl.WaitForChildren()
if Config.IsUsingTus() {
tusServer.Stop()
}
}