-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
63 lines (53 loc) · 1.15 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
package main
import (
"context"
"errors"
"fmt"
"log"
"net/http"
"os"
"time"
"oj/db"
"oj/handlers"
"oj/handlers/eventsource"
"oj/services/email"
"oj/worker"
"github.com/alexandrevicenzi/go-sse"
)
func main() {
err := db.DB.Ping()
if err != nil {
log.Fatalf("could not ping db: %s", err)
}
err = worker.Start(context.Background())
if err != nil {
log.Fatalf("could not start worker: %s", err)
}
go func() {
count := 0
for {
id := fmt.Sprint(count)
data := time.Now().Format(time.RFC3339Nano)
eventsource.SSE.SendMessage("", sse.NewMessage(id, data, "KEEP_ALIVE"))
count += 1
time.Sleep(30 * time.Second)
}
}()
err = email.Send("kable startup", "application started", os.Getenv("DEV_EMAIL"))
if err != nil {
log.Fatalf("failed to send startup email: %s", err)
}
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
handler := handlers.Router(db.DB)
log.Printf("listening on port %s", port)
err = http.ListenAndServe(":"+port, handler)
if errors.Is(err, http.ErrServerClosed) {
log.Printf("server closed\n")
} else if err != nil {
log.Printf("server closed unexpectedly: %v\n", err)
os.Exit(1)
}
}