-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
60 lines (52 loc) · 1.38 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
package main
import (
"context"
"fmt"
"net/http"
"os"
"os/signal"
"strconv"
"sync"
"syscall"
"time"
"github.com/davidmontoyago/go-event-ingestor-api/pkg/ingestor"
log "github.com/davidmontoyago/go-event-ingestor-api/pkg/log"
"github.com/gorilla/mux"
)
func main() {
// start work queue and workers
maxQueue := getEnvAsIntOrFail("MAX_QUEUE")
maxWorkers := getEnvAsIntOrFail("MAX_WORKERS")
workQueue := ingestor.NewWorkQueue(maxQueue, maxWorkers)
var waitgroup sync.WaitGroup
ctx, cancelFunc := context.WithCancel(context.Background())
workQueue.StartWorkProcessorPool(ctx, &waitgroup)
// do graceful termination
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)
go func() {
<-c
cancelFunc()
waitgroup.Wait()
log.Info.Println("all workers finished... exiting now.")
os.Exit(0)
}()
router := mux.NewRouter().StrictSlash(true)
router.HandleFunc("/ingest", func(w http.ResponseWriter, r *http.Request) {
ingestor.IngestPayload(w, r, workQueue)
}).Methods("PUT")
srv := &http.Server{
Handler: router,
Addr: ":8080",
WriteTimeout: 10 * time.Second,
ReadTimeout: 5 * time.Second,
}
log.Error.Fatal(srv.ListenAndServe())
}
func getEnvAsIntOrFail(key string) int {
value, err := strconv.Atoi(os.Getenv(key))
if err != nil {
log.Error.Fatal(fmt.Sprintf("must specify %s: %v", key, err))
}
return value
}