-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
53 lines (45 loc) · 951 Bytes
/
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
package main
import (
"database/sql"
"log"
"net/http"
"os"
"time"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/httprate"
)
func dataCleaner(db *sql.DB) {
for {
log.Print("Cleaning old data")
_ = purgeOldEntries(db)
time.Sleep(24 * time.Hour)
}
}
func main() {
db, err := openDB("insights.db")
if err != nil {
log.Fatal(err)
}
go dataCleaner(db)
r := chi.NewRouter()
r.Use(middleware.RealIP)
r.Use(middleware.Logger)
limiter := httprate.NewRateLimiter(1, 30*time.Minute, httprate.WithKeyByIP())
r.Use(limiter.Handler)
r.Post("/collect", handler(db))
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
log.Print("Starting Insights server on :" + port)
server := &http.Server{
Addr: ":" + port,
ReadHeaderTimeout: 3 * time.Second,
Handler: r,
}
err = server.ListenAndServe()
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}