-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
92 lines (75 loc) · 1.94 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
package main
import (
"context"
"fmt"
"io"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/cipherbin/cipher-bin-server/internal/app"
"github.com/cipherbin/cipher-bin-server/internal/db"
)
func main() {
f, err := os.OpenFile("errors.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("error opening file: %v", err)
}
defer f.Close()
mw := io.MultiWriter(os.Stdout, f)
log.SetOutput(mw)
db, err := db.New()
if err != nil {
log.Fatal(err)
}
defer db.Close()
appSrv := app.NewServer(db)
port := os.Getenv("CIPHER_BIN_PORT")
if port == "" {
port = "4000"
}
// Implement a graceful shutdown. This allows in-flight requests to complete before
// shutting down the server, preventing potential data loss or corruption.
httpServer := &http.Server{Addr: ":" + port, Handler: appSrv.Mux}
serverCtx, serverStopCtx := context.WithCancel(context.Background())
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
stopTicker := make(chan struct{})
go func() {
<-sig
shutdownCtx, _ := context.WithTimeout(serverCtx, 5*time.Second)
go func() {
<-shutdownCtx.Done()
if shutdownCtx.Err() == context.DeadlineExceeded {
log.Fatal("graceful shutdown timed out, forcing exit")
}
}()
if err := httpServer.Shutdown(shutdownCtx); err != nil {
log.Fatal(err.Error())
}
close(stopTicker)
serverStopCtx()
}()
// Spin off go routine that checks every 5 minutes for stale messages.
// If a message is 30 days or older, it will be destroyed.
go func() {
uptimeTicker := time.NewTicker(5 * time.Minute)
for {
select {
case <-uptimeTicker.C:
appSrv.Db.DestroyStaleMessages()
case <-stopTicker:
uptimeTicker.Stop()
return
}
}
}()
fmt.Printf("Serving application on port %s\n", port)
err = httpServer.ListenAndServe()
if err != nil && err != http.ErrServerClosed {
log.Fatal(err.Error())
}
<-serverCtx.Done()
}