-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
executable file
·175 lines (152 loc) · 4.06 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package main
import (
"crypto/tls"
"github.com/brianhempel/sneakynote.com/store"
"log"
"net/http"
"sync/atomic"
"time"
"os"
"os/signal"
"syscall"
)
var (
mainStore *store.Store
lastStatusLogTime time.Time
)
func main() {
if len(os.Args) == 1 {
StartServer()
} else if os.Args[1] == "setup" {
SetupStore()
} else if os.Args[1] == "teardown" {
TeardownStore()
} else {
log.Print("Invalid argument ", os.Args[1])
log.Print(" ")
log.Print("No arguments starts the server.")
log.Print(" ")
log.Print("./sneakynote.com setup")
log.Print("will set up the datastore.")
log.Print(" ")
log.Print("./sneakynote.com teardown")
log.Print("will tear down the datastore.")
os.Exit(1)
}
}
func StartServer() {
MaybeSetupStore()
StartPeriodicStatusLogger()
log.Printf("Starting sweeper...")
StartSweeper()
port := os.Getenv("SNEAKYNOTE_PORT")
certs := os.Getenv("SNEAKYNOTE_CERTS")
privateKey := os.Getenv("SNEAKYNOTE_PRIVATE_KEY")
if port == "" {
port = "8080"
}
log.Printf("Starting SneakyNote server on port " + port + "!")
if certs == "" || privateKey == "" {
err := http.ListenAndServe(":" + port, Handlers())
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
} else {
go http.ListenAndServe(":80", RedirectToHTTPSHandler())
log.Print("Using TLS")
server := &http.Server{
Addr: ":" + port,
Handler: AddHSTSHeader(Handlers()),
TLSConfig: TLSConfig(),
}
err := server.ListenAndServeTLS(certs, privateKey)
if err != nil {
log.Fatal("ListenAndServeTLS: ", err)
}
}
}
func TLSConfig() *tls.Config {
return &tls.Config{
MinVersion: tls.VersionTLS10,
PreferServerCipherSuites: true,
CipherSuites: []uint16{
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
tls.TLS_RSA_WITH_AES_256_CBC_SHA,
tls.TLS_RSA_WITH_AES_128_CBC_SHA,
},
}
}
func GetStore() {
mainStore = store.Get()
}
func MaybeSetupStore() {
if _, err := os.Stat(store.Get().ExpiredPath); os.IsNotExist(err) {
SetupStore()
} else {
GetStore()
}
}
func SetupStore() {
log.Printf("Setting up datastore...")
mainStore = store.Setup()
}
func TeardownStore() {
log.Printf("Tearing down datastore...")
if mainStore == nil {
GetStore()
}
mainStore.Teardown()
}
func StartSweeper() {
go mainStore.SweepContinuously()
}
func StartPeriodicStatusLogger() {
lastStatusLogTime = time.Now()
ticker := time.NewTicker(time.Hour * 3)
go func() {
for range ticker.C {
logStatus()
}
}()
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt, os.Kill, syscall.SIGTERM)
go func() {
<-signalChan
logStatus()
os.Exit(0)
}()
}
func logStatus() {
now := time.Now()
created := atomic.SwapUint64(¬esCreatedCount, 0)
full := atomic.SwapUint64(¬eStorageFullRequestCount, 0)
tooLarge := atomic.SwapUint64(¬eTooLargeRequestCount, 0)
duplicateId := atomic.SwapUint64(¬eDuplicateIdRequestCount, 0)
opened := atomic.SwapUint64(¬esOpenedCount, 0)
expired := atomic.SwapUint64(¬eExpiredRequestCount, 0)
alreadyOpened := atomic.SwapUint64(¬eAlreadyOpenedRequestCount, 0)
notFound := atomic.SwapUint64(¬eNotFoundCount, 0)
status := atomic.SwapUint64(&statusRequestCount, 0)
assets := atomic.SwapUint64(&assetRequestCount, 0)
total := atomic.SwapUint64(&totalRequestCount, 0)
requestsPerSecond := float64(total) / now.Sub(lastStatusLogTime).Seconds()
log.Printf("Requests: total=%d rps=%.6f assets=%d Notes: created=%d opened=%d alreadyOpened=%d expired=%d notFound=%d full=%d tooLarge=%d duplicateId=%d status=%d",
total,
requestsPerSecond,
assets,
created,
opened,
alreadyOpened,
expired,
notFound,
full,
tooLarge,
duplicateId,
status)
lastStatusLogTime = now
}