-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.go
More file actions
75 lines (60 loc) · 1.87 KB
/
main.go
File metadata and controls
75 lines (60 loc) · 1.87 KB
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
package main
import (
"encoding/json"
"flag"
"fmt"
"net/http"
"sync"
"time"
)
func main() {
WEBHOOK_URL := flag.String("webhook_url", "http://localhost:3000/webhook", "where do you want your emitted event to go?")
LOG_DIR_JSON := flag.String("log_dir", "./logs/", "directory for logger")
flag.Parse()
mux := http.NewServeMux()
timer := CreateTimer(*WEBHOOK_URL, *LOG_DIR_JSON)
timer.Logger.Info().Msg(fmt.Sprintf("Sending events on webhook URL %s", *WEBHOOK_URL))
// System goroutine;
timer.SysWg.Add(1)
// Handle when a user tries to register a timeout
// Spawn the goroutine
mux.HandleFunc("POST /register", timer.RegisterHandler)
mux.HandleFunc("POST /cancel", timer.CancelHandler)
mux.HandleFunc("POST /remaining", timer.RemainingHandler)
mux.HandleFunc("POST /extend", timer.ExtendHandler)
mux.HandleFunc("GET /test", func(w http.ResponseWriter, r *http.Request) {
// log.Println("Received GET /test")
timer.Logger.Info().Msg("Received GET /test")
response := struct {
Time int64 `json:"time"`
Data string `json:"data"`
}{
Time: time.Now().Unix(),
Data: fmt.Sprintf("saturn is up: %d timer(s) running", len(timer.State.TimerMap)),
}
responseBytes, _ := json.Marshal(response)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = w.Write(responseBytes)
})
// placeholder for the webhook
mux.HandleFunc("POST /webhook", timer.WebhookTest)
var systemwg sync.WaitGroup
systemwg.Add(2)
// Spawn server goroutine
go func() {
timer.Logger.Info().Msg("Starting Server...")
err := http.ListenAndServe(":3000", mux)
if err != nil {
// log.Println(fmt.Errorf("Error in server -> %v", err))
timer.Logger.Error().Msg(fmt.Sprintf("Error in server: %v", err))
defer systemwg.Done()
}
}()
// Spawn timer goroutine
go func() {
defer systemwg.Done()
timer.SysWg.Wait()
}()
systemwg.Wait()
}